- Home /
Inspector button for custom class
I know I can add buttons to monobehaviour based classes using editor extension class, but I can't seem to find any solution on how to add buttons to my custom classes.
Namely I have a class that should store pos,rot,scale and some others stuff and I want it to have editor button in Inspector so I can just move the object as I want in editor then save its position values.
This is usefull for me when I have array of objects of this class to easily just move object and click save instead of writing down that stuff then entering it manually.
Thanks in advance!
If I get what you mean you just want a Button in a custom editor? If not so, please say, IF so just GUILayout.Button() will do the trick.
I think u didn;t get it dude. Example:
[System.Seriazible]
ClassB
ClassA : $$anonymous$$onobehavior
ClassB[] ArrayofClassB = new ClassB[w/e];
How can i extend the ClassB to have button so that in editor when i open array list i have the button for each object in array?
EDIT: I've tried to do it classical way, but then I get error that I cant convert UnityEngine.Object to ClassB object.
Allright, so just for my information it's setup like this:
[System.Serializable]
public ClassB
{
//Stuff
}
public ClassA : $$anonymous$$onobehaviour
{
ClassB[] Array = new Class[w/e];
}
Answer by Malfegor · Sep 10, 2014 at 09:00 AM
ALLRIGHT! I'll just post all of my code (It's been done quite quick, but it's tested!)
Class B:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class ClassB
{
public string Name = "";
public ClassB(int ID)
{
Name = "Button " + ID;
}
}
Class A:
using UnityEngine;
using System.Collections;
public class ClassA : MonoBehaviour {
public ClassB[] Arraything = new ClassB[5] { new ClassB(1), new ClassB(2), new ClassB(3), new ClassB(4), new ClassB(5) };
}
Editor Class :
using UnityEditor;
using UnityEngine;
using System.Collections;
[CustomEditor(typeof(ClassA))]
public class ClassAEditor : Editor
{
bool IsFoldout = false;
public override void OnInspectorGUI ()
{
ClassA Target = (ClassA)target;
IsFoldout = EditorGUILayout.Foldout(IsFoldout, "Buttons");
if(IsFoldout)
for(int i = 0; i < Target.Arraything.Length; ++i)
{
GUILayout.Button(Target.Arraything[i].Name);
}
}
}
Remember that the Editor class needs to be in an Editor folder! Hope this works for ye!
Thank you man I forgot to mark this as valid answer, good job!
Actually it is a myth that editor classes need to be in an editor folder:
#if UNITY_EDITOR
[CustomEditor(typeof(MyClassName)), InitializeOnLoadAttribute]
public class MyClassNameEditor : Editor
{
MyClassName myClass;
SerializedObject so;
void OnEnable()
{
generator = (MyClassName)target;
so = new SerializedObject(myClass);
}
public override void OnInspectorGUI()
{
DrawPropertiesExcluding(so, "m_Script");
so.ApplyModifiedProperties();
so.Update();
if(GUILayout.Button("MyClassButtonLabel"))
{
myClass.DoStuff();
}
}
}
#endif
I came up with this a while back and have been hoarding it. Feel free to use it inside any script you want. Generally I do it on the script I am working on.
I have modified it to used titles instead of showing the script, and I removed that, so it will look naked at the top.
But it works great and alleviates the need for nonsense editor folders that make everything stupid ugly.
Your answer
Follow this Question
Related Questions
How do you make a custom inspector for a class or instance? 3 Answers
Questions Regarding Images in Custom Inspector/Editor 1 Answer
How would I go about creating a custom Unity Event in a Custom Unity Editor/Inspector? 0 Answers
Arranging KeyCode type variables in custom editor 0 Answers
Custom Inspector Variables 1 Answer