UE4開發插件流程
1. 直接從Editor中生成一個空的插件模板
2. 關(guān)掉(diào)vs,右(yòu)鍵生成一下工程文件,把Plugins掃進(jìn)去
3. 打開(kāi)解決方案開始編寫插(chā)件
首(shǒu)先(xiān)把插件的配置文TestPlugin.uplugin件改一下(被(bèi)這個坑了兩天)
這個LoadingPhase的值(zhí)默認為Default,必須(xū)修改為PreDefault,不然重啟Editor會報關聯不上插(chā)件源碼的(de)錯誤,切記!
修改編譯模塊配置TestPlugin.Build.cs文件,c#文件
詳(xiáng)細代碼(mǎ),裏麵有注釋
using UnrealBuildTool;
using System.IO; //路徑(jìng)獲取需要(yào)用到IO
public class TestPlugin : ModuleRules
{
private string ModulePath //當前TestPlugin.Build.cs文件所在的路徑
{
get { return Path.GetDirectoryName(RulesCompiler.GetModuleFilename(this.GetType().Name)); }
}
private string ThirdPartyPath //這個插件引(yǐn)用的第(dì)三方庫的目錄
{
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( //公有文件(jiàn)搜索路徑
new string[] {
"TestPlugin/Public"
// ... add public include paths required here ...
}
);
PrivateIncludePaths.AddRange(
new string[] {
"TestPlugin/Private" //私有文件(jiàn)搜索路徑
// ... 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))//平台(tái)判斷
{
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"));//加載第三(sān)方靜態庫.lib
}
if (isLibrarySupported) //成功加載(zǎi)庫的情(qíng)況下,包含第三方庫的頭文件
{
// Include path
System.Console.WriteLine("----- PublicIncludePaths.Add true");
PublicIncludePaths.Add(Path.Combine(MyTestLibPath, "Include"));
}
return isLibrarySupported;
}
}
寫個自(zì)定(dìng)義的char – TestChar,繼承自Character
先看下(xià)文件結構,需要藍(lán)圖可見(jiàn)的必須丟到Public下
先修改預編譯頭文(wén)件TestPluginPrivatePCH.h,必(bì)須包(bāo)含CoreUObject,不然編譯(yì)不過,切記!
#include "TestPlugin.h"
// UObject core#include "CoreUObject.h" //默認是不含這個的
// Actor based classes
#include "GameFramework/Character.h" //包插件中所有用的的引擎類都丟到這裏來(lái)
頭文件,正常編寫自定(dìng)義的類一(yī)樣
#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;
};
//cpp文件,包含的是預編譯(yì)文(wén)件和(hé)類的頭文件
#include "TestPluginPrivatePCH.h"
#include "TestChar.h"
#include "TestLib.h" //引入的第三(sān)方庫的頭文件
ATestChar::ATestChar() : Super()
{
mAge = myPrint("hello world", 123); //第三方庫(kù)中的方法
mName = "yangx";
}
第三方(fāng)庫打成了(le)一個靜態庫TestLib.lib
TestLib.h
#ifndef __TEST_LIB_H__
#define __TEST_LIB_H__
#include
#include
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繼承(chéng)自這個TestChar類
5. 拖到場景運行遊戲
- 上一篇:使用HTC VIVE和WISEGLOVE的(de)VR技術實現(xiàn)火災現 2017/7/6
- 下一篇:HoloLens 開發簡(jiǎn)要教程(Unity3d+VS2015 2017/6/29