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
1
Question by Artifactx · Jan 27, 2015 at 06:04 AM · inspectorreflection

Custom Editor: Inspect members of GameObject

I'm trying to achieve the same functionality as the OnClick() on the Button Script in the UNITY UI. I want to be able to add a GameObject in the inspector, and then select a script that is attached to that object and then select a variable or function from that script.

This picture illustrates what I want: alt text

So far I have been able to add an object to the inspector and add all scripts from that object to an array, my code looks like this:

 [CustomEditor(typeof(BarScript))]
 public class LevelScriptEditor : Editor
 {
     //Array that holds all scripts
     private MonoBehaviour[] scripts;
 
     public override void OnInspectorGUI()
     {
         //Creates a reference to the target
         BarScript myBarScript = (BarScript)target; 
 
         //Set's the barscript's object to the object that we just dragged onto the inspector
         myBarScript.ObjectToMonitor = (GameObject)EditorGUILayout.ObjectField(myBarScript.ObjectToMonitor, typeof(GameObject), true);
 
         //Stores all scripts on the object in the MonoBehaviour array
         scripts = myBarScript.ObjectToMonitor.GetComponents<MonoBehaviour>();
     }
 }

This is what the array contains:

alt text

My question is. How do I show the names of these scripts in the inspector and expose the members of these scripts, so that they can be selected from the inspector?

gg.png (9.5 kB)
27-01-2015-06-43-37.png (20.4 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
0

Answer by GameVortex · Jan 27, 2015 at 07:39 AM

That functionality is exposed from Unity. No need to make your own solution. You just need to create a UnityEvent variable in your script. Then you will have the same functionality:

 using UnityEngine;
 using System.Collections;
 
 public class EventsExample : MonoBehaviour 
 {
     public UnityEngine.Events.UnityEvent onClick;
 
     private void OnMouseDown()
     {
         onClick.Invoke();
     }
 }
Comment
Add comment · Show 6 · 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 Artifactx · Jan 27, 2015 at 08:16 AM 0
Share

OnClick was just an example. It's not the onClick I want its the ability to pick a a specific script and then select a field from the inspector.

avatar image GameVortex · Jan 27, 2015 at 08:22 AM 0
Share

OnClick here is also just an example, name it whatever you want and invoke it whenever you want.

What is your end goal? What do you want to do with the functions you select?

avatar image GameVortex · Jan 27, 2015 at 08:26 AM 0
Share

Oh wait, you said Field now, in your question you said members. Do you want to expose Fields (Variables) or $$anonymous$$ethods (Functions)?

avatar image Artifactx · Jan 27, 2015 at 08:26 AM 0
Share

I'm trying to create an asset package. And I need it to be as generic as possible. The functionality in the package is dependant on 1 variable from the user. I want to make it possible for the user to select this variable by dragging his object to my script and then selecting the variable my package needs to use.

avatar image GameVortex · Jan 27, 2015 at 08:33 AM 0
Share

Alright. You can easily get all fields with reflection: Type.GetFields

Then create a string array from the returned FieldInfo array and display it with: http://docs.unity3d.com/ScriptReference/EditorGUILayout.Popup.html

Show more comments
avatar image
0

Answer by Simeon · Jan 27, 2015 at 09:30 AM

Use Type.IsSubclassOf to get all classes that the specified object derives from. Then find all methods and store them as Method Info. Then you can invoke them from that method info anytime you want just by specifying a object reference and parameters;

 List<System.Reflection.MethodInfo> infos = new List<MethodInfo>();
 
                 foreach (System.Reflection.Assembly ass in System.AppDomain.CurrentDomain.GetAssemblies())
                 {
                     foreach (System.Type type in ass.GetTypes())
                     {
                         if(typeof(MyType).IsSubclassOf(type))
                         {
                             foreach(System.Reflection.MethodInfo info in type.GetMethods(BindingFlags.Instance))
                             {
                                 infos.Add(info);
                             }
                         }
                     }
                 }

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

21 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

Related Questions

Get all exposed properties from all components 0 Answers

Building a Collection of Subclasses and Modifying Their Public Members from the Inspector 0 Answers

How to modify ParticleSystem from Editor script? 1 Answer

How does Unity sort fields in order of their declaration? 1 Answer

How can I hide a MonoBehaviour from the Inspector? (or make it extremely thin or compact) 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