Search Unity

Question How pass data from EditorWindow to PropertyDrawer

Discussion in 'Immediate Mode GUI (IMGUI)' started by HamsterDontWork, Jul 21, 2020.

  1. HamsterDontWork

    HamsterDontWork

    Joined:
    May 4, 2019
    Posts:
    1
    Just like when you click a Object field On Inspector , it will pop up a window to let you choose a object

    I want to make a text field with a button ,when I click button ,it pop up a window for choosing string for that text field,like that:
    upload_2020-7-21_23-58-3.png

    Code (CSharp):
    1.  
    2. public class StringContainer : MonoBehaviour
    3. {
    4.     public SealedString Text;
    5. }
    6.  
    7. [System.Serializable]
    8. public struct SealedString
    9. {
    10.     public string Key;
    11. }
    12.  
    13. [CustomPropertyDrawer(typeof(SealedString))]
    14. public class SealedStringDrawer : PropertyDrawer
    15. {
    16.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    17.     {
    18.         EditorGUI.BeginProperty(position, label, property);
    19.  
    20.         SerializedProperty key = property.FindPropertyRelative("Key");
    21.  
    22.         position = EditorGUI.PrefixLabel(position, new GUIContent(key.stringValue), GUI.skin.textField);
    23.  
    24.         if (GUI.Button(position, "Open Select Window"))
    25.         {
    26.             SelectWindow.Open();
    27.         }
    28.  
    29.         EditorGUI.EndProperty();
    30.     }
    31. }
    32.  
    33.  
    34.  
    35.  
    36. public class SelectWindow : EditorWindow
    37. {
    38.     public static void Open()
    39.     {
    40.         var window = GetWindow<SelectWindow>();
    41.         window.Show();
    42.     }
    43.  
    44.     private void OnGUI()
    45.     {
    46.         string[] strings = { "1", "2", "3", "4", "5" };
    47.  
    48.         EditorGUILayout.BeginVertical();
    49.  
    50.         foreach (var s in strings)
    51.         {
    52.             if (GUILayout.Button(s))
    53.             {
    54.                 //set the SealedString.Key = s,[B]How to do ?[/B]
    55.             }
    56.         }
    57.  
    58.         EditorGUILayout.EndVertical();
    59.     }
    60. }
    61. [FONT=inherit]
    [/FONT]

    Note: PropertyDrawer is necessary,because I may use a complex data rather than string
     
    KyryloKuzyk likes this.