示例#1
0
 public TweenerTypeData(TweenerDataAttribute attr, ePropertyType propertyType, Type type, Type baseType, Type[] generics)
 {
     this.attr         = attr;
     this.propertyType = propertyType;
     this.type         = type;
     this.baseType     = baseType;
     this.generics     = generics;
 }
示例#2
0
        static void Initialize()
        {
            string scriptPath = "Assets/TweenComponent.cs";

            foreach (string guid in AssetDatabase.FindAssets("TweenComponent"))
            {
                string p = AssetDatabase.GUIDToAssetPath(guid);
                if (!p.EndsWith("/TweenComponent.cs"))
                {
                    continue;
                }
                scriptPath = p;
            }
            Type          tFloat                    = typeof(float);
            Type          tVector2                  = typeof(Vector2);
            Type          tVector3                  = typeof(Vector3);
            Type          tQuaternion               = typeof(Quaternion);
            Type          tColor                    = typeof(Color);
            Type          ta                        = typeof(TweenerDataAttribute);
            Type          ttb                       = typeof(TweenerBase);
            Type          ttc                       = typeof(TweenComponentBase);
            string        tns                       = ttc.Namespace;
            string        eTweenTypeFullName        = tns + ".TweenComponent+eTweenType";
            Type          prevTweenTypeEnum         = null;
            List <string> nss                       = new List <string>();
            SortedList <int, TweenerTypeData> types = new SortedList <int, TweenerTypeData>();

            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (Type type in assembly.GetTypes())
                {
                    if (type.IsEnum)
                    {
                        if (type.FullName == eTweenTypeFullName)
                        {
                            prevTweenTypeEnum = type;
                        }
                        continue;
                    }
                    if (!type.IsSubclassOf(ttb))
                    {
                        continue;
                    }
                    if (type.IsGenericType)
                    {
                        continue;
                    }
                    TweenerDataAttribute attr = Attribute.GetCustomAttribute(type, ta) as TweenerDataAttribute;
                    if (attr == null)
                    {
                        continue;
                    }
                    Type bt = type.BaseType;
                    if (!bt.IsGenericType)
                    {
                        continue;
                    }
                    Type[] gts = bt.GetGenericArguments();
                    if (gts.Length != 3)
                    {
                        continue;
                    }
                    ePropertyType pt = ePropertyType.Float;
                    if (tFloat.Equals(gts[1]))
                    {
                        pt = ePropertyType.Float;
                    }
                    else if (tVector2.Equals(gts[1]))
                    {
                        pt = ePropertyType.Vector2;
                    }
                    else if (tVector3.Equals(gts[1]) || tQuaternion.Equals(gts[1]))
                    {
                        pt = ePropertyType.Vector3;
                    }
                    else if (tColor.Equals(gts[1]))
                    {
                        pt = ePropertyType.Color;
                    }
                    else
                    {
                        continue;
                    }
                    types.Add(attr.Priority, new TweenerTypeData(attr, pt, type, bt, gts));
                    string ns = GetTypeNameSpace(type);
                    if (!string.IsNullOrEmpty(ns) && !nss.Contains(ns))
                    {
                        nss.Add(ns);
                    }
                    foreach (Type t in gts)
                    {
                        ns = GetTypeNameSpace(t);
                        if (!string.IsNullOrEmpty(ns) && !nss.Contains(ns))
                        {
                            nss.Add(ns);
                        }
                    }
                }
            }
            nss.Sort();
            Type          tt         = typeof(Transform);
            List <string> tweenTypes = new List <string>();
            List <List <TweenerTypeData> > tweenDatas = new List <List <TweenerTypeData> >();
            List <Type> components = new List <Type>();

            foreach (KeyValuePair <int, TweenerTypeData> kv in types)
            {
                int index = tweenTypes.IndexOf(kv.Value.attr.TweenType);
                if (index < 0)
                {
                    index = tweenTypes.Count;
                    tweenTypes.Add(kv.Value.attr.TweenType);
                    tweenDatas.Add(new List <TweenerTypeData>());
                }
                tweenDatas[index].Add(kv.Value);
                Type componentType = kv.Value.generics[0];
                if (!tt.Equals(componentType) && !components.Contains(componentType))
                {
                    components.Add(componentType);
                }
            }
            int sizeComponents = components.Count;
            Dictionary <string, int> tweenTypeEnum = new Dictionary <string, int>();
            StringBuilder            code          = new StringBuilder();

            foreach (string ns in nss)
            {
                if (ns == tns)
                {
                    continue;
                }
                code.AppendLine(string.Format("using {0};", ns));
            }
            code.AppendLine();
            code.AppendLine(string.Format("namespace {0} {{", ttc.Namespace));
            code.AppendLine();
            code.AppendLine("\tpublic class TweenComponent : TweenComponentBase {");
            code.AppendLine();
            int enumMax = WriteEnum(code, "\t\t", prevTweenTypeEnum, "public", "eTweenType", tweenTypes, tweenTypeEnum);

            code.AppendLine();
            if (sizeComponents > 0)
            {
                foreach (Type type in components)
                {
                    code.AppendLine(string.Format("\t\tprivate ComponentData<{0}> m{0};", type.Name));
                }
                code.AppendLine();
            }
            code.AppendLine(string.Format("\t\tprotected override int TweenTypeMax {{ get {{ return {0}; }} }}", enumMax));
            code.AppendLine();
            Type tcim = typeof(TweenerComponentCreateIfMissingAttribute);

            if (sizeComponents > 0)
            {
                code.AppendLine("\t\tprotected override void InitComponents() {");
                code.AppendLine("\t\t\tGameObject go = gameObject;");
                foreach (Type type in components)
                {
                    code.AppendLine(string.Format("\t\t\tm{0} = new ComponentData<{0}>(go, {1});",
                                                  type.Name, Attribute.GetCustomAttribute(type, tcim) != null ? "true" : "false"));
                }
                code.AppendLine("\t\t}");
            }
            else
            {
                code.AppendLine("\t\tprotected override void InitComponents() { }");
            }
            code.AppendLine();
            code.AppendLine("\t\tprotected override void PlayTweenData(TweenData tween) {");
            code.AppendLine("\t\t\tswitch ((eTweenType)tween.tweenType) {");
            Type tq         = typeof(Quaternion);
            int  typesCount = tweenTypes.Count;

            tween_type_contents = new GUIContent[typesCount];
            tween_type_values   = new int[typesCount];
            for (int i = 0; i < typesCount; i++)
            {
                string type = tweenTypes[i];
                List <TweenerTypeData> datas     = tweenDatas[i];
                TweenerTypeData        firstData = datas[0];
                int typeIndex;
                if (tweenTypeEnum.TryGetValue(type, out typeIndex))
                {
                    type_tweeners.Add(typeIndex, datas);
                    while (typeIndex >= tween_field_and_label.Count)
                    {
                        tween_field_and_label.Add(null);
                    }
                    if (tween_field_and_label[typeIndex] == null)
                    {
                        tween_field_and_label[typeIndex] = new FieldAndLabel(firstData.propertyType, new GUIContent(firstData.attr.PropertyName));
                    }
                    tween_type_contents[i] = new GUIContent(string.IsNullOrEmpty(firstData.attr.TweenCategory) ?
                                                            firstData.attr.TweenType : string.Concat(firstData.attr.TweenCategory, "/", firstData.attr.TweenType));
                    tween_type_values[i] = typeIndex;
                }
                code.AppendLine(string.Format("\t\t\t\tcase eTweenType.{0}:", type));
                for (int j = 0; j < 2; j++)
                {
                    int count = 0;
                    foreach (TweenerTypeData data in datas)
                    {
                        if (j == 0 ^ tt.Equals(data.generics[0]))
                        {
                            continue;
                        }
                        string valueProp = null;
                        switch (data.propertyType)
                        {
                        case ePropertyType.Float:
                            valueProp = "tween.floatValue";
                            break;

                        case ePropertyType.Vector2:
                            valueProp = "tween.vector2Value";
                            break;

                        case ePropertyType.Vector3:
                            valueProp = tq.Equals(data.generics[1]) ? "Quaternion.Euler(tween.vector3Value)" : "tween.vector3Value";
                            break;

                        case ePropertyType.Color:
                            valueProp = "tween.colorValue";
                            break;
                        }
                        string component = "transform";
                        if (j > 0)
                        {
                            component = string.Format("m{0}.component", data.generics[0].Name);
                            code.AppendLine(string.Format("\t\t\t\t\t{0}if ({1} != null) {{",
                                                          count == 0 ? "" : "} else ", component));
                        }
                        code.AppendLine(string.Format("\t\t\t\t\t{0}PlayTweener<{1}, {2}, {3}, {4}>(tween, {5}, {6});",
                                                      j == 0 ? "" : "\t", data.type.Name, data.generics[0].Name, GetTypeName(data.generics[1]),
                                                      data.generics[2].Name, component, valueProp));
                        count++;
                    }
                    if (j > 0 && count > 0)
                    {
                        code.AppendLine("\t\t\t\t\t}");
                    }
                }
                code.AppendLine("\t\t\t\t\tbreak;");
            }
            code.AppendLine("\t\t\t}");
            code.AppendLine("\t\t}");
            code.AppendLine();
            code.AppendLine("\t}");
            code.AppendLine();
            code.AppendLine("}");
            string codeContent = code.ToString();

            if (!File.Exists(scriptPath) || File.ReadAllText(scriptPath, Encoding.UTF8) != codeContent)
            {
                File.WriteAllText(scriptPath, code.ToString());
                AssetDatabase.Refresh();
            }
        }