[GFFrameWork] UI framework 2-UI interface auto generate template code tool

Preface

When we write the UI interface, we will find that there are such repeated steps: create the UI Prefab, create the View script of the corresponding interface, and then create the binding property fields with the UI node components, as well as some framework level interface methods. Although sometimes writing these codes doesn't need to go through the brain, and the keyboard clicks and flies, but in fact, it's a waste of time. I just want to write a tool to automatically generate UI templates, which reduces the repetition of mechanical code and saves a lot of time. We programmers should be good at creating tools to make ourselves lazy.

Effect

operation

  1. Choose tools - > autogenii

  2. Select the Prefab interface to generate

  3. Click generate to generate the desired level-1 and level-2 UI View code

Interface Prefab requirements

Since it is automatic generation, the UI must conform to certain specifications. Here we will add @ component name after the node name of the generated property field

Effect after generation


The second level interface is generated here, so it inherits SubWindow. If it is the first level interface, it inherits WindowBase, which is also the inherited WindowBase.

Code

AutoGenUI.CS
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class AutoGenUI : EditorWindow
{
    [MenuItem("Tools/AutoGenUI")]
    static void Audo_Gen_UI()
    {
        EditorWindow.GetWindow<AutoGenUI>();
    }

    void OnGUI()
    {
        GUILayout.Label("Choice UI node,Automatic generation UI Script,Add after the node name of the component to be generated@Component name");
        if (GUILayout.Button("Start generating parent window"))
        {
            if (Selection.activeGameObject)
            {
                Debug.Log("Start generation");
                GenUIUtil.GenWindow(Selection.activeGameObject);
                AssetDatabase.Refresh();
                Debug.Log("Generation end");
            }
        }

        if (GUILayout.Button("Start generating child windows"))
        {
            if(Selection.activeGameObject)
            {
                Debug.Log("Start generation");
                GenUIUtil.GenSubWindow(Selection.activeGameObject);
                AssetDatabase.Refresh();
                Debug.Log("Generation end");
            }
        }

        if (Selection.activeGameObject != null)
        {
            GUILayout.Label(Selection.activeGameObject.name);
        }
    }

    void OnSelectionChange()
    {
        // When we choose to change
        this.Repaint();
    }
}

GenUIUtil.CS
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

public class GenUIUtil
{
    const string filePath = "/Code/Game@hotfix/DialGame/";

    static Dictionary<string, string> componentAbbreviation = new Dictionary<string, string>()
    {
        {"Text","txt_" },
        {"Button","btn_" },
        {"Transform","tsf_" },
        {"Image","img_" },
        {"Label","lbl_" },
        {"Input","ipt_" },
        {"InputField","ifd_"},
    };

    public static void GenWindow(GameObject selectGameObject)
    {
        GenUI(true, selectGameObject);
    }

    public static void GenSubWindow(GameObject selectGameObject)
    {
        GenUI(false, selectGameObject);
    }

    static void GenUI(bool windowOrSubWindow, GameObject selectGameObject)
    {
        string gameObjectName = selectGameObject.name;
        string fileName = string.Format("CUI{0}{1}", gameObjectName, windowOrSubWindow ? "Window" : "Widget");
        string fatherClass = windowOrSubWindow ? "WindowBase" : "SubWindow";
        StreamWriter sw = new StreamWriter(Application.dataPath + filePath + fileName + ".cs");

        sw.WriteLine("using GFFramework.UI;");
        sw.WriteLine("using UnityEngine;");
        sw.WriteLine("using UnityEngine.UI;");

        sw.WriteLine("//Create at:" + System.DateTime.Now.ToString());

        sw.WriteLine(string.Format("public class {0} : {1}", fileName, fatherClass));
        sw.WriteLine("{");

        var childs = deepFindChild(selectGameObject.transform);

        foreach (var c in childs)
        {
            var path = getRelativePath(c);
            var nameSplite = c.gameObject.name.Split('@');
            string componentName = nameSplite[1];
            string nodeName = nameSplite[0];
            sw.WriteLine(string.Format("\t[TransformPath(\"{0}\")]", path));
            sw.WriteLine(string.Format("\tprivate {0} {1};", c.gameObject.name.Split('@')[1], (componentAbbreviation[componentName] + nodeName).Replace(" ", "")));
            sw.WriteLine("\n");
        }

        //Constructor
        sw.WriteLine(string.Format("\tpublic {0}(Transform transform) : base(transform)", fileName));
        sw.WriteLine("\t{");
        sw.WriteLine("\t}");
        sw.WriteLine("\n");

        //Init
        sw.WriteLine("\tpublic override void Init()");
        sw.WriteLine("\t{");
        sw.WriteLine("\t\tbase.Init();");
        sw.WriteLine("\t}");
        sw.WriteLine("\n");

        //Close
        sw.WriteLine("\tpublic override void Close()");
        sw.WriteLine("\t{");
        sw.WriteLine("\t\tbase.Close();");
        sw.WriteLine("\t}");
        sw.WriteLine("\n");

        //Open
        sw.WriteLine("\tpublic override void Open(WindowData data = null)");
        sw.WriteLine("\t{");
        sw.WriteLine("\t\tbase.Open();");
        sw.WriteLine("\t}");
        sw.WriteLine("\n");

        //Destroy
        sw.WriteLine("\tpublic override void Destroy()");
        sw.WriteLine("\t{");
        sw.WriteLine("\t\tbase.Destroy();");
        sw.WriteLine("\t}");

        sw.WriteLine("}");
        sw.Flush();
        sw.Close();
    }

    static List<Transform> deepFindChild(Transform root)
    {
        List<Transform> collects = new List<Transform>();
        var childs = root.GetComponentsInChildren<Transform>();
        foreach (var child in childs)
        {
            if (child.name.Contains("@"))
                collects.Add(child);
        }
        return collects;
    }

    static string getRelativePath(Transform obj)
    {
        if (obj.parent.parent)
        {
            return getRelativePath(obj.parent) + "/" + obj.name;
        }
        return obj.name;
    }
}

GFFramework address

https://github.com/dingxiaowei/GFFrameWork

More tutorials

http://dingxiaowei.cn/

Tags: Windows github

Posted on Fri, 29 Nov 2019 12:08:50 -0800 by ajdegans