- Home /
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:
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:
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?
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();
}
}
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.
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?
Oh wait, you said Field now, in your question you said members. Do you want to expose Fields (Variables) or $$anonymous$$ethods (Functions)?
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.
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
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);
}
}
}
}
Your answer
Follow this Question
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