Example #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();
                    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 " + EditorTools.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 (UnityTools.IsInteger(method.ReturnType))
                        {
                            outfile.WriteLine("\t\treturn 0;");
                        }
                        else if (UnityTools.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());
        }