Unity—自動化代碼生成


自動化代碼生成


一.前言

由於之前寫過關於UI框架的文章,這篇基於之前的基礎,添加了自動生成代碼的功能;

如果學習過程有困惑可以跳轉到之前的文章《Unity——基於UGUI的UI框架》;

二.效果展示

三.打包UIFrame

我使用的是Rider編輯器,用其他的也可以;

目的就是將之前寫好的UIFrame框架的幾個公共類打包成dll供調用,只有打包成dll才可以在Plugins和Scripts文件夾中都能使用;

操作步驟如下:

1.創建新的classLibrary解決方案:

2.導入寫好的類,目前只需要到這以下這三個類即可;

這幾個類要添加統一的命名空間,在其他項目使用只需要using這個命名空間即可;

3.點擊BuildSolution打包;

由於這幾個類繼承了MonoBehaviour,需要添加UnityEngine依賴,根據錯誤提示添加;

另外.Net的版本過高也會導致打包出錯,修改一下版本即可;

四.自動生成代碼

主要方法類UGUITool;由於是編輯器拓展功能,必須放在plugins文件夾下;

字段:

uiDirPath:所有面板加載路徑;

classPath:自動生成的代碼存放路徑;

classTemp:自動化代碼模板字符串;

方法:

CreateCode():核心方法;

思路:

1.根據面板加載路徑,讀取所有面板的prefab信息;

2.根據預制體的名稱,自動生成@Init和@UIType部分的代碼字符串;

3.字符串全部生成完成后替換對應部分,保存在存放路徑;

注意:

我這里所有面板都是放在Resources中的,如果正式項目肯定不會Resources加載,所以需要做SteamingAssets路徑判斷;

代碼部分:

public class UGUITool
{
    public static string uiDirPath = "Resources/Prefabs/UIPanel/";
    public static string classPath = "Scripts/UIFrame/UIManagerA.cs";

    private static string classTemp =
        @"
        using System.Collections;
        using System.Collections.Generic;
        using UnityEngine;
        using UnityEngine.UI;
        using UIFrame;
        public partial class UIManager : MonoSingle<UIManager>
        {
            private void InitPath()
            {
                dicPath = new Dictionary<string, string>();     
                //@Init
            }
        }
        
        public class UIType
        {
            //@UIType
        }
";

    [MenuItem("LittlePerilla/UGUI/CreateCode")]
    public static void CreateCode()
    {
        //Resources可以替換
        string path = Path.Combine(Application.dataPath, uiDirPath);
        Debug.Log("生成代碼" + path);
        
        StringBuilder pt = new StringBuilder();
        StringBuilder tp = new StringBuilder();
        GameObject go = null;
        string[] fileList = Directory.GetFiles(path, "*.prefab", SearchOption.AllDirectories);
        int index = 0;

        string s = "Resources";
        foreach (var filePath in fileList)
        {
            string newPath = string.Empty;
            newPath = filePath.Replace("\\", "/");
            if (newPath.Contains(s))
            {
                newPath = newPath.Substring(newPath.IndexOf(s) + s.Length + 1);
                newPath = newPath.Substring(0, newPath.IndexOf("."));
                go = Resources.Load<GameObject>(newPath);
            }
            else
            {
                //newPath = newPath.Substring(0, newPath.IndexOf("."));            //其他路徑必須加后綴名
                go = AssetDatabase.LoadAssetAtPath<GameObject>(newPath) as GameObject;
            }

            if (go == null)
            {
                Debug.LogError(newPath);
                continue;
            }

            if (go.GetComponent<UIBase>())
            {
                string name = go.name.ToLower();
                string uiName = $"UI{name.Substring(3, 1).ToUpper()}{name.Substring(4, name.Length-4)}";
                pt.AppendLine($"dicPath[\"{name}\"] = \"{newPath}\";");
                tp.AppendLine($"public const string {uiName} = \"{name}\";");
                index++;
            }
        }

        string codeClass = classTemp.Replace("//@Init", pt.ToString());
        codeClass = codeClass.Replace("//@UIType", tp.ToString());

        path = Path.Combine(Application.dataPath, classPath);
        File.WriteAllText(path, codeClass, Encoding.UTF8);
        Debug.Log($"{codeClass}生成完畢");
        AssetDatabase.Refresh();
    }
}

五.使用須知

路徑必須設置正常,並且之后所有UI面板都必須放在對應的文件夾下才會自動生成對應的代碼;

自動化代碼是提高工作效率的重要手段,如果有修改意見請與作者聯系;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM