如何更改 child 属性 个 parent 元素?

How to change child property of parent elements?

我正在制作简单的游戏,但我不知道如何在代码中更改颜色。 我有 Parent 游戏 object,我打算在其中附加脚本以更改 child 元素颜色。

例如我有这个:

Parent:A
Childs in A = 1,2;

我想获取所有 2 个元素并在第一个 child 中将颜色更改为黑色,在第二个中更改为白色。

我想在改变颜色时改变标签,这样我就可以实现随机颜色 在 childs.

我不知道我怎样才能在 parent 中得到那 2 child 来改变 属性。

我可以将 child 命名为 1 和 2,然后从代码中查找 child 和游戏 object 名称 1 并更改颜色 属性 吗,如果我我该怎么做?

下面的代码部分是 GetProperty 方法的快速示例用法。只需使用 MyGetProperty 和 MySetProperty 如下所示。请记住,字符串将引用的变量必须是属性。

public class Parent {
        private int child1 = 0;
        private int child2 = 0;
        public int iChild1 {
            get {
                return child1; 
            }
            set {
                child1 = value;
            }
        }
        public int iChild2 {
            get {
                return child2;
            }
            set {
                child2 = value;
            }
        }

        public void MainMethod() { 
            MySetProperty("iChild1",1);
            MySetProperty("iChild2",2);
            string strOutput = String.Format("iChild1 = {0} iChild2 = {1}",MyGetPrperty("iChild1"), MyGetPrperty("iChild2"));
        }

        public object MyGetProperty(string strPropName)
        {
            Type myType = typeof(Parent);
            PropertyInfo myPropInfo = myType.GetProperty(strPropName);
            return myPropInfo.GetValue(this, null);
        }

        public void MySetProperty(string strPropName, object value)
        {
            Type myType = typeof(Parent);
            PropertyInfo myPropInfo = myType.GetProperty(strPropName);
            myPropInfo.SetValue(this, value, null);
        }

    }