unity自动引用生成

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;


/// <summary>
/// 模板脚本生成
/// </summary>
public class ScriptCreater : EditorWindow
{
    #region 生成CS脚本引用
    //按照前缀命名的就会自动生成引用
    private static Dictionary<string, string> compDic = new Dictionary<string, string>
    {
        {"rect_", "RectTransform"},
        {"btn_", "Button"},
        { "text_","Text"},
         {"wImg_", "Image"},        
    };
    private static Dictionary<string, string[]> nameOfPath = new Dictionary<string, string[]>();
    private static string GenerateClassName;//类名
    private static void GenerateMainClass()
    {
        string mainPath = $"{Application.dataPath}/Scripts/Example/{GenerateClassName}.cs";
        if (!File.Exists(mainPath))
        {
            File.Create(mainPath).Close();
        }
#if UNITY_EDITOR
        UnityEditor.AssetDatabase.Refresh();
#endif
        File.WriteAllText(mainPath,
            @"using UnityEngine;
using System;
using UnityEngine.UI;
public partial class _D_CLASSNAME 
{
 
    protected override void OnCreate()
    {
                       
    }
   
}
".Replace("_D_CLASSNAME", GenerateClassName)
        );
#if UNITY_EDITOR
        UnityEditor.AssetDatabase.Refresh();
#endif
        GeneratePartClass();

    }

    private static void GeneratePartClass()
    {

        string fielddata = "";
        string referdata = "";

        foreach (var item in nameOfPath)
        {

            fielddata += $"\tprivate {item.Value[1]} {item.Key};\n";
            referdata += $"\t\t{item.Key} = transform.Find(\"{ item.Value[0] }\").GetComponent<{item.Value[1]}>();\n";

        }
        referdata += "\t\tAfterInitRef();";

        string baseTemp = @"using System;
using UnityEngine;
using UnityEngine.UI;

public partial class _D_CLASSNAME 
{
FILEDLIST
    protected override void InitComp()
    {
REFERENCE
    }
}
";
        string info = "";
        info = baseTemp.Replace("FILEDLIST", fielddata);
        info = info.Replace("_D_CLASSNAME", GenerateClassName);
        info = info.Replace("REFERENCE", referdata);
        string aprtPath = $"{Application.dataPath}/Scripts/Game/Runtime/Logic/Dialog/Partial/{GenerateClassName}.cs";
        if (!File.Exists(aprtPath))
        {
            File.CreateText(aprtPath).Close();
#if UNITY_EDITOR
            UnityEditor.AssetDatabase.Refresh();
#endif
        }

        File.WriteAllText(aprtPath, info);
#if UNITY_EDITOR
        UnityEditor.AssetDatabase.Refresh();
#endif
    }


    [MenuItem("GameObject/UI组件引用生成", false, 1)]
    [MenuItem("Assets/UI组件引用生成")]
    public static void UGUIPrefabGenerateCS()
    {
        nameOfPath.Clear();
        UnityEngine.Object obj = Selection.activeObject;
        if (obj == null)
        {
            Debug.LogError("请选择预设");
            return;
        }
        GameObject prefab = obj as GameObject;
        List<string> types = new List<string>();
        string baseName = prefab.name;
        GenerateClassName = baseName;
        foreach (Transform item in prefab.GetComponentsInChildren<Transform>(true))
        {
            Debug.Log(item.name);
            foreach (var v in compDic)
            {
                if (item.name.StartsWith(v.Key))
                {
                    string fieldName = "comp_" + item.name;
                    string path = "";
                    var curgo = item;
                    types.Clear();
                    while (curgo != null)
                    {
                        if (curgo.parent != null) types.Add(curgo.name);
                        curgo = curgo.parent;
                    }
                    for (int i = types.Count - 1; i >= 0; i--)
                    {
                        path += (types[i] + (i != 0 ? "/" : ""));
                    }
                    nameOfPath.Add(fieldName, new string[] { path, v.Value });

                }
            }

        }
        Debug.Log($"nameOfPath.Count = {nameOfPath.Count}");
        GenerateMainClass();

    }
    #endregion
}

思路:按照规范命名的才会生成引用

相关推荐

  1. unity自动引用生成

    2024-03-22 16:02:03       20 阅读
  2. flutter 自动生成静态资源的引用

    2024-03-22 16:02:03       11 阅读
  3. Unity 地图数据生成

    2024-03-22 16:02:03       22 阅读
  4. mysql 自动生成随机数

    2024-03-22 16:02:03       36 阅读
  5. MybatisPlus自动生成代码

    2024-03-22 16:02:03       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-22 16:02:03       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-22 16:02:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-22 16:02:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-22 16:02:03       20 阅读

热门阅读

  1. ChatGPT:如何利用人工智能写出高质量论文

    2024-03-22 16:02:03       21 阅读
  2. LeetCode刷题——347. 前 K 个高频元素

    2024-03-22 16:02:03       24 阅读
  3. 下载NLP_gluedata数据集的脚本

    2024-03-22 16:02:03       18 阅读
  4. 类似于 FastAdmin的快速后台开发框架都有哪些

    2024-03-22 16:02:03       20 阅读
  5. k8s工作节点主要模块

    2024-03-22 16:02:03       21 阅读
  6. 大数据开发(HBase真题)

    2024-03-22 16:02:03       17 阅读
  7. Puppet 2024年度报告:平台工程发掘 DevOps 无限潜质

    2024-03-22 16:02:03       19 阅读
  8. 后台发送GET/POST方法

    2024-03-22 16:02:03       16 阅读
  9. Qt Excel文件读写

    2024-03-22 16:02:03       21 阅读
  10. 9. Linux 信号详解

    2024-03-22 16:02:03       21 阅读
  11. 在Linux/Ubuntu/Debian中创建自己的命令快捷方式

    2024-03-22 16:02:03       21 阅读