3
\$\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\$
  • 2
    \$\begingroup\$ Have you read the docs on PropertyDrawers? \$\endgroup\$ – DMGregory Dec 15 '15 at 15:31
  • \$\begingroup\$ I can close this or you can post as an answer and I'll mark it. \$\endgroup\$ – mt_ Dec 15 '15 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 Dec 15 '15 at 15:42
2
\$\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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.