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

Question Proper Sprite Preview through Serilialized Properties

Discussion in 'Immediate Mode GUI (IMGUI)' started by CladInRed, Jun 19, 2022.

  1. CladInRed

    CladInRed

    Joined:
    Mar 3, 2020
    Posts:
    2
    Hello! I want to get a nice-looking object picker, like the one here through custom GUI


    However, this one is hard coded and sadly doesn't even change the image when selected!
    Here's the code for such results:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. [CustomEditor(typeof(SpeakerSO))]
    5. public class SpeakerEditor : Editor
    6. {
    7.     SpeakerSO speaker;
    8.     SerializedProperty nameProp;
    9.  
    10.     Sprite neutralPProp, happyPProp, sadPProp, angryPProp;
    11.  
    12.     private void OnEnable()
    13.     {
    14.         speaker = (SpeakerSO) target;
    15.         nameProp = serializedObject.FindProperty("characterName");
    16.         neutralPProp = speaker.neutralPortrait;
    17.  
    18.     }
    19.  
    20.     public override void OnInspectorGUI()
    21.     {
    22.         //base.OnInspectorGUI();
    23.         GUILayout.Label("This script is to set up personas that will be able to speak within the game and their portraits");
    24.  
    25.         EditorGUILayout.PropertyField(nameProp);
    26.         speaker.neutralPortrait = (Sprite)EditorGUILayout.ObjectField("Neutral Sprite",neutralPProp,typeof(Sprite),false);
    27.         speaker.happyPortrait = (Sprite)EditorGUILayout.ObjectField("Happy Sprite",happyPProp,typeof(Sprite),false);
    28.         speaker.sadPortrait = (Sprite)EditorGUILayout.ObjectField("Sad Sprite",sadPProp,typeof(Sprite),false);
    29.  
    30.  
    31.  
    32.         //serializedObject.ApplyModifiedProperties();
    33.     }
    34. }
    As you can see, I've already dabbled with Serialized Properties and they work pretty well! Though, once I change my ObjectFields to include Serialized Properties, this is the result it get.



    Hurray! I can now change my object in the field and am using more flexible code! However, I've lost my nice preview image. Is there a way I can have the best of both worlds?! Here is the code I used to achieve this result:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. [CustomEditor(typeof(SpeakerSO))]
    5. public class SpeakerEditor : Editor
    6. {
    7.     SpeakerSO speaker;
    8.     SerializedProperty nameProp;
    9.  
    10.     SerializedProperty neutralPProp, happyPProp, sadPProp, angryPProp;
    11.  
    12.     private void OnEnable()
    13.     {
    14.         speaker = (SpeakerSO) target;
    15.         nameProp = serializedObject.FindProperty("characterName");
    16.         neutralPProp = serializedObject.FindProperty("neutralPortrait");
    17.  
    18.     }
    19.  
    20.     public override void OnInspectorGUI()
    21.     {
    22.         //base.OnInspectorGUI();
    23.         GUILayout.Label("This script is to set up personas that will be able to speak within the game and their portraits");
    24.  
    25.         EditorGUILayout.PropertyField(nameProp);
    26.         EditorGUILayout.ObjectField(neutralPProp, typeof(Sprite), new GUIContent("Neutral Sprite"));
    27.        
    28.         serializedObject.ApplyModifiedProperties();
    29.     }
    30. }
    31.  
    32.  
    Thank you for the help in advance!