4
\$\begingroup\$

I have a script attached to a game object. The script has many public fields and I'm satisfied with how Unity displays most of the fields for editing in the Editor. Except for a few. I'd like to write a complex custom editor script for those fields and leave the rest as is - public and visible in the Editor. I don't want to write lots of extra code to mimic what the editor does so far.

The perfect scenario would be to be able to define a custom editor script for a certain type (the type is not derived from MonoBehavior and cannot be). If a field of that type is public in any script attached to a GO I'd like the Editor to display it the way I've defined.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Have you read the docs on PropertyDrawers? \$\endgroup\$
    – DMGregory
    Commented Dec 15, 2015 at 15:31
  • \$\begingroup\$ I can close this or you can post as an answer and I'll mark it. \$\endgroup\$
    – mt_
    Commented Dec 15, 2015 at 15:38
  • \$\begingroup\$ It would be better if you write up the solution that worked for you as an answer, for others to use as reference. It's completely OK to answer your own questions here. I don't have a concrete example of using PropertyDrawer to demonstrate, so you'd likely be able to write a more complete and detailed answer than I would just cribbing from the docs. ;) \$\endgroup\$
    – DMGregory
    Commented Dec 15, 2015 at 15:42

1 Answer 1

5
\$\begingroup\$

In your MonoBehavior script, you can use [HideInInspector] to hide your variable.

public class GameObjectScript: MonoBehaviour
{
    public int randomInt;

    [HideInInspector]
    public string name;
}

You can edit your Inspector view by creating an editor script for GameObjectScript that inherits from Editor.

[CustomEditor(typeof(GameObjectScript))]
public class GameObjectScriptEditor: Editor
{

    GameObjectScript myScript;

    public override void OnInspectorGUI()
    {
        DrawDefaultInspector(); //Drawing the default inspector except the one you hide.
                               //Which is only int randomInt

        myScript.name = EditorGUILayout.TextField("My Name: ", myScript.name); //Draw whatever you want in the inspector
    }
}

Please note to place the GameObjectScriptEditor into "Asset/Script/Editor". You can read more in the Unity documentation.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .