编写ue4插件
1. 直接从Editor中生成一个空的插件模板
然后最右下角
2. 关掉vs,右键生成一下工程文件,把Plugins扫进去
打开解决方案开始编写插件
瞧,插件加进来了
3. 编写插件
首先把插件的配置文TestPlugin.uplugin件改一下(被这个坑了两天)
这个LoadingPhase的值默认为Default,必须修改为PreDefault,不然重启Editor会报关联不上插件源码的错误,切记!
修改编译模块配置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; } }
- 详细代码,里面有注释
写个自定义的char – TestChar,继承自Character
先看下文件结构,需要蓝图可见的必须丢到Public下
先修改预编译头文件TestPluginPrivatePCH.h,必须包含CoreUObject,不然编译不过,切记!
#include "TestPlugin.h" // UObject core #include "CoreUObject.h" //默认是不含这个的 // Actor based classes #include "GameFramework/Character.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; };
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; }