Call C# static method #23
-
Can i somehow call static C# method from python script?
I get errors when try to use this code in python: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Yes, it's possible. I'm guessing you didn't add the assembly C Sharp: var engine = UnityPython.CreateEngine();
var scope = engine.CreateScope();
var source = engine.CreateScriptSourceFromString(text.text);
source.Engine.Runtime.LoadAssembly(
AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(t => t.GetTypes())
.Where(t => t.Namespace == "Foobar")
.Select(t => t.Assembly)
.First()
);
source.Execute(scope);
...
namespace Foobar
{
public static class TestStatic
{
public static void TestStaticMethod()
{
Debug.Log(">> Test Static");
}
}
} Python: import Foobar
Foobar.TestStatic.TestStaticMethod() There's probably a way to add TestStatic without needing to put it in a namespace, and maybe there's an easier way to do all of this, but this is at least a good starting point if that's what your problem is. Otherwise, you will need to post what kind of errors you are getting, a small and self-contained example that causes the error, and a stacktrace. On another note, this repository could use an update to make loading assemblies or classes into the IronPython runtime easier if IronPython doesn't expose an easy way to do that already, but that's something I probably won't get around to due to the relative priority of this repository compared to other things I'm doing. I do welcome merge requests though, if anyone would like to put the work in to do that. |
Beta Was this translation helpful? Give feedback.
-
It worked, thank you for help!) |
Beta Was this translation helpful? Give feedback.
Yes, it's possible. I'm guessing you didn't add the assembly
TestStatic
is in to the IronPython runtime. For example:C Sharp:
Python:
There's probab…