- Home /
How to display a list of methods and allow a choice of one of them
I am able to get a list of methods from a script but I am unable to actually show each individual method in the editor and I am not really sure how to do so. All I really need to know is how to show an element of an array. Any help is appreciated. Cheers.
Answer by yummy81 · Jan 31, 2018 at 09:06 PM
I wrote this script on the fly. It creates public list of strings ( the list will be shown in the inspector ), which it subsequently fills with the names of all the methods declared in the script. They are found via reflection.
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using System.Reflection;
using System;
using System.Linq;
public class MyScript : MonoBehaviour
{
public List<string> methods;
private void OnValidate()
{
this.Foo();
}
private void Foo()
{
Type t = this.GetType();
MethodInfo[] m = t.GetMethods(BindingFlags.Instance|BindingFlags.Static|BindingFlags.NonPublic|BindingFlags.Public).Where(x=>x.DeclaringType==t).OrderBy(x=>x.Name).ToArray();
this.methods = new List<string>();
foreach (var n in m)
{
this.methods.Add(n.Name);
}
}
private void Awake() {}
private IEnumerator Start() {yield return new WaitForSeconds(0.5f);}
protected virtual void Update() {}
public void Foo1(string a, float b) {}
private void Foo2() {}
private static void Foo3() {}
protected int Foo4(int a) {return a;}
private void OnGUI() {}
}
I will give it a try thank you so much for your reply.
@piotrek_81
Ok I am doing this:
[System.Serializable]
public class AnswersForInterview
{
public string answer;
public $$anonymous$$onoScript component;
}
public class ShowFunctionList : $$anonymous$$onoBehaviour
{
AnswersForInterview answer;
$$anonymous$$onoScript toUse;
public List<string> methods;
private void OnValidate()
{
Foo();
}
private void Foo()
{
toUse = answer.component;
Type t = toUse.GetType();
$$anonymous$$ethodInfo[] m = t.Get$$anonymous$$ethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public).Where(x => x.DeclaringType == t).OrderBy(x => x.Name).ToArray();
methods = new List<string>();
foreach (var n in m)
{
methods.Add(n.Name);
}
}
}
Those are 2 different classes in which I am trying to show in the inspector. Am I doing something wrong. Thanks for your help.
let's take the "answer" as the example. If you want it to be shown in the inspector, there are two ways you can achieve this. You can do this:
public AnswersForInterview answer;
Or this:
[SerializeField]
private AnswersForInterview answer;
The same for "toUse". If you don't specify the access modifier it will be set as "private" by dafault.
So I am using the $$anonymous$$onoScript component from the smaller class and getting its methods but after I drag in the $$anonymous$$onoScript the methods list does not populate with any of the methods from it. It has an array size of 0. I want to be able to drag any script in to the editor and show its methods but it will not work.
I think it is because I have to check to see if a public variable is filled while in edit mode in unity. I am adding the script only in edit mode.
I figured out the problem but it doesnt help me much. So my classes must be [Serializable] so I can access their features for my main class. So OnValidate() will not work because it only Validates for $$anonymous$$onoBehaviour classes. Is there some work around for custom classes?