Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 MDarkwing · Sep 09, 2014 at 04:38 PM · editorbuttoninspectorclasscustom

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!

Comment
Add comment · Show 3
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 Malfegor · Sep 09, 2014 at 04:55 PM 0
Share

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.

avatar image MDarkwing · Sep 10, 2014 at 08:27 AM 0
Share

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.

avatar image Malfegor · Sep 10, 2014 at 08:41 AM 0
Share

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];
 }

1 Reply

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

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!

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 MDarkwing · Sep 25, 2014 at 09:09 AM 0
Share

Thank you man I forgot to mark this as valid answer, good job!

avatar image Malfegor · Sep 25, 2014 at 09:53 AM 0
Share

No probs, man. Glad it worked for you!

avatar image triangle4studios · Mar 11 at 04:48 AM 0
Share

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

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

25 People are following this question.

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

Related Questions

How do you make a custom inspector for a class or instance? 3 Answers

Custom Editor problems 2 Answers

Can i extend or customise editor for all MonoBehaviours? 3 Answers

Get if inspector field labels are on separate line 1 Answer

How can I create a list of preset class/objects to be used in the editor 1 Answer


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