unity组件路径自动生成

unity 有时候找路径太麻烦 写了一个自动生成脚本的工具

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

public class Auto_BuildCode
{
    private static string path_dic = Application.dataPath + "/Code";
    private static string path_suffix = ".cs";
    private static string space_one = "    ";
    private static string space_two = "        ";

    private static Dictionary<string, string> compon_tranform = new Dictionary<string, string>();
    private static Dictionary<string, string> compon_image = new Dictionary<string, string>();
    private static Dictionary<string, string> compon_text = new Dictionary<string, string>();
    private static Dictionary<string, string> compon_slider = new Dictionary<string, string>();

    [MenuItem("Game/BuildCode")]
    private static void BuildCode()
    {
        Object select = Selection.activeObject;

        if (select != null && select is GameObject)
        {
            GameObject prefab = (GameObject)select;
            Get_All_Compon(prefab.transform);
            string code_name = prefab.name;
            Code_Create(code_name);
            Code_Write(code_name);
        }

        AssetDatabase.Refresh();
    }

    private static void Get_All_Compon(Transform prefab)
    {
        Transform[] components = prefab.GetComponentsInChildren<Transform>();
        for (int i = 0; i < components.Length; i++)
        {
            string compon = components[i].name.Split('_')[0];
            string compon_path = string.Empty;

            switch (compon)
            {
                case "Transform":
                    compon_path = Get_Compon_Path(prefab, components[i]);
                    compon_tranform.Add(components[i].name, compon_path);
                    break;
                case "Image":
                    compon_path = Get_Compon_Path(prefab, components[i]);
                    compon_image.Add(components[i].name, compon_path);
                    break;
                case "Text":
                    compon_path = Get_Compon_Path(prefab, components[i]);
                    compon_text.Add(components[i].name, compon_path);
                    break;
                case "Slider":
                    compon_path = Get_Compon_Path(prefab, components[i]);
                    compon_slider.Add(components[i].name, compon_path);
                    break;
                default:
                    break;
            }
        }
    }

    private static string Get_Compon_Path(Transform parent, Transform child)
    {
        StringBuilder sb = new StringBuilder();

        while (true)
        {
            if (child.parent == null)
            {
                break;
            }
            else if (child.parent != parent)
            {
                sb.Insert(0, child.name);
                sb.Insert(0, "/");
            }
            else
            {
                sb.Insert(0, child.name);
                break;
            }
            child = child.parent;
        }

        return sb.ToString();
    }

    private static void Code_Create(string code_name)
    {
        if (!Directory.Exists(path_dic))
        {
            Directory.CreateDirectory(path_dic);
        }

        string path_file = path_dic + "/" + code_name + path_suffix;

        if (File.Exists(path_file))
        {
            File.Delete(path_file);
        }
    }

    private static void Code_Write(string code_name)
    {
        string path_file = path_dic + "/" + code_name + path_suffix;

        FileStream fs = new FileStream(path_file, FileMode.OpenOrCreate);
        StreamWriter sw = new StreamWriter(fs);

        sw.WriteLine("using System;");
        sw.WriteLine("using System.Collections;");
        sw.WriteLine("using System.Collections.Generic;");
        sw.WriteLine("using UnityEngine;");
        sw.WriteLine("using UnityEngine.UI;");
        sw.WriteLine("");
        sw.WriteLine("public class " + code_name + " : MonoBehaviour");
        sw.WriteLine("{");

        #region 定义变量
        foreach (var compon in compon_tranform)
        {
            sw.WriteLine(space_one + "public Transform " + compon.Key + ";");
        }

        foreach (var compon in compon_image)
        {
            sw.WriteLine(space_one + "public Image " + compon.Key + ";");
        }

        foreach (var compon in compon_text)
        {
            sw.WriteLine(space_one + "public Text " + compon.Key + ";");
        }

        foreach (var compon in compon_slider)
        {
            sw.WriteLine(space_one + "public Slider " + compon.Key + ";");
        }
        #endregion

        sw.WriteLine(space_one + "private void Awake()");
        sw.WriteLine(space_one + "{");

        #region 对UI变量进行赋值
        foreach (var compon in compon_tranform)
        {
            sw.WriteLine(space_two + compon.Key + " = " + "transform.Find(" + '"' + compon.Value + '"' + ");");
        }

        foreach (var compon in compon_image)
        {
            sw.WriteLine(space_two + compon.Key + " = " + "transform.Find(" + '"' + compon.Value + '"' + ").GetComponent<Image>();");
        }

        foreach (var compon in compon_text)
        {
            sw.WriteLine(space_two + compon.Key + " = " + "transform.Find(" + '"' + compon.Value + '"' + ").GetComponent<Text>();");
        }

        foreach (var compon in compon_slider)
        {
            sw.WriteLine(space_two + compon.Key + " = " + "transform.Find(" + '"' + compon.Value + '"' + ").GetComponent<Slider>();");
        }
        #endregion

        sw.WriteLine(space_one + "}");
        sw.WriteLine("}");

        sw.Dispose();
        fs.Dispose();
    }
}
View Code
原文地址:https://www.cnblogs.com/Joke-crazy/p/10299615.html