Unity 中 Prefab(预制体)的创建工具

Unity 中 Prefab(预制体)的创建工具

欢迎转载,但请附上本文链接。

很多时候会用到 Prefab,因为这个的确使用方便,废话不多说,直接上代码(放置在Editor目录中):

using UnityEngine;
using UnityEditor;
using System;
using UnityEditor.SceneManagement;
using System.IO;


namespace PrefabsBuilder
{

    public class PrefabsBuild : EditorWindow
    {
        static string m_OutputPath = "Assets/MyTools/PrefabsBuilder/Prefabs";  //需要静态
        static string m_OutputName = "";
        static GameObject m_NeedCreatedGameObject;
        static GameObject m_LastNeedCreatedGameObject;


        //利用构造函数来设置窗口名称
        PrefabsBuild()
        {
            this.titleContent = new GUIContent("Prefabs Build");
        }

        //添加菜单栏用于打开窗口
        [MenuItem("MyTools/Prefabs Build")]
        static void showWindow()
        {
            EditorWindow.GetWindow(typeof(PrefabsBuild));
        }


        void OnGUI()
        {
            GUILayout.BeginVertical();

            //绘制标题
            GUILayout.Space(10);
            GUI.skin.label.fontSize = 16;
            GUI.skin.label.alignment = TextAnchor.MiddleCenter;
            GUILayout.Label("Prefabs Build");


            GUI.skin.label.fontSize = 11;
            GUI.skin.label.alignment = TextAnchor.UpperLeft;


            //绘制当前时间
            //GUILayout.Space(10);
            //GUILayout.Label("Time: " + System.DateTime.Now);

            //绘制当前正在编辑的场景
            GUILayout.Space(10);
            GUILayout.Label("Current Scene: " + EditorSceneManager.GetActiveScene().name);

            //绘制对象
            GUILayout.Space(10);
            m_NeedCreatedGameObject = (GameObject)EditorGUILayout.ObjectField("Source Object", m_NeedCreatedGameObject, typeof(GameObject), true);

            //绘制Prefabs存储路径输入区域
            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            GUILayout.Label("Output Path", GUILayout.MaxWidth(80));
            m_OutputPath = EditorGUILayout.TextArea(m_OutputPath, GUILayout.MaxHeight(32));
            GUILayout.EndHorizontal();


            //绘制Prefabs文件名输入区域
            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            GUILayout.Label("Output Name", GUILayout.MaxWidth(80));
            if (m_NeedCreatedGameObject != null && (m_OutputName == "" || m_NeedCreatedGameObject != m_LastNeedCreatedGameObject))
            {
                m_LastNeedCreatedGameObject = m_NeedCreatedGameObject;
                string date = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString("00") + System.DateTime.Now.Day.ToString("00")
                    + System.DateTime.Now.Hour.ToString("00") + System.DateTime.Now.Minute.ToString("00") + System.DateTime.Now.Second.ToString("00");
                m_OutputName = m_NeedCreatedGameObject.name + "_" + date;
            }
            m_OutputName = EditorGUILayout.TextArea(m_OutputName, GUILayout.MaxHeight(16));
            GUILayout.EndHorizontal();

            EditorGUILayout.Space();

            //添加名为"Build"按钮,用于调用Build()函数
            if (GUILayout.Button("Build"))
            {
                Build();
            }

            GUILayout.EndVertical();
        }

        //用于保存当前信息
        void Build()
        {
            if(m_NeedCreatedGameObject == null) { Debug.LogWarning("Null Need Created GameObject!"); return; }

            try
            {
                string outpath = m_OutputPath.Replace("\\", "/"); //"\"和"\\"都会被转为"/".

                bool isSuccessed = false;
                string[] name = m_OutputName.Split('(');
                string path = outpath + "/" + name[0] + "_vab.prefab";
                //如果路径下已经存在该名字的prefab,则覆盖
                //UnityEditor.PrefabUtility.SaveAsPrefabAsset(m_NeedCreatedGameObject, path, out isSuccessed); //Unity2018.3及以上版本方法
                if (UnityEditor.PrefabUtility.CreatePrefab(path, m_NeedCreatedGameObject) != null) { isSuccessed = true; }

                if (isSuccessed) { Debug.LogWarning("Create Prefab Successed!"); }
                else { Debug.LogWarning("Create Prefab Failed!"); }
            }
            catch(Exception e) { Debug.LogWarning("Create Prefab Failed!\r\n" + e.Message); }
        }

    }//end class
}//end namespace

版权声明:本文为blueskynight原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。