1
\$\begingroup\$

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?
\$\endgroup\$
8
  • \$\begingroup\$ 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. \$\endgroup\$
    – House
    Feb 23, 2016 at 17:10
  • \$\begingroup\$ @Byte56 well I've already gotten that far, did you see my code? \$\endgroup\$ Feb 23, 2016 at 17:17
  • \$\begingroup\$ Ah, I totally skimmed over that line. So are you looking for something like this: gamedev.stackexchange.com/questions/83027/… ? \$\endgroup\$
    – House
    Feb 23, 2016 at 17:18
  • \$\begingroup\$ @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. \$\endgroup\$ Feb 23, 2016 at 17:43
  • \$\begingroup\$ You can create a delegate from methodinfo: msdn.microsoft.com/en-us/library/53cz7sc6(v=vs.110).aspx \$\endgroup\$
    – House
    Feb 23, 2016 at 17:45

0

You must log in to answer this question.

Browse other questions tagged .