uForms.UFProject.ExportCode C# (CSharp) Method

ExportCode() public static method

public static ExportCode ( string codePath ) : void
codePath string
return void
        public static void ExportCode(string codePath)
        {
            string designerCodePath = codePath.Replace(".cs",".Designer.cs");

            // export the designer code always.
            {
                #region export the designer code

                CodeBuilder cb = new CodeBuilder();
                cb.WriteLine("using uForms;");
                cb.WriteLine("using UnityEngine;");
                cb.WriteLine("");
                cb.WriteLine("namespace " + current.Namespace);
                cb.WriteLine("{");
                cb.Indent++;
                cb.WriteLine("partial class " + current.ClassName);
                cb.WriteLine("{");
                cb.Indent++;
                cb.WriteLine("#region Auto generated code from uForms.");
                cb.WriteLine("");
                cb.WriteLine("private void InitializeComponent()");
                cb.WriteLine("{");
                cb.Indent++;
                current.Controls.ForEach(child =>
                {
                    cb.WriteLine("// " + child.Name);
                    child.WriteCode(cb);
                    cb.WriteLine("this.Controls.Add(this." + child.Name + ");");
                });
                cb.Indent--;
                cb.WriteLine("}");
                cb.WriteLine("");
                cb.WriteLine("#endregion");
                cb.WriteLine("");
                current.Controls.ForEach(child => child.ForTree(node => node.WriteDefinitionCode(cb)));
                cb.Indent--;
                cb.WriteLine("}");
                cb.Indent--;
                cb.WriteLine("}");
                File.WriteAllText(designerCodePath, cb.GetCode(), new UTF8Encoding(true));

                #endregion
            }

            // export the main code if it doesn't exist.
            if(!File.Exists(codePath))
            {
                #region export the main code

                CodeBuilder cb = new CodeBuilder();
                cb.WriteLine("using uForms;");
                cb.WriteLine("using UnityEngine;");
                cb.WriteLine("using UnityEditor;");
                cb.WriteLine("");
                cb.WriteLine("namespace " + current.Namespace);
                cb.WriteLine("{");
                cb.Indent++;
                cb.WriteLine("public partial class " + current.ClassName + " : UFWindow");
                cb.WriteLine("{");
                cb.Indent++;
                cb.WriteLine("[MenuItem(\"Tools/" + current.ClassName + "\")]");
                cb.WriteLine("public static void OpenWindow()");
                cb.WriteLine("{");
                cb.Indent++;
                cb.WriteLine("GetWindow<" + current.ClassName + ">(\"" + current.ClassName + "\");");
                cb.Indent--;
                cb.WriteLine("}");
                cb.WriteLine("");
                cb.WriteLine("void Awake()");
                cb.WriteLine("{");
                cb.Indent++;
                cb.WriteLine("InitializeComponent();");
                cb.Indent--;
                cb.WriteLine("}");
                cb.Indent--;
                cb.WriteLine("}");
                cb.Indent--;
                cb.WriteLine("}");
                File.WriteAllText(codePath, cb.GetCode(), new UTF8Encoding(true));

                #endregion
            }

            if(codePath.Contains("Assets/"))
            {
                codePath = codePath.Substring(codePath.IndexOf("Assets/"));
                AssetDatabase.ImportAsset(codePath);
            }
            if(designerCodePath.Contains("Assets/"))
            {
                designerCodePath = designerCodePath.Substring(designerCodePath.IndexOf("Assets/"));
                AssetDatabase.ImportAsset(designerCodePath);
            }
        }

Usage Example

Esempio n. 1
0
        void OnGUI()
        {
            GUILayout.BeginHorizontal();
            {
                if (GUILayout.Button("New Form", UIOP.Button))
                {
                    UFProject.CreateNewProject();
                    RepaintAll();
                }

                if (GUILayout.Button("Import Code", UIOP.Button))
                {
                    UFSelector.OpenWindow((t) =>
                    {
                        UFProject.ImportCode(t);
                        UFSelection.ActiveControl = null;
                        RepaintAll();
                    });
                }

                if (GUILayout.Button("Export Code", UIOP.Button))
                {
                    string path = EditorUtility.SaveFilePanel("Select the path to export", "Assets/Editor", UFProject.Current.ClassName, "cs");
                    if (!string.IsNullOrEmpty(path) && path.Contains("/Editor/"))
                    {
                        UFProject.ExportCode(path);
                    }
                }

                if (GUILayout.Button("Export Native Code", UIOP.Button))
                {
                    string path = EditorUtility.SaveFilePanel("Select the path to export", "Assets/Editor", UFProject.Current.ClassName, "cs");
                    if (!string.IsNullOrEmpty(path) && path.Contains("/Editor/"))
                    {
                        UFProject.ExportNativeCode(path);
                    }
                }

                if (GUILayout.Button("Import Xml", UIOP.Button))
                {
                    string path = EditorUtility.OpenFilePanel("Select the path to import", "", "xml");
                    if (!string.IsNullOrEmpty(path) && File.Exists(path))
                    {
                        UFProject.ImportXml(path);
                        RepaintAll();
                    }
                }

                if (GUILayout.Button("Export Xml", UIOP.Button))
                {
                    string path = EditorUtility.SaveFilePanel("Select the path to export", "", UFProject.Current.ClassName, "xml");
                    if (!string.IsNullOrEmpty(path))
                    {
                        UFProject.ExportXml(path);
                    }
                }
            }
        }