- Home /
How do you call a static function in C# from Android?
We used to use instance methods in C#, but changed them to static methods. This is fine for the iOS guys who are using mono methods to call back to the native code, but I don't know how to accomplish this in Android. Before we were using UnitySendMessage, but this needs an object to call it on. How do I call a static class method in C# from Android with the least effort. I'll provide an example of what I mean. Thank you!
C#:
public class Example : MonoBehaviour
{
public static void DoStuff(string text)
{
// Do stuff with text
}
}
Java:
public class Test
{
// Call C# static method
// Used to use UnityPlayer.UnitySendMessage when method was instance
}
What's wrong with the usual way, Example.DoStuff();
?
I want to call Example.DoStuff(), but that is in C# code and I don't know how to access that from Java.
Answer by Owen-Reynolds · Apr 03, 2014 at 09:59 PM
I feel like I'm missing something (a Java plugin?) But, you say you can use UnitySendMessage to run functions on scripts on gameObjects. So, could launder calls to statics through drivers on dummy(empty) gameObjects:
public class CallStaticsForPlugIn : Monobehave {
public void doStuff(string text) { Example.doStuff(text); }
}
If all the statics are in MonoBehave classes (like the example above,) could just put that driver next to the static (maybe call it "runDoStuff".)
Thank you. That was one of our thoughts, but we have so many functions in so many places to call, that this would get very large. So was hoping there was an alternative.
Sounds like the real solution is letting the code-monkeys know they aren't in charge, and need to run big changes past everyone it will affect. Seriously, some of the worst student projects I saw were when the only talented programmer was also the lead, and went down a rabbit hole of "fun to write" code.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Acces an Awake variable 2 Answers
Multiple Cars not working 1 Answer
an object reference is required for the non static field error 1 Answer
instantiate random prefabs based on player camera distance 1 Answer