- Home /
How to draw the default read-only object label in a CustomEditor?
I am starting to learn about custom editors to create a better inspector for my AI settings. With so many settings, I decided to create a bool that while unchecked will hide things that I don't need to change.
But, I could not find a way to create a read-only object field that opens the script when clicked, while every monobehaviours class has it in it's default inspector.
By now, I am using a simple label to just act like an "placeholder" (as show in "AIBehaviourShoot") for the desired label (as show in "AIBehaviourChase" and every other default inspector).
and here is my current script:
using UnityEditor;
[CustomEditor(typeof(AIBehaviourChase))]
[CanEditMultipleObjects]
public class AIBehaviourChaseEditor : Editor
{
SerializedProperty canChase;
private void OnEnable()
{
canChase = serializedObject.FindProperty("canChase");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
if (!canChase.hasMultipleDifferentValues)
{
if (canChase.boolValue)
{
// the defaul inspector shows the read-only object field
DrawDefaultInspector();
}
else
{
// this is the placeholder label that I want to change.
EditorGUILayout.LabelField("Script", "AIBehaviourChase");
canChase.boolValue = EditorGUILayout.Toggle("Can Chase", canChase.boolValue);
}
}
serializedObject.ApplyModifiedProperties();
}
}
Sorry my english.
EDIT: There is no way of doing it?
Answer by a_p_u_r_o · Jan 18, 2016 at 05:47 PM
Maybe this is what you are looking for.
http://docs.unity3d.com/ScriptReference/EditorGUI.BeginDisabledGroup.html
Here goes an example.
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(NewBehaviourScript))]
public class NewBehaviourScriptGUI : Editor {
public override void OnInspectorGUI()
{
string[] guids = AssetDatabase.FindAssets("NewBehaviourScript t:Script");
string path = AssetDatabase.GUIDToAssetPath(guids[0]);
Object obj = AssetDatabase.LoadAssetAtPath<Object>(path);
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.ObjectField("Script:", obj, typeof(Object), false);
EditorGUI.EndDisabledGroup();
}
}
Follow up as of May 10, 2016.
I recently found that this question was asked before.
http://answers.unity3d.com/questions/550829/
And learned it can be also written in this way.
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Script"));
EditorGUI.EndDisabledGroup();
Simplest! and may be this is the most similar to what Unity does internally, I guess.
It depends but you may prefer to use MonoScript.FromMonoBehaviour(). See comments.
YES! Exactly this! I already knew the DisabledGroup thing, the problem was to reference the asset path, never knew about GUIDToAssetPath, thank you so much!
This seems to be a very complicated and risky way of getting hold of the script asset reference. Unity has a method for getting the $$anonymous$$onoScript instance from a $$anonymous$$onoBehaviour reference:
$$anonymous$$onoScript.From$$anonymous$$onoBehaviour
This method will return the reference to the $$anonymous$$onoScript asset that the given $$anonymous$$onoBehaviour belongs to. $$anonymous$$onoScript is an editor class that represents a script asset. $$anonymous$$onoScript is derived from TextAsset and literally represents the plain text of the script file. It also adds the GetClass method which returns the System.Type of the $$anonymous$$onoBehaviour class / ScriptableObject class that is defined inside that script asset.
Thank you, Bunny83! I thought there must be something of that effect. But I couldn't find it out.
Now we can make it as easy as this.
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(NewBehaviourScript))]
public class NewBehaviourScriptGUI : Editor {
public override void OnInspectorGUI() {
$$anonymous$$onoScript script = $$anonymous$$onoScript.From$$anonymous$$onoBehaviour(target as $$anonymous$$onoBehaviour);
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.ObjectField("Script", script, typeof($$anonymous$$onoScript), false);
EditorGUI.EndDisabledGroup();
}
}
Learning something new everyday... Thank you both for the replies! $$anonymous$$nowing about that $$anonymous$$onoScript class will surely help me even more.
Answer by Masterio · Jan 04, 2016 at 12:51 PM
I already checked the scripting API, but it only show how to create a selectable object field. I really wanted to know how Unity made it to be a read-only object field.