using System; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; using ZXKFramework; public class SceneDataHandler : MonoBehaviour { public bool repeatSave; Dictionary sceneData; public void Init() { sceneData = new(); GuidComponent[] gObjs = FindObjectsOfType(true); foreach (var item in gObjs) { sceneData.Add(item.GetGuid().ToString(), item.gameObject); } } // 保存场景数据到JSON文件 public void SaveSceneDataToJson(string jsonFilePath) { #if UNITY_EDITOR //在编辑器模式下 if (repeatSave || !File.Exists(Application.streamingAssetsPath + "/" + jsonFilePath)) { GameObjectDatas gameObjectDatas = new GameObjectDatas(); // 遍历场景中的所有根物体 foreach (var gObj in FindObjectsOfType(true)) { if (gObj != null && LayerMask.LayerToName(gObj.gameObject.layer) != "Ignore Save Data" && gObj.GetComponent() == null && gObj.GetComponent() == null) { GameObjectData data = new GameObjectData(); data.guid = gObj.GetGuid().ToString(); data.position = gObj.transform.localPosition; data.rotation = gObj.transform.localRotation; data.scale = gObj.transform.localScale; data.isActive = gObj.gameObject.activeSelf; if (gObj.transform.parent) { if (gObj.transform.parent.TryGetComponent(out GuidComponent guidComponent)) { data.parentGUID = guidComponent.GetGuid().ToString(); } } if (gObj.TryGetComponent(out Collider collider)) { data.colliderEnable = collider.enabled; } else { data.colliderEnable = false; } if (gObj.TryGetComponent(out LinearMapping mapping)) { data.linearMapping = mapping.value; } if (gObj.TryGetComponent(out SkinnedMeshRenderer render)) { if (render.sharedMesh) { for (int i = 0; i < render.sharedMesh.blendShapeCount; i++) { data.skinnedMeshRender.Add(render.GetBlendShapeWeight(i)); } } } gameObjectDatas.gameObjectDatas.Add(data); } } string json = JsonUtility.ToJson(gameObjectDatas); //File.WriteAllText(Application.dataPath + "/Resources/" + jsonFilePath, json); File.WriteAllText(Application.streamingAssetsPath + "/" + jsonFilePath, json); #if UNITY_EDITOR AssetDatabase.Refresh(); #endif Debug.Log("存储完毕"); return; } #endif } public static void SaveSceneDataToJsonEditor(string jsonFilePath) { GameObjectDatas gameObjectDatas = new GameObjectDatas(); // 遍历场景中的所有根物体 foreach (var gObj in FindObjectsOfType(true)) { if (gObj != null && LayerMask.LayerToName(gObj.gameObject.layer) != "Ignore Save Data" && gObj.GetComponent() == null && gObj.GetComponent() == null) { GameObjectData data = new GameObjectData(); data.guid = gObj.GetGuid().ToString(); data.position = gObj.transform.localPosition; data.rotation = gObj.transform.localRotation; data.scale = gObj.transform.localScale; data.isActive = gObj.gameObject.activeSelf; if (gObj.transform.parent) { if (gObj.transform.parent.TryGetComponent(out GuidComponent guidComponent)) { data.parentGUID = guidComponent.GetGuid().ToString(); } } if (gObj.TryGetComponent(out Collider collider)) { data.colliderEnable = collider.enabled; } else { data.colliderEnable = false; } if (gObj.TryGetComponent(out LinearMapping mapping)) { data.linearMapping = mapping.value; } if (gObj.TryGetComponent(out SkinnedMeshRenderer render)) { if (render.sharedMesh) { for (int i = 0; i < render.sharedMesh.blendShapeCount; i++) { data.skinnedMeshRender.Add(render.GetBlendShapeWeight(i)); } } } gameObjectDatas.gameObjectDatas.Add(data); } } string json = JsonUtility.ToJson(gameObjectDatas); //File.WriteAllText(Application.dataPath + "/Resources/" + jsonFilePath, json); File.WriteAllText(Application.streamingAssetsPath + "/" + jsonFilePath, json); #if UNITY_EDITOR AssetDatabase.Refresh(); #endif Debug.Log("存储完毕"); } // 从JSON文件加载场景数据 public void LoadSceneDataFromJson(string jsonFilePath, Action callBack = null) { Game.Instance.res.Load(jsonFilePath, args => { GameObjectDatas gameObjectDatas = JsonUtility.FromJson(args.text); foreach (var data in gameObjectDatas.gameObjectDatas) { // 查找或创建物体 GameObject foundObject = FindObjectByGUID(data.guid); if (foundObject != null) { if (!string.IsNullOrEmpty(data.parentGUID)) { try { if (foundObject.transform.parent.GetComponent().GetGuid().ToString() != data.parentGUID) { GameObject parentObject = FindObjectByGUID(data.parentGUID); if (parentObject != null) { foundObject.transform.SetParent(parentObject.transform); } } } catch (Exception) { Debug.Log("有问题"); } } else { foundObject.transform.SetParent(null); } foundObject.transform.localPosition = data.position; foundObject.transform.localRotation = data.rotation; foundObject.transform.localScale = data.scale; foundObject.SetActive(data.isActive); if (foundObject.TryGetComponent(out Collider collider)) { collider.enabled = data.colliderEnable; } if (foundObject.TryGetComponent(out LinearMapping mapping)) { mapping.value = data.linearMapping; } if (foundObject.TryGetComponent(out SkinnedMeshRenderer render)) { for (int i = 0; i < data.skinnedMeshRender.Count; i++) { render.SetBlendShapeWeight(i, data.skinnedMeshRender[i]); } } } else { //Debug.Log("没有找到物体,InstanceID: " + data.id); } } callBack?.Invoke(); Debug.Log("读取完毕"); }); } public static void LoadSceneDataFromJsonEditor(string jsonFilePath) { string json = File.ReadAllText(Application.streamingAssetsPath + "/" + jsonFilePath); GameObjectDatas gameObjectDatas = JsonUtility.FromJson(json); foreach (var data in gameObjectDatas.gameObjectDatas) { // 查找或创建物体 GameObject foundObject = FindObjectByGUIDEditor(data.guid); if (foundObject != null) { if (!string.IsNullOrEmpty(data.parentGUID)) { if (foundObject.transform.parent.GetComponent().GetGuid().ToString() != data.parentGUID) { GameObject parentObject = FindObjectByGUIDEditor(data.parentGUID); if (parentObject != null) { foundObject.transform.SetParent(parentObject.transform); } } } else { foundObject.transform.SetParent(null); } foundObject.transform.localPosition = data.position; foundObject.transform.localRotation = data.rotation; foundObject.transform.localScale = data.scale; foundObject.SetActive(data.isActive); if (foundObject.TryGetComponent(out Collider collider)) { collider.enabled = data.colliderEnable; } if (foundObject.TryGetComponent(out LinearMapping mapping)) { mapping.value = data.linearMapping; } if (foundObject.TryGetComponent(out SkinnedMeshRenderer render)) { for (int i = 0; i < data.skinnedMeshRender.Count; i++) { render.SetBlendShapeWeight(i, data.skinnedMeshRender[i]); } } } else { //Debug.Log("没有找到物体,InstanceID: " + data.id); } } Debug.Log("读取完毕"); } GameObject FindObjectByGUID(string guid) { if (sceneData.ContainsKey(guid)) { return sceneData[guid]; } return null; } static GameObject FindObjectByGUIDEditor(string guid) { GuidComponent[] gObjs = FindObjectsOfType(true); foreach (GuidComponent gObj in gObjs) { if (gObj.GetGuid().ToString() == guid) { return gObj.gameObject; } } return null; } private void OnDestroy() { sceneData?.Clear(); } }