Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by darugylsweater · Jan 28, 2016 at 05:47 PM · c#editoreditor-scriptingeditorguilayout

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 alt text

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.

spineanimator.png (27.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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

alt text


no-editor-gui.jpg (55.4 kB)
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image darugylsweater · Jan 28, 2016 at 10:09 PM 0
Share

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;

avatar image Rispat-Momit · Dec 15, 2019 at 04:48 AM 0
Share

You don't know how long I have been searching for that!!!!!! I love you!!!!! :D :D :D

avatar image Tiberius1701 · Jun 22, 2020 at 11:47 PM 0
Share

Thank you soooo much for this answer! I've been trying to figure out how to do this for a long time now.!

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges