278 lines
11 KiB
C#
278 lines
11 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.IO;
|
|||
|
|
using UnityEditor;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using ZXKFramework;
|
|||
|
|
|
|||
|
|
public class SceneDataHandler : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
public bool repeatSave;
|
|||
|
|
Dictionary<string, GameObject> sceneData;
|
|||
|
|
public void Init()
|
|||
|
|
{
|
|||
|
|
sceneData = new();
|
|||
|
|
GuidComponent[] gObjs = FindObjectsOfType<GuidComponent>(true);
|
|||
|
|
foreach (var item in gObjs)
|
|||
|
|
{
|
|||
|
|
sceneData.Add(item.GetGuid().ToString(), item.gameObject);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// <20><><EFBFBD>泡<EFBFBD><E6B3A1><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>JSON<4F>ļ<EFBFBD>
|
|||
|
|
public void SaveSceneDataToJson(string jsonFilePath)
|
|||
|
|
{
|
|||
|
|
#if UNITY_EDITOR //<2F>ڱ༭<DAB1><E0BCAD>ģʽ<C4A3><CABD>
|
|||
|
|
if (repeatSave || !File.Exists(Application.streamingAssetsPath + "/" + jsonFilePath))
|
|||
|
|
{
|
|||
|
|
GameObjectDatas gameObjectDatas = new GameObjectDatas();
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD>и<EFBFBD><D0B8><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
foreach (var gObj in FindObjectsOfType<GuidComponent>(true))
|
|||
|
|
{
|
|||
|
|
if (gObj != null && LayerMask.LayerToName(gObj.gameObject.layer) != "Ignore Save Data" && gObj.GetComponent<RectTransform>() == null && gObj.GetComponent<SpriteRenderer>() == 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("<22>洢<EFBFBD><E6B4A2><EFBFBD><EFBFBD>");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
public static void SaveSceneDataToJsonEditor(string jsonFilePath)
|
|||
|
|
{
|
|||
|
|
GameObjectDatas gameObjectDatas = new GameObjectDatas();
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD>и<EFBFBD><D0B8><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
foreach (var gObj in FindObjectsOfType<GuidComponent>(true))
|
|||
|
|
{
|
|||
|
|
if (gObj != null && LayerMask.LayerToName(gObj.gameObject.layer) != "Ignore Save Data" && gObj.GetComponent<RectTransform>() == null && gObj.GetComponent<SpriteRenderer>() == 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("<22>洢<EFBFBD><E6B4A2><EFBFBD><EFBFBD>");
|
|||
|
|
}
|
|||
|
|
// <20><>JSON<4F>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>س<EFBFBD><D8B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
public void LoadSceneDataFromJson(string jsonFilePath, Action callBack = null)
|
|||
|
|
{
|
|||
|
|
Game.Instance.res.Load<TextAsset>(jsonFilePath, args =>
|
|||
|
|
{
|
|||
|
|
GameObjectDatas gameObjectDatas = JsonUtility.FromJson<GameObjectDatas>(args.text);
|
|||
|
|
foreach (var data in gameObjectDatas.gameObjectDatas)
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD>һ<D2BB><F2B4B4BD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
GameObject foundObject = FindObjectByGUID(data.guid);
|
|||
|
|
if (foundObject != null)
|
|||
|
|
{
|
|||
|
|
if (!string.IsNullOrEmpty(data.parentGUID))
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
if (foundObject.transform.parent.GetComponent<GuidComponent>().GetGuid().ToString() != data.parentGUID)
|
|||
|
|
{
|
|||
|
|
GameObject parentObject = FindObjectByGUID(data.parentGUID);
|
|||
|
|
if (parentObject != null)
|
|||
|
|
{
|
|||
|
|
foundObject.transform.SetParent(parentObject.transform);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception)
|
|||
|
|
{
|
|||
|
|
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
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("û<><C3BB><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD>壬InstanceID: " + data.id);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
callBack?.Invoke();
|
|||
|
|
Debug.Log("<22><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>");
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
public static void LoadSceneDataFromJsonEditor(string jsonFilePath)
|
|||
|
|
{
|
|||
|
|
string json = File.ReadAllText(Application.streamingAssetsPath + "/" + jsonFilePath);
|
|||
|
|
GameObjectDatas gameObjectDatas = JsonUtility.FromJson<GameObjectDatas>(json);
|
|||
|
|
foreach (var data in gameObjectDatas.gameObjectDatas)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>һ<D2BB><F2B4B4BD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
GameObject foundObject = FindObjectByGUIDEditor(data.guid);
|
|||
|
|
if (foundObject != null)
|
|||
|
|
{
|
|||
|
|
if (!string.IsNullOrEmpty(data.parentGUID))
|
|||
|
|
{
|
|||
|
|
if (foundObject.transform.parent.GetComponent<GuidComponent>().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("û<><C3BB><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD>壬InstanceID: " + data.id);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
Debug.Log("<22><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>");
|
|||
|
|
}
|
|||
|
|
GameObject FindObjectByGUID(string guid)
|
|||
|
|
{
|
|||
|
|
if (sceneData.ContainsKey(guid))
|
|||
|
|
{
|
|||
|
|
return sceneData[guid];
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
static GameObject FindObjectByGUIDEditor(string guid)
|
|||
|
|
{
|
|||
|
|
GuidComponent[] gObjs = FindObjectsOfType<GuidComponent>(true);
|
|||
|
|
foreach (GuidComponent gObj in gObjs)
|
|||
|
|
{
|
|||
|
|
if (gObj.GetGuid().ToString() == guid)
|
|||
|
|
{
|
|||
|
|
return gObj.gameObject;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
private void OnDestroy()
|
|||
|
|
{
|
|||
|
|
sceneData?.Clear();
|
|||
|
|
}
|
|||
|
|
}
|