【发布时间】:2016-11-23 13:16:15
【问题描述】:
我用 C++ 编写了一个国际象棋库,并为 Windows(32 位和 64 位)和 Android(x86 和 armebi-v7)编译了它。
我的 Windows 构建完全正确,但是当我为 Android 构建它并运行游戏时,我的adb logcat -s Unity 收到以下异常:
11-23 16:39:37.278 22335 22352 I Unity : DllNotFoundException: MatinChess
11-23 16:39:37.278 22335 22352 I Unity : at (wrapper managed-to-native) MatinChess.Net.ExternMethods:Initialize ()
11-23 16:39:37.278 22335 22352 I Unity : at MatinChess.Net.MatinChess..ctor () [0x00000] in <filename unknown>:0
11-23 16:39:37.278 22335 22352 I Unity : at Model.Awake () [0x00000] in <filename unknown>:0
当我解压缩我的 apk 文件时,我有:
lib
|_ armeabi-v7a
| |_ libmain.so
| |_ libMatinChess.so
| |_ libmono.so
| |_ libunity.so
|_ x86
|_ libmain.so
|_ libMatinChess.so
|_ libmono.so
|_ libunity.so
这是我的设置和脚本:
基于here 和here,我在我的Assets 中创建了Plugins 文件夹,并像这样放置我的库:
Plugins
|_ Android
| |_ libs
| |_ armeabi-v7a
| | |_ libMatinChess.so
| |_ x86
| |_ libMatinChess.so
|_ x64
| |_ MatinChess.dll
|_ x86
|_ MatinChess.dll
我确定我的 Inspector 中的 Platform Settings 配置正确。
我在我的脚本中使用了我的库并基于here 和here,而不是在Android 上使用libMatinChess.so,我在开始时没有使用lib,最后没有使用.so。所以是这样的:
class ExternMethods
{
#if UNITY_ANDROID
const string dll = "MatinChess";
#else
const string dll = "MatinChess.dll";
#endif
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public extern static PlayState CheckState();
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public extern static void Initialize();
//
// other extern methods
//
}
因此,当我为 Windows 构建 Unity 游戏时,它可以正常工作。为了让它在编辑器中工作,我关注了this link,并编写了以下脚本:
public class Model : MonoBehaviour
{
void Awake()
{
#if UNITY_EDITOR_32
var dllPath = Application.dataPath + Path.DirectorySeparatorChar + "Plugins" + Path.DirectorySeparatorChar + "x32";
#elif UNITY_EDITOR_64
var dllPath = Application.dataPath + Path.DirectorySeparatorChar + "Plugins" + Path.DirectorySeparatorChar + "x64";
#else // Player
var dllPath = Application.dataPath + Path.DirectorySeparatorChar + "Plugins";
#endif
var currentPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process);
if (currentPath != null && currentPath.Contains(dllPath) == false)
Environment.SetEnvironmentVariable("PATH", currentPath + Path.PathSeparator + dllPath, EnvironmentVariableTarget.Process);
}
}
所以它也适用于 Unity 编辑器。
【问题讨论】:
-
为什么要为 Android 配置 .dll 文件,因为您已经拥有 .so 文件?
-
@PravinD 我想说我的使用 .dll 配置的 Windows 构建工作正常,但具有 .so 配置的 Android 却不行。并告知您我的配置差异。
-
您使用的是哪个版本的 Unity3D?
-
@zwcloud 版本为 5.4.1f1 个人版
标签: android c++ unity3d mono native