using LitJson; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using UnityEditor; using UnityEngine; using UnityEngine.SceneManagement; public class CustomUtils : Editor { [MenuItem("Custom/保存场景内所有已赋值的工艺模型")] public static void SaveWorkShipInfo() { //查找本地文件中的工艺信息并存储 List workShips = new List(); JsonData data = JsonMapper.ToObject(File.ReadAllText(Application.streamingAssetsPath + "/Processing/WorkShipInfo.json")); foreach (JsonData item in data) { WorkShip workShip = new WorkShip(); workShip.SceneName = item["SceneName"].ToString(); workShip.MeshInfos = new List(); foreach (JsonData meshInfos in item["MeshInfos"]) { Meshinfo meshinfo = new Meshinfo(); meshinfo.type = int.Parse(meshInfos["type"].ToString()); meshinfo.key = meshInfos["key"].ToString(); meshinfo.meshName = meshInfos["meshName"].ToString(); meshinfo.content = meshInfos["content"].ToString(); workShip.MeshInfos.Add(meshinfo); } workShips.Add(workShip); } //遍历场景中的工艺信息并存储 string sceneName = SceneManager.GetActiveScene().name; WorkShip nowWorkShip = new WorkShip(); nowWorkShip.SceneName = sceneName; nowWorkShip.MeshInfos = new List(); List workShipBinds = FindObjectsOfType().ToList(); foreach (WorkShipBind meshInfo in workShipBinds) { Meshinfo meshinfo = new Meshinfo(); meshinfo.type = meshInfo.type; if(string.IsNullOrEmpty(meshInfo.key)) { meshInfo.key = meshInfo.gameObject.GetInstanceID() + ""; } meshinfo.key = meshInfo.key; if (string.IsNullOrEmpty(meshInfo.meshName)) { meshInfo.meshName = meshInfo.gameObject.name; } meshinfo.meshName = meshInfo.meshName; meshinfo.content = meshInfo.content; nowWorkShip.MeshInfos.Add(meshinfo); } //匹配以场景内最新的替换掉旧的 WorkShip nowSceneWorkShip = workShips.Find(x => x.SceneName == sceneName); if(nowSceneWorkShip == null) { //没有当前场景的信息 workShips.Add(nowWorkShip); } else { //有当前场景的就更新 foreach (Meshinfo item in nowWorkShip.MeshInfos) { Meshinfo localMeshinfo = nowSceneWorkShip.MeshInfos.Find(x=>x.key == item.key); if(localMeshinfo == null) { //为空就直接加进去 nowSceneWorkShip.MeshInfos.Add(item); } else { //不为空就更新信息 localMeshinfo.type = item.type; localMeshinfo.key = item.key; localMeshinfo.meshName = item.meshName; localMeshinfo.content = item.content; } } } string workShipInfos = Regex.Unescape(JsonMapper.ToJson(workShips)); File.WriteAllText(Application.streamingAssetsPath + "/Processing/WorkShipInfo.json", workShipInfos); Debug.Log("操作完成!!!!"); } }