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(); }