UE4利用插件(Plugins)导入Dll和.so

在unity下使用dll非常的方便,然而在unreal下。。。不说了都是泪。

本文使用的引擎版本是ue4.21.2

进入正题,我们使用一个插件来管理第三方的Dll和.so。

首先在已经有的项目工程中新建一个插件,

创建好插件后,在项目的目录下会找到Plugins/YOURplugin目录,其中内容如下图所示:(我的插件叫STmobile)

将要添加的Dll和.so复制进ThirdParty目录下,(当然你可以在ThirdParty下创建合适的文件路径):

然后打开YOURplugin.build.cs,添加如下代码:

private string ModulePath
{
get { return ModuleDirectory; }
}

private string ThirdPartyPath
{
get { return Path.GetFullPath(Path.Combine(ModulePath, "../../ThirdParty/")); }
}

    
public STmobile(ReadOnlyTargetRules Target) : base(Target) { PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs; if (Target.Platform == UnrealTargetPlatform.Win64) { PublicDelayLoadDLLs.Add(Path.Combine(ThirdPartyPath, "Win64", "st_mobile.dll")); PublicDelayLoadDLLs.Add(Path.Combine(ThirdPartyPath, "Win64", "STAvatar.dll")); RuntimeDependencies.Add(new RuntimeDependency(Path.Combine(ThirdPartyPath, "Win64", "st_mobile.dll"))); RuntimeDependencies.Add(new RuntimeDependency(Path.Combine(ThirdPartyPath, "Win64", "STAvatar.dll"))); } 。。。。。。。 }

  注意路径不要搞错哦。.so同理,

    public STmobile(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;
        if (Target.Platform == UnrealTargetPlatform.Android)
        {
            AdditionalPropertiesForReceipt.Add(new ReceiptProperty("AndroidPlugin", Path.Combine(ModuleDirectory, "STmobile_APL.xml")));

            PublicAdditionalLibraries.Add(ThirdPartyPath + "Android/libs/armeabi-v7a/libxxxx.so");
            PublicAdditionalLibraries.Add(ThirdPartyPath + "Android/libs/armeabi-v7a/libxxxx2.so");
        }
}

  

这里会多添加一个xml文件,这个是干啥的呢?当我们打包android包时,会利用这个文件将.so复制到打包的目录下,如果没有这一步,打包出的apk运行时,会crash报找不到libxxx.so的错误,这个xml文件要放在YOURplugin.build.cs的同一目录下,下面是这个xml的主要内容:

<?xml version="1.0" encoding="utf-8"?>
<!-- steps to add to build additions -->
<root xmlns:android="http://schemas.android.com/apk/res/android">
 <!-- init section is always evaluated once per architecture -->
  <init>
      <log text="GameFramework init"/>
    <setBool result="bSupported" value="false"/>
            <isArch arch="armeabi-v7a">
                <setBool result="bSupported" value="true"/>
            </isArch>
  </init>
  
  <!-- optional files or directories to copy to Intermediate/Android/APK -->
  <resourceCopies>    
    <isArch arch="armeabi-v7a">
      <log text="Copying libxxx.so"/>
      <copyFile src="$S(PluginDir)/../../ThirdParty/Android/libs/armeabi-v7a/libxxx.so"
                      dst="$S(BuildDir)/libs/armeabi-v7a/libxxx.so" />
     
    </isArch>
    </resourceCopies>
</root>

Ok,前期准备工作完后,关闭工程,重新生成下.sln。

然后就是Dll函数的使用,在ue4工程中怎么使用Dll函数呢?在c++中我们有两种调用Dll函数的形式:

  1. 显式调用
  2. 隐式调用

这里我们使用显示调用,首先在YOURplugin.cpp的StartupModule函数中获得Dll的handle,如插件中的代码示例,照葫芦画瓢即可。

得到handle后,即可显式调用dll中的函数,示例如下:

 
 

static inline FSTmobileModule& Get()
{
return FModuleManager::LoadModuleChecked<FSTmobileModule>("STmobile");
}


static
inline int exampleFunc(int a,int b) { #if PLATFORM_WINDOWS if (FSTmobileModule::Get().ST_mobileLibraryHandle) { typedef int(*_exampleFunc)(int a ,int b); _exampleFunc ExampleFunc; FString procName = "dllFuncName"; ExampleFunc = (_exampleFunc)FPlatformProcess::GetDllExport(FSTmobileModule::Get().ST_mobileLibraryHandle, *procName); if (ExampleFunc) { return ExampleFunc(a,b); } } #elif PLATFORM_ANDROID return dllFuncName(model_path, config, &handle); #endif }

调用.so中的函数,直接#include对应的头文件,然后调用同名函数即可。

如果想在project中调用插件中的函数,在project.build.cs中将YOURplugin包含进来,然后通过

FSTmobileModule::Get().exampleFunc(int a,int b)直接调用即可。记得#include "STmobile.h"哦

猜你喜欢

转载自www.cnblogs.com/chenc-c/p/10877320.html
今日推荐