Ejemplo n.º 1
0
        private static void CreateScriptFile()
        {
            bool   willWrite = false;
            string path      = Path.Combine(Directory, string.Format("{0}.cs", PainterName.RemoveSpecialCharacters()));

            if (File.Exists(path))
            {
                if (EditorUtility.DisplayDialog(
                        "File existed",
                        "The file name is existed in the current directory, overwrite?",
                        "Yes", "No"))
                {
                    willWrite = true;
                }
            }
            else
            {
                willWrite = true;
            }
            if (!willWrite)
            {
                return;
            }

            string[] text = GenerateCode();

            File.WriteAllLines(path, text);
            AssetDatabase.Refresh(); //import the file

            Object asset = AssetDatabase.LoadAssetAtPath <Object>(FileUtil.GetProjectRelativePath(path));

            Selection.activeObject = asset;
            EditorGUIUtility.PingObject(asset);
        }
Ejemplo n.º 2
0
        private static string[] GenerateCode()
        {
            Type painterInterface =
                PainterType == PAINTER_TYPE_GEOMETRY_TEXTURE ? typeof(IGTexturePainter) :
                PainterType == PAINTER_TYPE_FOLIAGE ? typeof(IGFoliagePainter) :
                PainterType == PAINTER_TYPE_OBJECT ? typeof(IGObjectPainter) :
                null;
            //Declare it
            TypeDeclaration d = new TypeDeclaration.Builder()
                                .SetAccessModifier(AccessModifier.PUBLIC)
                                .SetIsStatic(false)
                                .SetInheritance(Inheritance.DEFAULT)
                                .SetScope(Scope.CLASS)
                                .SetName(PainterName.RemoveSpecialCharacters())
                                .SetInterfaces(painterInterface)
                                .Build();

            //Import regularly used namespaces, just call a Using(...) if you need more
            UsingNode un = new UsingNode()
                           .Using("UnityEngine")
                           .Using("System.Collections")
                           .Using("System.Collections.Generic")
                           .Using("Pinwheel.Griffin")
                           .Using("Pinwheel.Griffin.PaintTool");

            //Import the base class namespace to simplify its name in the declaration string
            if (d.baseClass != null)
            {
                un.Using(d.baseClass.Namespace);
            }

            //The same for interfaces
            if (d.implementedInterface.HasElement())
            {
                for (int i = 0; i < d.implementedInterface.Count; ++i)
                {
                    un.Using(d.implementedInterface[i].Namespace);
                }
            }

            //Give it a hint to determine type names
            TypeAlias.SetUsingNode(un);

            List <string> code = new List <string>();

            //Implement the type
            TypeNode tn = new TypeNode()
                          .Declare(d)
                          .Implement(code);

            //Put the new type into the namespace
            //NamespaceNode nn = new NamespaceNode()
            //    .SetName("CustomPainter")
            //    .SetTypes(tn);

            //Put everything into a script
            ScriptNode sn = new ScriptNode()
                            .SetCompilationDirective("GRIFFIN")
                            .SetUsing(un)
                                           //.SetNamespace(nn)
                            .SetTypes(tn); //also set type in case namespace is empty

            //Now make the code look awesome
            CodeFormatter formatter = new CodeFormatter();

            formatter.addSpaceAfterUsings    = true;
            formatter.addSpaceBetweenMethods = true;
            formatter.bracketStyle           = CodeFormatter.OpenCurlyBracketStyle.NewLine;

            string[] script = formatter.Format(sn);

            return(script); //phew...
        }