Unity編輯器工具_一鍵生成/修改預制體


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

public class Tool
{
    static Dictionary<int, Table_item> items_ID = TableManager.Getitem();
    static Dictionary<string, Table_item> items_Name = new Dictionary<string, Table_item>();

    [MenuItem("Menu/一鍵導入道具信息", false, 1)]
    public static void 一鍵導入道具信息()
    {
        //---------------------表格初始化,並讀取到字典中---------------------------//
        //此處使用的自己的文件讀取方式,可以按各自的讀取方式進行修改
        TableManager.Initialize();
        items_ID = TableManager.Getitem();
        items_Name.Clear();
        foreach (var item in items_ID)
        {
            items_Name.Add(item.Value.EnName, item.Value);
        }

        //---------------------創建文件夾和預制體(一定要優先創建,之后好做關聯)---------------------------//
        foreach (var item in items_Name)
        {
            string path = "Assets/Resources/" + item.Value.FileName;
            string filePath = path + "/" + item.Value.EnName + ".Prefab";

            //判斷預制體所在的文件夾是否存在,若不存在則創建
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            //當前預制體
            GameObject Target = AssetDatabase.LoadAssetAtPath(filePath, typeof(GameObject)) as GameObject;
                    //預制體不存在
            if (!Target)
            {
                GameObject newGameObject = new GameObject();
                
                //新建預制體的處理,例如添加腳本,數據初始化,可以才是用switch來分別對不同的預制體添加不同的腳本

                PrefabUtility.SaveAsPrefabAsset(newGameObject, filePath);
                GameObject.DestroyImmediate(newGameObject);
            }
                   else    
                   {
                       Target = PrefabUtility.InstantiatePrefab(Target) as GameObject;

                        //此處進行預制體的修改,依舊使用Switch來獲取對應腳本來進行數據替換
                        
                        PrefabUtility.SaveAsPrefabAsset(Target, filePath);
                        GameObject.DestroyImmediate(Target);
                   }
        }
        //保存修改
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }
 
  

  


免責聲明!

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



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