1、直接从Editor中生成一个空的插件模板
然后最右下角
2、关掉vs,右键生成一下工程文件,把Plugins扫进去
打开解决方案开始编写插件
瞧,插件加进来了
3、编写插件
1- 首先把插件的配置文TestPlugin.uplugin件改一下(被这个坑了两天)
这个LoadingPhase的值默认为Default,必须修改为PreDefault,不然重启Editor会报关联不上插件源码的错误,切记!
2- 修改编译模块配置TestPlugin.Build.cs文件,c#文件
详细代码,里面有注释
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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
3- 写个自定义的char – TestChar,继承自Character
先看下文件结构,需要蓝图可见的必须丢到Public下
1、先修改预编译头文件TestPluginPrivatePCH.h,必须包含CoreUObject,不然编译不过,切记!
#include "TestPlugin.h"
// UObject core
#include "CoreUObject.h" //默认是不含这个的
// Actor based classes
#include "GameFramework/Character.h" //包插件中所有用的的引擎类都丢到这里来
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2、头文件,正常编写自定义的类一样
#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;
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
3、 cpp文件,包含的是预编译文件和类的头文件
#include "TestPluginPrivatePCH.h"
#include "TestChar.h"
#include "TestLib.h" //引入的第三方库的头文件
ATestChar::ATestChar() : Super()
{
mAge = myPrint("hello world", 123); //第三方库中的方法
mName = "yangx";
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
//第三方库打成了一个静态库TestLib.lib
TestLib.h
#ifndef __TEST_LIB_H__
#define __TEST_LIB_H__
#include <string>
#include <iostream>
int myPrint(std::string _name, int _age);
#endif
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
TestLib.cpp
#include "TestLib.h"
int myPrint(std::string _name, int _age)
{
return _age + 1000;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
4、编译运行,在Editor中create一个Blueprint继承自这个TestChar类
5、拖到场景运行游戏
6、插件的git地址
https://github.com/yangxuan0261/SlatePlugin.git
本文转自:http://blog.csdn.net/yangxuan0261/article/details/52098104