• 您的位置(zhì):首頁 > 新聞動態 > 技術文章

    UE4開(kāi)發插件(jiàn)流程

    2017/7/4      點擊:


    1. 直接從(cóng)Editor中生成一個(gè)空的插件模板
    2. 關掉vs,右鍵生成(chéng)一下工程文件,把Plugins掃進去
    3. 打(dǎ)開解決方案開始編寫插件 
    首先把插件(jiàn)的配置文TestPlugin.uplugin件改一下(被這個坑了兩天) 
    這(zhè)個LoadingPhase的值默認為Default,必須(xū)修(xiū)改為PreDefault,不然重啟(qǐ)Editor會報(bào)關聯不上插件源碼的錯誤(wù),切記! 

    修改編譯模塊配置TestPlugin.Build.cs文(wén)件,c#文件 

    詳細代碼,裏麵有注釋 
    using UnrealBuildTool;
    using System.IO; //路徑獲取(qǔ)需要用到IO


    public class TestPlugin : ModuleRules
    {
        private string ModulePath //當(dāng)前TestPlugin.Build.cs文件所在的路徑
        {
            get { return Path.GetDirectoryName(RulesCompiler.GetModuleFilename(this.GetType().Name)); }
        }


        private string ThirdPartyPath //這個插件引用的第三(sān)方庫的目(mù)錄(lù)
        {
            get { return Path.GetFullPath(Path.Combine(ModulePath, "../../ThirdParty/")); }
        }


        private string MyTestLibPath //第(dì)三方庫MyTestLib的目錄(lù)
        {
            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" //私(sī)有(yǒu)文件(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); //加(jiā)載第三(sān)方庫
        }


        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) //成功加(jiā)載庫的情況下(xià),包含第三方庫的頭文件
            {
                // Include path
                System.Console.WriteLine("----- PublicIncludePaths.Add true"); 
                PublicIncludePaths.Add(Path.Combine(MyTestLibPath, "Include"));
            }
            return isLibrarySupported;
        }
    }


    寫個自定義的char – TestChar,繼承自Character 
    先看下文件結構,需要藍圖可見的必須(xū)丟到Public下 

    先修改預編(biān)譯頭文件TestPluginPrivatePCH.h,必須(xū)包含CoreUObject,不(bú)然編(biān)譯不過,切記!

    #include "TestPlugin.h"

    // UObject core
    #include "CoreUObject.h" //默認是不含這個的

    // Actor based classes
    #include "GameFramework/Character.h" //包插(chā)件中所有用的的引(yǐn)擎類都丟到這裏來


    頭文件,正常編寫自定義的類一樣
    #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" //引入的(de)第三方庫的頭文件
    ATestChar::ATestChar() : Super()
    {
        mAge = myPrint("hello world", 123); //第三方庫中的(de)方法
        mName = "yangx";
    }


    第三方庫打成了一個靜態庫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. 編(biān)譯運行,在Editor中(zhōng)create一個Blueprint繼承自這個TestChar類

    5. 拖到場景運行遊(yóu)戲


    网站地图 正能量网-传播正能量弘扬主旋律-免费正能量网站www正能量-免费正能量网站www正能量下载在线观看官网