Im creating a script to help me with animating a character in my game (Many pieces make it hard to animate), Im trying to organize my script some so I am looking into custom editors a bit.
This is the inspector at the moment
And here is the inspector script
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(SpineAnimator))]
public class SpineAnimatorEditor : Editor {
private bool showBodyParts = false;
private bool showBodySliders = false;
public override void OnInspectorGUI(){
DrawDefaultInspector ();
showBodyParts = EditorGUILayout.Foldout(showBodyParts, "Body Parts");
if (showBodyParts) {
}
showBodySliders = EditorGUILayout.Foldout (showBodySliders, "Body Sliders");
if (showBodySliders) {
}
SpineAnimator spineScript = (SpineAnimator)target;
if (GUILayout.Button ("Reset All")) {
spineScript.reset();
}
}
}
I am trying to put the sliders and gameobjects into foldouts, I already have the foldouts now how would I go about putting the sliders and gameobjects in the foldout.
Sorry! I got carried away at first!
Here is the correct answer to your initial question (Game Object Fields, Sliders)
public override void OnInspectorGUI ()
{
MyClass aTarget = target as MyClass;
m_showBodyParts = EditorGUILayout.Foldout (m_showBodyParts, "Show Body Parts");
if (m_showBodyParts) {
aTarget.m_RightLowerArmAngle = EditorGUILayout.Slider
(
aTarget.m_RightLowerArmAngle,
0f,
10f
);
aTarget.m_RArmLow = EditorGUILayout.ObjectField
(
"R Arm Low",
aTarget.m_RArmLow,
typeof(GameObject),
true
);
}
}
However, just for foldouts and sliders you don’t need to do an editor script. The Unity inspector GUI + some Attributes will do just fine and you don’t need to create the second C# file.
If you write something like this as simple public variables to your Monobehavior:
public class MyClass : MonoBehaviour
{
[System.Serializable]
public class MyClassRArmData
{
public GameObject m_RArmLow;
public GameObject m_RArmUp;
[Range(0f,1f)]
public float m_RightLowerArmAngle = 0f;
}
public MyClassRArmData m_rArmData;
[System.Serializable]
public class MyClassLArmData
{
public GameObject m_LArmLow;
public GameObject m_LArmUp;
[Range(0f,1f)]
public float m_LeftLowerArmAngle = 0f;
}
public MyClassLArmData m_lArmData;
...
…without any additional editor class, this will appear as
