mardi 9 octobre 2018

C# Unity: Get public classes in script attached to GameObject

Still new to reflections: I'm currently reading a bunch of properties and printing them - I'm eliminating the undesired ones by giving the ones I want a name with specific prefix. For example:

var elementProperties = new object();

if(element.GetComponent<MyFirstScript>() != null)
    elementProperties = element.GetComponent<MyFirstScript>();
else if(element.GetComponent<MySecondScript>() != null)
    elementProperties = element.GetComponent<MySecondScript>();
else
        return;

System.Reflection.PropertyInfo[] properties = elementProperties.GetType().GetProperties();

for(int i = 0; i < properties.Length; i++)
{
    if(properties[i].Name.Substring(0, 3) != "my_")
        continue;
    //IF VARIABLE NAME STARTS WITH "my_" DO STUFF WITH properties[i].PropertyType OR GetValue ...
}

The trouble is this doesn't find classes. I thought it would better fit my needs if I would instead of searching by variable name, to make a class with extra variables such as bool isReadable = true; and some other neat information (isEditable, displayName etc.).

So, how do I go about it in this context?

Searching in entire GameObject as opposed to single scripts would work as well.





Aucun commentaire:

Enregistrer un commentaire