- Home /
Dynamic Target for Object field (and others) in Custom Inspector
Hey there,
I am trying to implement a functionality framework for the UI in unity. I am writing a custom inspector for it because the event system of unity can only handle methods with maxmimum of one parameter. The solution I thought of is using a custom inspector to choose the functionality and change the UI dynamically.
The monobehaviour (Not the actual code just snippets and with names changed!):
private static readonly List<string> methods = new List<string>();
/// <summary>
/// Selector for the method that should be fired.
/// </summary>
private int targetmethod;
public int Targetmethod {get {return targetmethod;} set {targetmethod = value;}}
public static int Methodcount {get {return methods.Count;}}
/// <summary>
/// Initialization of static elements.
/// </summary>
static StaticConstructor()
{
methods.Add("StartScene");
methods.Add("ChangeTextField");
methods.Add("SaveGame");
methods.Add("LoadGame");
methods.Add("Quit Game");
methods.Add("Set Sound volume");
methods.Add("Set Window Mode");
methods.Add("Change Resolution");
methods.Add("Method8");
}
/// <summary>
/// Constructor
/// </summary>
public Constructor()
{
Object1 = new object();
Object2 = new object();
Object3 = new object();
Object4 = new object();
Object5 = new object();
}
/// <summary>
/// Get string Array of all available methods.
/// </summary>
public string[] Methods
{
get
{
return methods.ToArray();
}
}
/// <summary>
/// placeholder objects to be manipulated.
/// </summary>
public object Object1, Object2, Object3, Object4, Object5;
/// <summary>
/// Activate selected function.
/// </summary>
public void FireFunction()
{
switch (targetmethod)
{
case 0:
try
{
StartScene(Object1 as string);
Debug.Log("Called" + methods[targetmethod] + " on " + this.name);
}
catch (Exception e)
{
Debug.LogError(e.Message);
throw;
}
break;
case 1:
....
}
private void StartScene(string sceneName)
{
try
{ SceneManager.LoadScene(sceneName); }
catch(Exception e)
{
Debug.Log(e.Message);
if (sceneName == null) throw new ArgumentNullException(sceneName,"Scene Name is not given.");
}
}
I am using C# type: object as targets because I can't anticipate the way this framework would be used. Using type specific objects would make a huge mess of public objects in the monobehaviour (2-5 objects per type of UI element)
The Custom Inspector (Not the actual code just snippets and with names changed!):
public int Index = 0;
private ClassName classnametargetvariable;
private EditorGUILayout layout;
private GUILayoutOption[] options;
public ClassNameEditor(GUILayoutOption[] options, EditorGUILayout layout)
{
this.options = options;
this.layout = layout;
}
/// <summary>
/// <para>
/// Implement this function to make a custom inspector.
/// </para>
/// </summary>
public override void OnInspectorGUI()
{
//base.OnInspectorGUI();
classnametargetvariable = (class) target;
//options = new GUILayoutOption[1];
//options[0] = new GUILayoutOption(GUILayoutOption.Type.stretchWidth, true);
//string[] bStrings = classnametargetvariable .Methods;
Index = EditorGUILayout.Popup(Index, ClassNametargetvariable.Methods);
classnametargetvariable .Targetmethod = Index;
switch (Index)
{
case 0:
StartSceneUi();
break;
case 1:
....
}
private void ChangeTextUi()
{
ClassNametargetvariable.Object1 = EditorGUILayout.ObjectField("Text Object Old", classnametargetvariable .Object1 as Text, typeof(Text), true, options);
classnametargetvariable .Object2 = EditorGUILayout.ObjectField("Text Object New", classnametargetvariable .Object2 as Text, typeof(Text), true, options);
}
}
While the dynamic aspect of the custom inspector works the functionality does not. I get nullreference exceptions. Has anyone an Idea how to solve this ?
Your answer
Follow this Question
Related Questions
Modifying default Inspector GUI creator 3 Answers
Distribute terrain in zones 3 Answers
InspectorEditor: Find target object that has focus 1 Answer
Problem using EditorGUILayout.ObjectField with custom type 1 Answer
Multiple Cars not working 1 Answer