简体   繁体   English

C#Unity:在附加到GameObject的脚本中获取公共类

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

edit: Vanethrane brought my attention to the fact that I am already calling classes in my examples - but I'm looking for those who's name I cannot check manually (it is for this purpose unknown). 编辑:Vanethrane使我注意到了我已经在示例中调用类的事实-但我正在寻找无法手动检查的名称(为此目的是未知的)。

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 automatically 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; 我认为,如果我不使用变量名搜索,而是使用诸如bool isReadable = true之类的额外变量来创建一个类,那会更好地满足我的需求。 and some other neat information (isEditable, displayName etc.). 以及其他一些简洁的信息(isEditable,displayName等)。

So, how do I go about it in this context? 那么,在这种情况下我该如何处理呢?

Searching in entire GameObject as opposed to single scripts would work as well. 搜索整个GameObject而不是单个脚本也可以。

you already have the script object why not just change its properties directly? 您已经有了脚本对象,为什么不直接更改其属性呢?

  MyFirstScript elementproperties = GameObject.GetComponent<MyFirstScript>();

script.my_blah="blahblah";

or in your case: 或者您的情况:

elementproperties.variable=change;

you need to add the public modifier to any variables you want to get from outside class 您需要将public修饰符添加到要从外部类获取的任何变量中

For just getting a list of all components that are attached to a GameObject you can simply use ( Source ) 为了只获取GameObject附带的所有组件的列表,您可以简单地使用( Source

GetComponents(typeof(Component));

Than for checking the components types / type names you might prefere using Regex to look for certain matches without hai´ving to check the complete name. 比起检查组件类型/类型名称,您可能更喜欢使用Regex查找某些匹配项而无需检查完整名称。

In general it is recommend to use a switch - case instead of if - else if you are checking just one variable for different values since this is way more efficient. 通常,建议您使用一个switch - case而不是if - else如果您只检查一个变量的不同值,因为这样做效率更高。

Unfortunately this does not work for comparing types like 不幸的是,这不适用于比较像

var Type = component.GetType();

// can not use switch for that
if(Type == typeof(MyFirstClass)) // ... etc

But you can use the Regex to match a certain part of the Type name using eg 但是您可以使用Regex来匹配Type名称的特定部分,例如

var components = GetComponents(typeof(Component));

foreach(var component in components)
{
    var Type = component.GetType();
    var TypeName = Type.Name;

    // Matching types starting with My
    if( Regex.IsMatch(TypeName, "My.*"))
    {
       // ....
    }
}

(For writing and testing regex I recommend https://regexr.com ) (对于编写和测试正则表达式,我建议https://regexr.com

Or you can use a switch - case for matching complete type names like 或者您可以使用一个switch - case匹配完整的类型名称

foreach(var component in components)
{
    var Type = component.GetType();
    var TypeName = Type.Name;

    switch(TypeName)
    {
        case "MyFirstClass":
        // ...
        break;

        case "MySecondClass":
        // ....
        break;

        //.... etc

        default:
        // Nothing matched
        return;
    }
}

Unlike an if - else if - else statement, a swith - case does not lose performance with more cases so even if there are a lot of names to check this might be the best approach. if - else if - else语句不同, swith - case不会在更多案例中失去性能,因此即使要检查的名称很多,这也可能是最好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM