Esempio n. 1
0
        private static void CreateNodeScript(string name, Playflock.Log.NodeType nodeType)
        {
            var unit = new CodeCompileUnit();
            //add namespace
            var importsNamespace = new CodeNamespace
            {
                Imports =
                {
                    new CodeNamespaceImport("UnityEngine"),
                    new CodeNamespaceImport("UnityEngine.UI"),
                    new CodeNamespaceImport("System.Collections")
                }
            };
            var playflockNamespace = new CodeNamespace("Playflock.Log.Node");

            unit.Namespaces.Add(importsNamespace);
            unit.Namespaces.Add(playflockNamespace);
            // add class
            var declarationClass = new CodeTypeDeclaration(name + "Node");

            declarationClass.IsClass = true;
            //inherits from
            declarationClass.BaseTypes.Add("HUDNode");
            //Methods
            var baseMethod = new CodeMemberMethod();

            switch (nodeType)
            {
            case Playflock.Log.NodeType.Toggle:
                baseMethod.Name = "OnToggle";
                baseMethod.Parameters.Add(new CodeParameterDeclarationExpression("System.Boolean", "isOn"));
                baseMethod.Attributes = MemberAttributes.Override | MemberAttributes.Public;
                baseMethod.Statements.Add(new CodeSnippetStatement("\t\t\tbase.OnToggle( isOn );"));
                break;

            case Playflock.Log.NodeType.Button:
                baseMethod.Name       = "OnClick";
                baseMethod.Attributes = MemberAttributes.Override | MemberAttributes.Public;
                baseMethod.Statements.Add(new CodeSnippetStatement("\t\t\tbase.OnClick();"));
                break;
            }
            //Methods
            declarationClass.Members.Add(baseMethod);
            playflockNamespace.Types.Add(declarationClass);

            var provider   = (CSharpCodeProvider)CodeDomProvider.CreateProvider("csharp");
            var scriptPath = _rootPath + _nodeFolder + "/" + name + "Node.cs";

            using (var sw = new StreamWriter(scriptPath, false))
            {
                var tw = new IndentedTextWriter(sw, "    ");
                //generate source code
                var options = new CodeGeneratorOptions();
                options.BracingStyle             = "C";
                options.BlankLinesBetweenMembers = true;
                provider.GenerateCodeFromCompileUnit(unit, tw, options);
                tw.Close();
                sw.Close();
            }

            //delete autogenerated summary (optional)
            using (var Reader = new StreamReader(scriptPath))
            {
                var fileContent = Reader.ReadToEnd();

                var sb = new StringBuilder(fileContent);
                sb.Remove(0, 420);
                Reader.Close();
                using (var streamWriter = new StreamWriter(scriptPath))
                {
                    streamWriter.Write(sb);
                    streamWriter.Close();
                }
            }
            //Debug.Log("HUD node script generation completed");
        }
Esempio n. 2
0
        void OnGUI()
        {
            if (!isEditorBusy)
            {
                var isRootPathEmpty = String.IsNullOrEmpty(_rootPath);
                if (isRootPathEmpty)
                {
                    _rootPath = EditorPrefs.GetString("rootPath");
                    EditorGUILayout.HelpBox("Please select root directory of HUDDebug", MessageType.Error);
                }
                GUILayout.BeginHorizontal();
                GUILayout.Label("Root path : " + _rootPath);
                if (GUILayout.Button("Select folder"))
                {
                    var path = EditorUtility.OpenFolderPanel("Select HUDDebug folder", "Assets", "");
                    EditorPrefs.SetString("rootPath", path);
                    _rootPath = path;
                }
                GUILayout.EndHorizontal();
                GUILayout.Space(50f);
                GUILayout.Label("Node name:", GUILayout.MaxWidth(100f));
                _nodeName = EditorPrefs.GetString("nodeName");
                _nodeName = GUILayout.TextField(_nodeName, GUILayout.MaxWidth(400f));
                EditorPrefs.SetString("nodeName", _nodeName);
                GUILayout.Space(10f);
                GUILayout.Label("Implementation type:", GUILayout.MaxWidth(150f));
                _nodeType =
                    (Playflock.Log.NodeType)EditorGUILayout.EnumPopup("", _nodeType, GUILayout.MaxWidth(200f));
                GUILayout.Space(10f);
                GUILayout.Label("Widget name:", GUILayout.MaxWidth(100f));
                _widgetName = EditorPrefs.GetString("widgetName");
                _widgetName = GUILayout.TextField(_widgetName, GUILayout.MaxWidth(400f));
                EditorPrefs.SetString("widgetName", _widgetName);
                _templateWidget =
                    (GameObject)
                    EditorGUILayout.ObjectField("From template:", _templateWidget, typeof(GameObject), true);
                GUILayout.Space(10f);
                GUILayout.BeginHorizontal();
                GUILayout.Label("Members:", GUILayout.MaxWidth(100f));
                if (GUILayout.Button("+", GUILayout.MaxWidth(30f)))
                {
                    //add members
                    Member member = new Member();
                    member.IsSerialized   = false;
                    member.Attribute      = MemberAttributes.Public;
                    member.NamespaceType  = NamespaceType.System;
                    member.Name           = "";
                    member.SystemTypeCode = TypeCode.Boolean;
                    member.UeTypeCode     = UeTypeCode.Color;
                    member.UeUiTypeCode   = UeUiTypeCode.Text;
                    _widgetMembers.Add(member);
                }
                GUILayout.EndHorizontal();
                GUILayout.Space(10f);
                _membersScrollViewPos = GUILayout.BeginScrollView(_membersScrollViewPos);
                for (int i = 0; i < _widgetMembers.Count; i++)
                {
                    GUILayout.BeginHorizontal();
                    _widgetMembers[i].Attribute =
                        (MemberAttributes)
                        EditorGUILayout.EnumPopup("", _widgetMembers[i].Attribute, GUILayout.MaxWidth(200f));
                    _widgetMembers[i].NamespaceType =
                        (NamespaceType)
                        EditorGUILayout.EnumPopup("", _widgetMembers[i].NamespaceType, GUILayout.MaxWidth(200f));
                    switch (_widgetMembers[i].NamespaceType)
                    {
                    case NamespaceType.System:
                        _widgetMembers[i].SystemTypeCode =
                            (System.TypeCode)
                            EditorGUILayout.EnumPopup("", _widgetMembers[i].SystemTypeCode,
                                                      GUILayout.MaxWidth(200f));
                        break;

                    case NamespaceType.UnityEngine:
                        _widgetMembers[i].UeTypeCode =
                            (UeTypeCode)
                            EditorGUILayout.EnumPopup("", _widgetMembers[i].UeTypeCode, GUILayout.MaxWidth(200f));
                        break;

                    case NamespaceType.UnityEngineUI:
                        _widgetMembers[i].UeUiTypeCode =
                            (UeUiTypeCode)
                            EditorGUILayout.EnumPopup("", _widgetMembers[i].UeUiTypeCode,
                                                      GUILayout.MaxWidth(200f));
                        break;
                    }
                    GUILayout.Label("Name : ", GUILayout.MaxWidth(40f));
                    _widgetMembers[i].Name         = GUILayout.TextArea(_widgetMembers[i].Name);
                    _widgetMembers[i].IsSerialized = EditorGUILayout.Toggle(_widgetMembers[i].IsSerialized,
                                                                            GUILayout.MaxWidth(10f));
                    GUILayout.Label("IsSerialized", GUILayout.MaxWidth(80f));
                    if (GUILayout.Button("-", GUILayout.MaxWidth(30f)))
                    {
                        _widgetMembers.RemoveAt(i);
                    }
                    GUILayout.EndHorizontal();
                    GUILayout.Space(10f);
                }
                GUILayout.EndScrollView();
            }
            if (!isEditorBusy && IsCreateAvailable())
            {
                if (GUILayout.Button("Create HUD"))
                {
                    if (File.Exists(_rootPath + _nodeFolder + "/" + EditorPrefs.GetString("nodeName") + "Node.cs"))
                    {
                        if (
                            !EditorUtility.DisplayDialog("HUD node",
                                                         "File with same name already exists, replace it?", "Replace",
                                                         "Cancel"))
                        {
                            return;
                        }
                    }

                    if (File.Exists(_rootPath + _widgetFolder + "/" + EditorPrefs.GetString("widgetName") + "Widget.cs"))
                    {
                        if (
                            !EditorUtility.DisplayDialog("HUD widget",
                                                         "File with same name already exists, replace it?", "Replace",
                                                         "Cancel"))
                        {
                            return;
                        }
                    }

                    Debug.Log("Creating HUD...");

                    if (!string.IsNullOrEmpty(EditorPrefs.GetString("nodeName")))
                    {
                        CreateNodeScript(EditorPrefs.GetString("nodeName"), _nodeType);
                        var nodeScriptPath = _rootPath + _nodeFolder + "/" + EditorPrefs.GetString("nodeName") +
                                             "Node.cs";
                        Debug.Log(nodeScriptPath);
                        AssetDatabase.Refresh();
                        EditorPrefs.SetBool("isCreatingNode", true);
                    }

                    if (!string.IsNullOrEmpty(EditorPrefs.GetString("widgetName")))
                    {
                        CreateWidgetScript(EditorPrefs.GetString("widgetName"));
                        var widgetScriptPath = _rootPath + _widgetFolder + "/" + EditorPrefs.GetString("widgetName") +
                                               "Widget.cs";
                        Debug.Log(widgetScriptPath);
                        AssetDatabase.Refresh();
                        EditorPrefs.SetBool("isCreatingWidget", true);
                    }
                }
            }
            else if (!IsCreateAvailable())
            {
                EditorGUILayout.HelpBox("Please fill at least one of the fields (Node or Widget)", MessageType.Warning);
            }
            else
            {
                EditorGUILayout.HelpBox("Please wait...", MessageType.Info);
            }
            Repaint();
        }