Search Unity

Is there a way to dynamically change popup Displayed Options?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Timbon77, Oct 30, 2020.

  1. Timbon77

    Timbon77

    Joined:
    Oct 3, 2019
    Posts:
    38
    Hi,

    I am trying to create 2 popups that one of them is able to control the the displayed options of the other.
    The first popup options is fixed and have the same vector, but the second one changes the vector based on the first popup selection.
    Code (CSharp):
    1.     public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
    2.     {
    3.           string[] Options1 = {"option1","option2,"option3"};
    4.          string[] Options2 = {"optionA","optionB,"optionC"};
    5.  
    6.           EditorGUI.Popup(rect1," popup2:",index,Option1);
    7.           EditorGUI.Popup(rect2," popup1:",index,{"123","ABC"});
    8.  
    9.         if (EditorGUI.EndChangeCheck()) {
    10.             Debug.Log("Switch from Option1 to Option2");
    11.         }
    12.    }
    13.  
    Second best thing would be to make the first popup disapear whenever we select somthing from the other popup.
     
  2. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,330
    So one PropertyDrawer containing two popups with one popup altering the contents of the second popup?

    One way to achieve this would be to have a string[] type member field and assigning a different value to it whenever the selected index for the first popup gets changed.
    Code (CSharp):
    1. using System;
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. public class DoublePopupPropertyDrawer : PropertyDrawer
    6. {
    7.     static readonly string[] options1 = {"A","B","C"};
    8.     static readonly string[] options2A = { "A1", "A2", "A3" };
    9.     static readonly string[] options2B = { "B1", "B2", "B3" };
    10.     static readonly string[] options2C = { "C1", "C2", "C3" };
    11.  
    12.     string[] options2 = null;
    13.     int firstSelectedIndex = 0;
    14.     int secondSelectedIndex = 0;
    15.  
    16.     void OnEnable()
    17.     {
    18.         UpdateSecondPopupOptions();
    19.     }
    20.  
    21.     public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
    22.     {
    23.         var rect1 = rect;
    24.         rect1.width *= 0.5f;
    25.         var rect2 = rect1;
    26.         rect2.x += rect1.width;
    27.  
    28.         EditorGUI.BeginChangeCheck();
    29.      
    30.         firstSelectedIndex = EditorGUI.Popup(rect1, "Popup 1", firstSelectedIndex, options1);
    31.  
    32.         if(EditorGUI.EndChangeCheck())
    33.         {
    34.             UpdateSecondPopupOptions();
    35.         }
    36.      
    37.         secondSelectedIndex = EditorGUI.Popup(rect2, "Popup 2", secondSelectedIndex, options2);
    38.     }
    39.  
    40.     void UpdateSecondPopupOptions()
    41.     {
    42.         switch(firstSelectedIndex)
    43.         {
    44.             case 0:
    45.                 if(options2 != options2A)
    46.                 {
    47.                     options2 = options2A;
    48.                     secondSelectedIndex = 0;
    49.                 }
    50.                 return;
    51.             case 1:
    52.                 if(options2 != options2B)
    53.                 {
    54.                     options2 = options2B;
    55.                     secondSelectedIndex = 0;
    56.                 }
    57.                 return;
    58.             case 2:
    59.                 if(options2 != options2C)
    60.                 {
    61.                     options2 = options2C;
    62.                     secondSelectedIndex = 0;
    63.                 }
    64.                 return;
    65.             default:
    66.                 throw new IndexOutOfRangeException(firstSelectedIndex.ToString());
    67.         }
    68.     }
    69. }
     
  3. Timbon77

    Timbon77

    Joined:
    Oct 3, 2019
    Posts:
    38
    Sir you are a lifesaver! thanks!
     
    SisusCo likes this.