- Home /
How can i create a button in a custom class with UnityEditor using custom editor?
I have a script to make dialogues, i create a second class in that script with the [System.Serializable] so i can see that class in the inspector, but i want to create a button that is supposed to be showed in the inspector, but i just can create the button in the primary class, i cant do it with the other class This is the Second class that i created in my script
[System.Serializable]
public class Dialogos{
[TextArea(5,3)]
public string dialogo;
public Vector3 offset;
}
How can i create a button so that i can see in the inspector(i declared that class as a array so i want that the button is created in any index i can create in the inspector) I hope i explained well :D
Deleted previous comment as I finally read the question :D
You need to create another script DialogosEditor and put it in Editor directory. It should have an attribute [CustomEditor(typeof(Dialogos))].
Check both links: https://docs.unity3d.com/ScriptReference/Editor.html
https://unity3d.com/learn/tutorials/topics/interface-essentials/adding-buttons-custom-inspector
Thats what i made!!, but the button just go to the bottom in the inspector, i cannot do that the buttons appears under the Vector3 variable, i cant adjust the position of the button
if you want the button below Vector3 field, you cannot use Draw Default editor. You have to remake every property step by step, line by line
https://docs.unity3d.com/ScriptReference/EditorGUILayout.PropertyField.html
[CustomEditor(typeof($$anonymous$$yGameObjectScript))]
[CanEdit$$anonymous$$ultipleObjects]
public class EditorGUILayoutPropertyField : Editor
{
SerializedProperty m_IntProp;
SerializedProperty m_VectorProp;
SerializedProperty m_GameObjectProp;
void OnEnable()
{
// Fetch the objects from the GameObject script to display in the inspector
m_IntProp = serializedObject.FindProperty("m_$$anonymous$$yInt");
m_VectorProp = serializedObject.FindProperty("m_$$anonymous$$yVector");
m_GameObjectProp = serializedObject.FindProperty("m_$$anonymous$$yGameObject");
}
public override void OnInspectorGUI()
{
EditorGUILayout.BeginVertical();
//The variables and GameObject from the $$anonymous$$yGameObject script are displayed in the Inspector with appropriate labels
EditorGUILayout.PropertyField(m_IntProp, new GUIContent("Int Field"), GUILayout.Height(20));
EditorGUILayout.PropertyField(m_VectorProp, new GUIContent("Vector Object"));
if (GUILayout.Button("I am a regular Automatic Layout Button"))
Debug.Log("Clicked Button");
EditorGUILayout.PropertyField(m_GameObjectProp, new GUIContent("Game Object"));
// Apply changes to the serializedProperty - always do this at the end of OnInspectorGUI.
EditorGUILayout.EndVertical();
serializedObject.Apply$$anonymous$$odifiedProperties();
}
}
This is the editor script i made
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Dialogo), true)]
public class DialogoEditor : Editor {
public override void OnInspectorGUI(){
DrawDefaultInspector();
}
}
As you can see, is a Little bit empty, thats why i dont put it in the question xD
First of all you have a typo: Dialogo ins$$anonymous$$d of Dialogos in CustomEditor :)