Пример #1
0
        protected virtual void CreateScript(string scriptName)
        {
            scriptName = scriptName.Replace(" ", "_");
            scriptName = scriptName.Replace("-", "_");
            string path = "Assets/" + scriptName + ".cs";

            if (File.Exists(path) == false)
            {
                using (StreamWriter outfile = new StreamWriter(path))
                {
                    MethodInfo[] methods = elementType.GetAllMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                    methods = methods.Where(x => x.IsAbstract).ToArray();

                    outfile.WriteLine("using UnityEngine;");
                    outfile.WriteLine("using System.Collections;");
                    outfile.WriteLine("using " + elementType.Namespace + ";");
                    outfile.WriteLine("");
                    if (!typeof(Component).IsAssignableFrom(elementType))
                    {
                        outfile.WriteLine("[System.Serializable]");
                    }
                    outfile.WriteLine("public class " + scriptName + " : " + elementType.Name + "{");
                    for (int i = 0; i < methods.Length; i++)
                    {
                        MethodInfo      method          = methods[i];
                        ParameterInfo[] parameters      = method.GetParameters();
                        string          parameterString = string.Empty;
                        for (int j = 0; j < parameters.Length; j++)
                        {
                            string typeName      = parameters[j].ParameterType.Name;
                            string parameterName = string.Empty;
                            if (Char.IsLower(typeName, 0))
                            {
                                parameterName = "_" + typeName;
                            }
                            else
                            {
                                parameterName = char.ToLowerInvariant(typeName[0]) + typeName.Substring(1);
                            }
                            parameterString += ", " + typeName + " " + parameterName;
                        }

                        if (!string.IsNullOrEmpty(parameterString))
                        {
                            parameterString = parameterString.Substring(1);
                        }

                        outfile.WriteLine("\t" + (method.IsPublic?"public":"protected") + " override " + UnityEditorUtility.CovertToAliasString(method.ReturnType) + " " + method.Name + "(" + parameterString + ") {");

                        if (method.ReturnType == typeof(string))
                        {
                            outfile.WriteLine("\t\treturn string.Empty;");
                        }
                        else if (method.ReturnType == typeof(bool))
                        {
                            outfile.WriteLine("\t\treturn true;");
                        }
                        else if (method.ReturnType == typeof(Vector2))
                        {
                            outfile.WriteLine("\t\treturn Vector2.zero;");
                        }
                        else if (method.ReturnType == typeof(Vector3))
                        {
                            outfile.WriteLine("\t\treturn Vector3.zero;");
                        }
                        else if (!method.ReturnType.IsValueType || method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            outfile.WriteLine("\t\treturn null;");
                        }
                        else if (UnityUtility.IsInteger(method.ReturnType))
                        {
                            outfile.WriteLine("\t\treturn 0;");
                        }
                        else if (UnityUtility.IsFloat(method.ReturnType))
                        {
                            outfile.WriteLine("\t\treturn 0.0f;");
                        }
                        else if (method.ReturnType.IsEnum)
                        {
                            outfile.WriteLine("\t\treturn " + method.ReturnType.Name + "." + Enum.GetNames(method.ReturnType)[0] + ";");
                        }

                        outfile.WriteLine("\t}");
                        outfile.WriteLine("");
                    }
                    outfile.WriteLine("}");
                }
            }
            AssetDatabase.Refresh();
            EditorPrefs.SetString("NewScriptToCreate", scriptName);
            EditorPrefs.SetInt("AssetWindowID", GetInstanceID());
        }
Пример #2
0
 /// <summary>
 /// Colors the string.
 /// </summary>
 /// <returns>The colored string.</returns>
 /// <param name="value">Value.</param>
 /// <param name="color">Color.</param>
 public static string ColorString(string value, Color color)
 {
     return("<color=#" + UnityUtility.ColorToHex(color) + ">" + value + "</color>");
 }