Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Grey out variables in inspector

Discussion in 'Scripting' started by Saturnix, Nov 13, 2012.

  1. Saturnix

    Saturnix

    Joined:
    Nov 2, 2012
    Posts:
    13
    Hello everyone! This is my first post in here, I hope to be in the right section.

    I have a script like this:

    Code (csharp):
    1.  
    2. var advanced : boolean = false;
    3.  
    4. var spring : float = 40;
    5. var damper : float = 20;
    6. var maxDistance : float = 0.001;
    7. var minDistance : float = 0.001;
    8. var anchor : Vector3 = Vector3(0, 0.5, 0);
    9.  
    All of those variables shows up in the inspector as I select the game object with the script attached. So far so good!

    What I want to do, by the way, is to grey out all the variables and make them editable just if the user check the "advanced" box (the first variable). If "advanced" is false, and by default it is, the user should not see the variables or see them grey, not editable...

    Compared to the rest of the work I've done so far (thanks to the amazing support from the Unity3d community and developers) this looks really easy to do. However, reading the documentation looks like I can only do this by declaring my own inspector class. Isn't there any faster and easier way?

    Thanks in advance!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You'd have to make a custom inspector to do that.

    --Eric
     
  3. Saturnix

    Saturnix

    Joined:
    Nov 2, 2012
    Posts:
    13
    ... and how would I do that? I'm trying since 2 hours, but the only result I got is to modify the Unity3d menu bar (!).
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  5. Saturnix

    Saturnix

    Joined:
    Nov 2, 2012
    Posts:
    13
    that's exactly where I started, but I'm unable to replicate the examples.

    I've tried to copy-paste the example


    Code (csharp):
    1. @CustomEditor (LookAtPoint)
    2. class LookAtPointEditor extends Editor {
    3.     function OnInspectorGUI () {
    4.         target.lookAtPoint = EditorGUILayout.Vector3Field ("Look At Point", target.lookAtPoint);
    5.         if (GUI.changed)
    6.             EditorUtility.SetDirty (target);
    7.     }
    8. }
    but LookAtPoint is an "Unknown identifier".

    Should I write separate scripts files for editor and variables? Isn't there any way to keep the editor declarations and the variables in the same script?

    Thanks!
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You need to have a script called LookAtPoint; follow all the steps starting under the "Custom Inspectors" heading.

    --Eric