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
}

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值