Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 heroshotgun · Mar 08, 2021 at 12:49 AM · editor-scripting

Populate Editor Popup with Selected Gameobject Functions

I have a gameobject that a user can select in the editor although I want to be able to access its public functions via the property drawer since its custom.


My intention is to have a drop down menu that displays all the objects classes and functions that a user can select via a popup drop down. Kinda similar to how the event component works

alt text


I know I can usually do that by getting all MonoBehaviour components via GetComponents but since this is a property drawer, it doesn't inherit from MonoBehaviou. Meaning I don't have access to those functions.


This is what i have so far

         EditorGUI.PropertyField(rect, prop, GUIContent.none);
         
         if (startTriggerProp.objectReferenceValue != null) {
             // Create popup drop down list with public functions of selected gameobject
         }


capture.png (4.5 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

1 Reply

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

Answer by CodesCove · Mar 08, 2021 at 06:56 PM

Here are few ideas to try / develop further (this more like pseudocode):

1)

If you just want the user to be able to select methods in edit mode to be invoked in the actual game then the easiest is to add out-of-the-box component where it looks like public UnityEvent unityEvent; variable. This has already default drawer where you can select the component and it gives you a list of available public methods methods (something like EventTrigger your screen capture is taken from). You can then invoke the methods via that event.

If you define the unityevent in the Editor code then you get the serialized property from it like this:

 SerializedProperty serializedEvent =  new SerializedObject(this).FindProperty("unityEvent");

After that just put in inside OnGUI this:

 EditorGUI.PropertyField(positionRect, serializedEvent);

Note: The UnityEvent is now only in the Editor code, not in the actual game code. So in the end you need UnityEvent to some Monobehavior so you can store the actual unityevent. Actually easiest way would be to already have it in some component and just get reference to it from the editor.

2)

Otherwise you need to use reflection yourself to get the MethodInfo's from the class, something like like this:

First make the field for the Component you want the methods from so you can drag & drop the needed component:

 Component c = EditorGUI.Objectfield(positionRect, c, typeof(Component));

then get the method infos to array (notice: don't call this in every OnGUI, only when c not null and is changed)

 MethodInfo[] methodInfos = c.GetType().
                            .GetMethods(BindingFlags.Public | BindingFlags.Instance);

then get method names to a List from the methodinfos array

 List<string> methodNames = new List<string>();
 foreach (MethodInfo m in methodInfos) methodNames.Add(m.Name);

then use EditorGUI.Popup to show the string list (using .ToArray() ) and handle the selection.

Because the indexing of string and methodinfo arrays matches (well this not guaranteed way but you can make it better) you can use the same selected int index to reference to the actual methodinfo to do what you want with it.

Note:

Above examples were more like pseudo code but I have used similar methodology in my own editor components so at least the ideas should be ok.

Hope some of this is help to you and you get everything working!

Added note:

You actually asked about to get the component references... This is quite easy to get:

 GameObject inspectedObjet = EditorGUI.Objectfield(positionRect, inspectedObjet  typeof(GameObject));

and then just get the Component rererences:

 Component[] comps = inspectedObjet.GetComponents<Component>();

after that iterate the components and use earlier shown reflection

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 heroshotgun · Apr 03, 2021 at 05:32 PM 0
Share

Thanks for that!

For anyone else looking into this, I simply used the first option:

 // MonoBehaviour Script
 public UnityEvent m_event;
 
 ---------------------
 
 // Custom Editor Script
 SerializedProperty m_prop = serializedObject.FindProperty("m_event");
 EditorGUILayout.PropertyField(onCheck);
 
 if (GUI.changed)
 {
        serializedObject.ApplyModifiedProperties();
 }


Here some threads that contain the same answer:

https://answers.unity.com/questions/1026062/how-to-add-unityevent-using-custom-inspector.html

https://answers.unity.com/questions/802786/46-how-do-i-draw-unityevent-in-custom-inspector.html

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

124 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

Related Questions

JavaScript version of [Serializable ]? 4 Answers

RequireComponent with custom public vars? 1 Answer

Created a new C# class via editor script, but error arise when I attempt to add the new component to a gameobject 2 Answers

Customize Editor Display 0 Answers

Storing a list when application Quits 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