Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm making a game that I'd like to be data driven. I've got a designer that can make levels using unity and saving them as a unity scene. What we're doing is exporting those levels as XML and loading them from a website into a single scene. This allows for rapid prototyping on devices; the designer does not need to wait for me to push builds out or push his own out.

Many of the scripts on the prefabs we're loading use UnityEvents, so I can allow my designer to trigger anything he wants in a given script. It gives him more freedom to design and stops my scripts from being too specific.

Currently, I'm reducing these UnityEvents down to the object they are referencing and the function they are calling and storing them as string attributes in the XML. However, I'm unsure how to convert from the string function name into an actual function pointer for the UnityEvent, nor do I know how to set up a UnityEvent the way the inspector allows me to. See here you can attach an object and select a function to trigger, then simply call Invoke() on the UnityEvent, but the only documentation I can find on UnityEvent scripting only allows you to add listeners with methods within the script.

enter image description here

Here I am getting the Type from a string and the MethodInfo from a string, but am unsure of how to set up the UnityEvent with this information

Type type = Type.GetType("ScriptName");
MethodInfo theMethod = type.GetMethod("FunctionName");
UnityEvent myEvent = new UnityEvent();
myEvent.AddListener(); //Reference to Method goes here 
//reference to the script goes where?
share|improve this question
    
I'm a bit too busy to create a full answer right now, but your solution is going to be in Reflection or in the GetMethod method of the Type class. – Byte56 Feb 23 '16 at 17:10
    
@Byte56 well I've already gotten that far, did you see my code? – Premier Bromanov Feb 23 '16 at 17:17
    
Ah, I totally skimmed over that line. So are you looking for something like this: gamedev.stackexchange.com/questions/83027/… ? – Byte56 Feb 23 '16 at 17:18
    
@Byte56 I'm not 100% sure. I don't know if that solves my issue because I'm not sure if MethodInfo will work with delegates, and I'm also not sure if using delegates allows my designer to easily hook up game objects to scripts to invoke certain methods when things are triggered. – Premier Bromanov Feb 23 '16 at 17:43
    
You can create a delegate from methodinfo: msdn.microsoft.com/en-us/library/53cz7sc6(v=vs.110).aspx – Byte56 Feb 23 '16 at 17:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.