Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Dalsia · Mar 19, 2019 at 03:09 AM · arrayeditor-scriptingserializefield

How can I add a parameter to a button in a custom editor?

I've created a button in a custom editor script that is associated with a class containing a serialized array of Transforms. I am wondering if it's possible to associate the button with a certain index in that array from the inspector; right now I have to go into my code to change which prefab is used in my DoSomething(Transform item) method.

Here is an example case:

 [CustomEditor(typeof(TestClass))]
 public class TestClassEditor : Editor 
 {
     public override void OnInspectorGUI()
     {
         base.OnInspectorGUI();
 
         if (GUILayout.Button("Do Something"))
         {
             var test = target as TestClass;
             test.DoSomething(test.MyPrefabs[0]); //I have to update this index via code
         }
     }
 }
 
 public class TestClass : Monobehavior 
 {
     public Transform[] MyPrefabs { get { return myPrefabs; } }
 
     [SerializeField]
     private Transform[] myPrefabs;

     public void DoSomething(Transform item)
     {
         //do something
     }
 }

To be clear, my ideal scenario is to have some sort of dropdown menu next to the "Do Something" button in the inspector that contains all of the possible indices of the myPrefabs array. I would then be able to select a certain index, and that index would be passed to the DoSomething(Transform item) method in the custom editor script.

I'm pretty new to making my own editor scripts, so any help is very much appreciated.

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

1 Reply

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

Answer by WarmedxMints · Mar 19, 2019 at 03:59 AM

You can get the names of the transforms and EditorGUILayout has a popup method which you can populate with those names and the popup will return an int which will give you your index.


 using UnityEngine;
 
 #if UNITY_EDITOR  
 using UnityEditor;

 [CustomEditor(typeof(TestClass))]
 public class TestClassEditor : Editor
 {
     private TestClass _test;
     private int _index;
 
     public void OnEnable()
     {
         _test = (TestClass)target;
     }
 
     public override void OnInspectorGUI()
     {
         base.OnInspectorGUI();
 
         var enable = _test.MyPrefabs != null && _test.MyPrefabs.Length > 0;
 
         if (enable)
         {
             var names = GetNames();
 
             EditorGUILayout.Space();
 
             _index = EditorGUILayout.Popup(_index, names);
 
             EditorGUILayout.Space();
 
             if (GUILayout.Button("Do Something with " + names[_index]))
             {
                 _test.DoSomething(_test.MyPrefabs[_index]); //I have to update this index via code
             }
         }
         else
         {
             EditorGUILayout.LabelField("MyPrefabs is empty");
         }
     }
 
     private string[] GetNames()
     {
         var count = _test.MyPrefabs.Length;
 
         var ret = new string[count];
 
         for(var i = 0; i < count; i++)
         {
             ret[i] = _test.MyPrefabs[i].name;
         }
 
         return ret;
     }
 }
 #endif
 
 public class TestClass : MonoBehaviour
 {
     public Transform[] MyPrefabs { get { return _myPrefabs; } }
 
     [SerializeField]
     private Transform[] _myPrefabs;
 
     public void DoSomething(Transform item)
     {
         //do something
     }
 }
 

Comment
Add comment · Show 1 · 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 Dalsia · Mar 19, 2019 at 04:48 AM 0
Share

This worked great. Thank you!

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

128 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Fill/populate [SerializeField] automatically via script in editor mode? 3 Answers

Editor scripting: Object reference not set to an instance of an object 0 Answers

"add selected" editor script 1 Answer

Display custom names for array 1 Answer

Keeping object between edit and play modes. 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