コード例 #1
0
    public static void setComp(GameObject go, string compStr)
    {
        if (null == go || string.IsNullOrEmpty(compStr))
        {
            return;
        }

        List <KeyValuePair <string, List <KeyValuePair <string, KeyValuePair <string, KeyValuePair <string, string> > > > > > propList = null;

        ComponentSimulator.decode(ref propList, compStr);

        for (int i = 0; i < propList.Count; ++i)
        {
            Type type = Type.GetType(propList[i].Key);
            if (null == type)
            {
                Debug.LogError("getType error! " + propList[i].Key);
                continue;
            }

            Component comp = go.GetComponent(type);
            if (null == comp)
            {
                Debug.LogError("getComponent error! " + go + " " + propList[i].Key);
                continue;
            }

            List <KeyValuePair <string, KeyValuePair <string, KeyValuePair <string, string> > > > compPropList = propList[i].Value;
            PropertyInfo[] pis = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            for (int j = 0; j < pis.Length; ++j)
            {
                if (!pis[j].CanWrite)
                {
                    continue;
                }

                int idx = compPropList.FindIndex(p => p.Key == pis[j].Name);
                if (-1 == idx)
                {
                    continue;
                }

                MethodInfo mi       = pis[j].GetSetMethod();
                Type       propType = pis[j].PropertyType;
                string     val      = compPropList[idx].Value.Value.Value;
                object     obj      = TypeOpe.StringToType(propType, val);
                if (null == obj)
                {
                    continue;
                }

                object[] valParam = new object[] { obj };
                try
                {
                    mi.Invoke(comp, valParam);
                }
                catch (Exception e)
                {
                    Debug.LogError(e.ToString());
                }
            }
        }
    }