【转载】UE4 插件开发

842 阅读2分钟

本文转自:UE4 插件开发

1、生成空插件模板

直接从 Editor 中生成一个空的插件模板

image.png

然后最右下角

image.png

image.png

2、生成 VS 工程文件

关掉 VS ,右键生成一下工程文件,把 Plugins 扫进去

image.png

打开解决方案开始编写插件

瞧,插件加进来了

image.png

3、编写插件

  • 首先把插件的配置文件 TestPlugin.uplugin 改一下(被这个坑了两天) 这个 LoadingPhase 的值默认为 Default ,必须修改为 PreDefault,不然重启 Editor 会报 关联不上插件源码的错误切记!

image.png

  • 修改编译模块配置 TestPlugin.Build.cs 文件,C# 文件

image.png

详细代码如下,里面有注释

using UnrealBuildTool;
using System.IO;
// 路径获取需要用到 IO
public class TestPlugin : ModuleRules 
{
	private string ModulePath // 当前 TestPlugin.Build.cs 文件所在的路径 
	{
		get 
		{
			return Path.GetDirectoryName(RulesCompiler.GetModuleFilename(this.GetType().Name));
		}
	}
        
	private string ThirdPartyPath // 这个插件引用的第三方库的目录 
	{
		get 
		{
			return Path.GetFullPath(Path.Combine(ModulePath, "../../ThirdParty/"));
		}
	}
        
	private string MyTestLibPath // 第三方库 MyTestLib 的目录 
	{
		get 
		{
			return Path.GetFullPath(Path.Combine(ThirdPartyPath, "MyTestLib"));
		}
	}
        
	public TestPlugin(TargetInfo Target) 
	{
		PublicIncludePaths.AddRange( // 公有文件搜索路径
		new string[] 
		{
			"TestPlugin/Public"
			// ... add public include paths required here ...
		}
		);
                
		PrivateIncludePaths.AddRange(
		            new string[] 
		{
			"TestPlugin/Private" // 私有文件搜索路径
			// ... add other private include paths required here ...
		}
		);
                
		PublicDependencyModuleNames.AddRange(
		            new string[] 
		{
			"Core"
			// ... add other public dependencies that you statically link with here ...
		}
		);
                
		PrivateDependencyModuleNames.AddRange(
		            new string[] 
		{
			"CoreUObject",
                        "Engine",
                        "Slate",
                        "SlateCore",
			// ... add private dependencies that you statically link with here ...
		}
		);
                
		DynamicallyLoadedModuleNames.AddRange(
		            new string[] 
		{
			// ... add any modules that your module loads dynamically here ...
		}
		);
                
		LoadThirdPartyLib(Target);
		// 加载第三方库
	}
        
	public bool LoadThirdPartyLib(TargetInfo Target) 
	{
		bool isLibrarySupported = false;
		if ((Target.Platform == UnrealTargetPlatform.Win64) || (Target.Platform == UnrealTargetPlatform.Win32))// 平台判断 
		{
			isLibrarySupported = true;
			System.Console.WriteLine("----- isLibrarySupported true");
			string PlatformSubPath = (Target.Platform == UnrealTargetPlatform.Win64) ? "Win64" : "Win32";
			string LibrariesPath = Path.Combine(MyTestLibPath, "Lib");
			PublicAdditionalLibraries.Add(Path.Combine(LibrariesPath, PlatformSubPath, "TestLib.lib"));
			// 加载第三方静态库 .lib
		}
		if (isLibrarySupported) // 成功加载库的情况下,包含第三方库的头文件 
		{
			// Include path
			System.Console.WriteLine("----- PublicIncludePaths.Add true");
			PublicIncludePaths.Add(Path.Combine(MyTestLibPath, "Include"));
		}
		return isLibrarySupported;
	}
}
  • 写个自定义的 char – TestChar,继承自 Character

先看下文件结构,需要 蓝图可见必须 丢到 Public

image.png

1、先修改预编译头文件 TestPluginPrivatePCH.h ,必须包含 CoreUObject,不然编译不过,切记!

#include "TestPlugin.h"
 
// UObject core
#include "CoreUObject.h" // 默认是不含这个的
 
// Actor based classes
#include "GameFramework/Character.h" // 包插件中所有用的的引擎类都丢到这里来

2、插件 TestChar.h 文件,正常编写自定义的类一样

#pragma once

#include "GameFramework/Character.h"
#include "TestChar.generated.h"

UCLASS()
class ATestChar : public ACharacter
{
    GENERATED_BODY()

public:
    // Sets default values for this character\'s properties
    ATestChar();

    UPROPERTY(EditAnywhere, Category = "Test Char")
    int32 mAge;
    UPROPERTY(EditAnywhere, Category = "Test Char")
    FString mName;
};

3、 插件 TestChar.cpp 文件,包含的是 预编译文件类的头文件

#include "TestPluginPrivatePCH.h"
#include "TestChar.h"
#include "TestLib.h" ///< 引入的第三方库的头文件
 
ATestChar::ATestChar() : Super()
{
    mAge = myPrint("hello world", 123); ///< 第三方库中的方法
    mName = "yangx";
}

插件引用的第三方库打成一个静态库 TestLib.lib

TestLib.h

#ifndef __TEST_LIB_H__
#define __TEST_LIB_H__
#include <string>
#include <iostream>
 
int myPrint(std::string _name, int _age);
 
#endif

TestLib.cpp

#include "TestLib.h"
 
int myPrint(std::string _name, int _age)
{
    return _age + 1000;
}

4、编译运行,在 Editor 中 create 一个 Blueprint 继承自这个 TestChar 插件类

image.png

image.png

5、拖到场景运行游戏

效果如下

image.png

4、插件的 Git 地址