Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

[Solved]How can I make one variable affect others in custom inspector?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Le_Tai, Aug 20, 2016.

  1. Le_Tai

    Le_Tai

    Joined:
    Jun 20, 2014
    Posts:
    429
    For example, I have a custom inspector like this


    Code (CSharp):
    1. public override void OnInspectorGUI()
    2.     {
    3.          a= EditorGUILayout.FloatField("A", a);
    4.          lock =EditorGUILayout.Toggle("Lock",lock);
    5.          b= EditorGUILayout.FloatField("B", b);
    6.      }
    How do I make adjusting `a` also change `b` and vice versa when `lock` is true?
     
  2. Le_Tai

    Le_Tai

    Joined:
    Jun 20, 2014
    Posts:
    429
  3. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    What do you mean?

    You mean something like this:
    Code (csharp):
    1.  
    2. ..... : Editor
    3. {
    4.     private float a = 0f;
    5.     private float b = 1f;
    6.     private bool isLocked = false;
    7.  
    8.     public override void OnInspectorGUI()
    9.     {
    10.         a = EditorGUILayout.FloatField( "A", a );
    11.  
    12.         //EditorGUI.BeginChangeCheck();
    13.         bool isLocked_old = isLocked;
    14.         isLocked = EditorGUILayout.Toggle( "Lock", isLocked );
    15.         //if( EditorGUI.EndChangeCheck() )
    16.         //    Debug.Log( "User Toggled Lock: " + (isLocked ? "On" : "Off") );
    17.  
    18.         b = EditorGUILayout.FloatField( "B", b );
    19.  
    20.         if( isLocked != isLocked_old )
    21.             Debug.Log( "Alter `a` And `b` now!" );
    22.     }
    23.    
    24. }
    25.