Search Unity

Unity Editor error with GUIContent

Discussion in 'Scripting' started by zine92, Nov 25, 2011.

  1. zine92

    zine92

    Joined:
    Nov 13, 2010
    Posts:
    1,347
    I have a script that has a public variable of type GUIContent. I'm currently writing an editor script that uses SerializedProperty and SerializedObject. When I tried to use EditorGUILayout.PropertyField(); on the guicontent variable, Unity shows an error:
    Code (csharp):
    1.  
    2. type is not a supported pptr value
    3. UnityEditor.SerializedProperty:get_objectReferenceValue()
    4. QuitButtonEditor:OnInspectorGUI() (at Assets/Main Assets/Core Game/GUI/Button/Quit Button/Editor/QuitButtonEditor.cs:27)
    5. UnityEditor.DockArea:OnGUI()
    6.  
    So is there anyway to use customeditor on the GUIContent field?

    Here's a sample code
    Code (csharp):
    1.  
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. [CustomEditor(typeof(QuitButton))]
    6. public class QuitButtonEditor : Editor
    7. {
    8.     private SerializedObject m_Object;
    9.  
    10.     private SerializedProperty content;
    11.     private SerializedProperty guiPosition;
    12.  
    13.     private void OnEnable()
    14.     {
    15.         m_Object = new SerializedObject(target);
    16.  
    17.         content = m_Object.FindProperty("content");
    18.         guiPosition = m_Object.FindProperty("guiPosition");
    19.     }
    20.  
    21.     public override void OnInspectorGUI()
    22.     {
    23.         //DrawDefaultInspector();
    24.  
    25.         m_Object.Update();
    26.  
    27.         // Put a texture selection
    28.         content.objectReferenceValue = EditorGUILayout.ObjectField(content.objectReferenceValue, typeof(Texture2D), true,GUILayout.MaxWidth(120), GUILayout.MaxHeight(120));
    29.         //EditorGUILayout.PropertyField();
    30.  
    31.         // Put a Rect box for setting GUI position
    32.         EditorGUILayout.PropertyField(guiPosition);
    33.  
    34.         m_Object.ApplyModifiedProperties();
    35.     }
    36. }
    37.  
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You obviously want to work with a Texture2D. I don't see a reason for why you should have a GUIContent instead of a Texture2D field. Maybe you have a good reason for that.

    WARNING: UNTESTED CODE
    I think that should work:
    Code (csharp):
    1. (content.objectReferenceValue as GUIContent).image = EditorGUILayout.ObjectField((content.objectReferenceValue as GUIContent).image, typeof(Texture2D), true,GUILayout.MaxWidth(120), GUILayout.MaxHeight(120));
    Maybe you also need to specify that you are dealing with Texture2D:
    Code (csharp):
    1. (content.objectReferenceValue as GUIContent).image = EditorGUILayout.ObjectField(((content.objectReferenceValue as GUIContent).image as Texture2D), typeof(Texture2D), true,GUILayout.MaxWidth(120), GUILayout.MaxHeight(120));
    If it doesn't work, I hope you get the idea.