IronPython in Unity3D

前端 未结 1 830
一整个雨季
一整个雨季 2021-02-01 18:10

I am trying to use IronPython as an external scripting language for Unity3D. The necessary DLLs for IronPython\'s execution load just fine inside of Assets\\Plugins. However, wh

相关标签:
1条回答
  • 2021-02-01 18:30

    So from a Unity script:

    PythonEngine engine = new PythonEngine();
    engine.LoadAssembly(Assembly.GetAssembly(typeof(GameObject)));
    engine.ExecuteFile("apple.py");
    

    And inside of a python script (mine was apple.py located in the same folder as the game.exe):

    import UnityEngine
    from UnityEngine import *
    
    Debug.Log("Hello From IronPython!")
    

    Edit #1

    I should note that the reason I was getting an error before was because the runtime version was specified as a 4.0 instead of a 3.5 or lower.

    Edit #2

    If you need to access your scripts from IronPython, then you can load your assembly as well by doing:

    engine.LoadAssembly(Assembly.GetAssembly(typeof(MyPlayerScriptOrSomething)));
    

    Then to use it in script:

    import MyPlayerScriptOrSomething
    

    Note that you do not have to do the LoadAssembly for every script, just once to get the assembly.

    Edit #3

    The IronPython DLLs should be placed in a "Plugins" folder somewhere under "Assets" (most simply: Assets->Plugins. Here is my setup:

    > Assets
    > > Plugins
    > > > IronMath.dll
    > > > IronPython.dll
    > > > Microsoft.Scripting.dll
    > > > Microsoft.Scripting.Core.dll
    

    Edit #4

    Your scripts can be put anywhere your program has access to them. For instance, if you wanted to place "apple.py" directly under "C:\", you could execute that file by doing:

    engine.ExecuteFile(@"c:\apple.py");
    

    Edit #5

    The version I am using is:

    enter image description here

    0 讨论(0)
提交回复
热议问题