- Home /
how to display variable of function in inspector
hi have a script with 2 function like this :
public class first: MonoBehaviour {
static public void one(){
int numb = 10;
// something
}
static public void two(){
int helth = 20;
// something
}
}
.. and i have another script like this : .. public class secound: MonoBehaviour {
Update(){
//first.one(); // normal use .. but...
}
}
.... i want to set name of function in inspector like "tow" or "one" then that function execute in Update() in secound class ... and i want after i set the name of function... the variables inside that function showing in inspector .. so.. how do i do this ?
Answer by fafase · Jun 25, 2014 at 02:51 PM
The way to do this would be to have a Dictionary of string and method/delegate. Below is an example you need to make your own, but the idea is there.
public class First:MonoBehaviour{
public string method; // Set in inspector asthe name of the method
Dictionary<string, Action> dict = new Dictionary<string, Action>();
void Start(){
dict.Add("one",new Action(One);
dict.Add("two",new Action(Two);
if(dict.Contains(method)) // Make sure the string is in the dictionary
dict[method](); // call the method
}
}
Best would be to use a enum with the same principle:
public enum MethodName{
One, Two
}
public class First:MonoBehaviour{
public MethodName method; // Set in inspector as the name of the method
Dictionary<MethodName, Action> dict = new Dictionary<MethodName, Action>();
void Start(){
dict.Add(MethodName.One,new Action(One);
dict.Add(MethodName.Two,new Action(Two);
dict[method](); // call the method
}
}
This way, no chance that the string is wrongly written.
thanks for the answer .. but this two codes not working... how do i use it ? .... and i want when i set name of function the variables of that function showing in inspector .. i must use classes or functions for that ? ... i want do this image is a inspector:
Then you also need an editor script that based on the choice of the enum, it shows the corresponding variable. There are examples on the internet http://answers.unity3d.com/questions/417837/change-inspector-variables-depending-on-enum.html
Dictionary is a good idea but i want to get parameters of a function in another script then i can show in inspector with a editor script... in below example how can i get name of settext function in another script and then get parameters of that function ? ... i want to recognize functions and parameters of each function in another script... how do i do this ?
using UnityEngine;
using System.Collections;
public class first: $$anonymous$$onoBehaviour
{
public void settext(string astring)
{
guiText.text = astring;
}
}