- Home /
How can I make a custom inspector for this object in c#
I want to know more about Custom Inspector scripting for C#, the documentation is very limited (javascript only).
One of my scripts, which queues up animations to be played (so that animations can be instigated by a trigger placed by the level designer) has currently 4 collections as public variables
public string[] animationNames = new string[1];
public WrapMode[] animationWrapModes;
public QueueMode[] animationQueueModes;
public PlayMode[] animationPlayModes;
What I want to do is to make this into one array with one object in it so that the level designer doesn't have to go through each collection and type in how many he wants. (I hope that makes sense!);
What I want to do is to make 4 collections into just one, since they are dependent on each other in code. This is what I have now:
This is what I wanna make it into:
The Unity documentation tells me that I need to do something like this to make an inspector for the LookAtPoint class (Just to learn how to do this I followed the example):
@CustomEditor (LookAtPoint)
class LookAtPointEditor extends Editor {
function OnInspectorGUI () {
target.lookAtPoint = EditorGUILayout.Vector3Field ("Look At Point", target.lookAtPoint);
if (GUI.changed)
EditorUtility.SetDirty (target);
}
}
I do not understand where the target can find the reference to the lookAtPoint in this code-snippet.
using UnityEngine; using UnityEditor;
[CustomEditor(typeof(LookAtPoint))] public class LookAtPointEditor : Editor {
void OnInspectorGUI() { target.lookAtPoint = EditorGUILayout.Vector3Field("Look At Point", target.lookAtPoint); if (GUI.changed) EditorUtility.SetDirty(target); } }
Can someone please let me know how I can achieve my goal with the 4 collections and perhaps also direct me to some more information on making custom inspectors in c# ?
Answer by Duke · Feb 17, 2010 at 03:56 PM
What you're trying to do shouldn't require a custom inspector.
You need to define a new class that holds all of your values. This class should be marked [Serializable] so that it can show up in the inspector and the editor can save/load it:
[Serializable]
public class MyAnimation
{
public string animationName;
public WrapMode animationWrapMode;
public QueueMode animationQueueMode;
public PlayMode animationPlayMode;
}
Then, you should just be able to replace your four arrays with a single array containing instances of your new class:
public MyAnimation[] animations;
That should do it.
I was hoping it would be that simple, thank you.
But I would still love to learn more about how to make custom inspectors (with my own gui).
using System; using UnityEngine;
[Serializable] public class AnimationHolder { public string animationName; public Wrap$$anonymous$$ode animationWrap$$anonymous$$ode; public Queue$$anonymous$$ode animationQueue$$anonymous$$ode; public Play$$anonymous$$ode animationPlay$$anonymous$$ode; }
thanks a lot for the straightforward explanation and the example