UnityPython 在 Editor 上工作,但不在 Build 上工作
UnityPython working on Editor but not on Build
我正在制作一款游戏,其中玩家执行 Python 代码以移动角色。我这样做的方法是使用 UnityPython,它基本上只是集成在 Unity 中的 IronPython。其工作方式是将玩家输入的代码包装在 Python 脚本中。然后这个脚本在另一个线程上 运行(这是从包装器工作,使用 Python 的 threading
class),它与 Unity 通信以移动玩家。该系统在编辑器上运行良好,但在构建上运行不佳。
首先,我确定问题出在脚本引擎使用的搜索路径没有找到正确的库文件夹(因为 UnityPython 本身在资产文件夹中)。我需要的特定 class 是 threading
和 Queue
.
引擎初始化程序如下所示:
var engine = UnityPython.CreateEngine();
var paths = engine.GetSearchPaths();
paths.Add(Application.dataPath + "/Python/Lib");
engine.SetSearchPaths(paths);
我想到的一个解决方法是将 Python 文件夹复制到 StreamingAssets 文件夹,然后在第三行使用 Application.streamingAssetsPath
代替。但是开发构建调试器然后给了我这个奇怪的错误:
System.TypeInitializationException: The type initializer for
'CodecsInfo' threw an exception. ---> System.NotSupportedException:
Encoding 37 data could not be found. Make sure you have correct
international codeset assembly installed and enabled.
这就是我现在被困的地方。关于此错误的含义以及如何解决它有什么想法吗?
编辑:问题似乎是在引擎使用脚本时发生的。这里的代码是这样的:
try
{
var source = engine.CreateScriptSourceFromFile(scriptPath);
source.Compile();
// script here is a dynamic variable
script = engine.Runtime.UseFile(scriptPath); // this is where the error is thrown
script.GameManager = this;
script.start();
}
catch (Exception e)
{
Debug.Log("Error: " + e);
}
问题似乎是由于缺少的 DLL 没有从编辑器复制到构建中造成的。感谢 exodrifter 的解决方案(找到 here)。
简单的说,项目的Plugins文件夹下需要这些DLL。
- I18N.CJK.dll
- I18N.dll
- I18N.MidEast.dll
- I18N.Other.dll
- I18N.Rare.dll
- I18N.West.dll
它们可以从 Unity Editor 的安装位置复制过来。 DLL 位于 Unity\Editor\Data\Mono\lib\mono.0
。确切位置取决于您安装 Unity 的位置。就我而言,它位于 C:\Program Files\Unity\Hub\Editor18.4.7f1\Editor\Data\Mono\lib\mono.0
我正在制作一款游戏,其中玩家执行 Python 代码以移动角色。我这样做的方法是使用 UnityPython,它基本上只是集成在 Unity 中的 IronPython。其工作方式是将玩家输入的代码包装在 Python 脚本中。然后这个脚本在另一个线程上 运行(这是从包装器工作,使用 Python 的 threading
class),它与 Unity 通信以移动玩家。该系统在编辑器上运行良好,但在构建上运行不佳。
首先,我确定问题出在脚本引擎使用的搜索路径没有找到正确的库文件夹(因为 UnityPython 本身在资产文件夹中)。我需要的特定 class 是 threading
和 Queue
.
引擎初始化程序如下所示:
var engine = UnityPython.CreateEngine();
var paths = engine.GetSearchPaths();
paths.Add(Application.dataPath + "/Python/Lib");
engine.SetSearchPaths(paths);
我想到的一个解决方法是将 Python 文件夹复制到 StreamingAssets 文件夹,然后在第三行使用 Application.streamingAssetsPath
代替。但是开发构建调试器然后给了我这个奇怪的错误:
System.TypeInitializationException: The type initializer for 'CodecsInfo' threw an exception. ---> System.NotSupportedException: Encoding 37 data could not be found. Make sure you have correct international codeset assembly installed and enabled.
这就是我现在被困的地方。关于此错误的含义以及如何解决它有什么想法吗?
编辑:问题似乎是在引擎使用脚本时发生的。这里的代码是这样的:
try
{
var source = engine.CreateScriptSourceFromFile(scriptPath);
source.Compile();
// script here is a dynamic variable
script = engine.Runtime.UseFile(scriptPath); // this is where the error is thrown
script.GameManager = this;
script.start();
}
catch (Exception e)
{
Debug.Log("Error: " + e);
}
问题似乎是由于缺少的 DLL 没有从编辑器复制到构建中造成的。感谢 exodrifter 的解决方案(找到 here)。
简单的说,项目的Plugins文件夹下需要这些DLL。
- I18N.CJK.dll
- I18N.dll
- I18N.MidEast.dll
- I18N.Other.dll
- I18N.Rare.dll
- I18N.West.dll
它们可以从 Unity Editor 的安装位置复制过来。 DLL 位于 Unity\Editor\Data\Mono\lib\mono.0
。确切位置取决于您安装 Unity 的位置。就我而言,它位于 C:\Program Files\Unity\Hub\Editor18.4.7f1\Editor\Data\Mono\lib\mono.0