- Home /
Custom Editor Gameobject foldout C#
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.
Answer by VivienS · Jan 28, 2016 at 08:49 PM
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
Thank you so much. I had tried to do it with the System.Serializable but it wasn't working, I was stupid and didn't have the public whateverclassname name;
You don't know how long I have been searching for that!!!!!! I love you!!!!! :D :D :D
Thank you soooo much for this answer! I've been trying to figure out how to do this for a long time now.!
Answer by giuliano-marinelli · Sep 03, 2021 at 07:09 PM
You maybe can use this custom decorator https://github.com/giuliano-marinelli/UnityFoldoutDecorator for Foldout Group and Range Decorator for the sliders.
Cheers.
Your answer
![](https://koobas.hobune.stream/wayback/20220612062103im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with Missing Monobehaviours and Asset Serialization? 0 Answers
Bind to OnWillSaveAssets and force to save my scene 0 Answers
Custom Editor - Is there any way to detect whether the user is in Prefab editing mode? 1 Answer
Is it possible to create a custom gettter/setter on SerializedProperty ? 0 Answers
object field decides when it wants to work,Why does my list clear itself 0 Answers