using System.Collections.Generic; using Newtonsoft.Json; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.Serialization; using UnityEngine.UI; namespace ZXL.Helper { public static class SceneHelper { public static void CreateSceneData(string sceneName) { SceneData sceneData = new SceneData(); sceneData.sceneName = sceneName; var activeScene = SceneManager.GetActiveScene(); var rootGameObjects = activeScene.GetRootGameObjects(); List list = new List(); foreach (var root in rootGameObjects) { var deeps = root.transform.FindChildDeeps(); list.AddRange(deeps); } foreach (var info in list) { var rootGameObject = info.gameObject; GameObjectData gameObjectData = new GameObjectData(); gameObjectData.name = rootGameObject.name; gameObjectData.rootPath = info.scenePath; gameObjectData.parentName = rootGameObject.transform.parent == null ? string.Empty : rootGameObject.transform.parent.name; var transformPosition = rootGameObject.transform.localPosition; var transformRotation = rootGameObject.transform.localRotation; var transformScale = rootGameObject.transform.localScale; gameObjectData.localPosition = new[] { transformPosition.x, transformPosition.y, transformPosition.z }; gameObjectData.localRotation = new[] { transformRotation.x, transformRotation.y, transformRotation.z, transformRotation.w }; gameObjectData.localScale = new[] { transformScale.x, transformScale.y, transformScale.z }; gameObjectData.isActive = rootGameObject.activeSelf; List components = new List(); rootGameObject.GetComponents(components); Debug.Log(rootGameObject.name); foreach (var component in components) { ComponentData componentData = new ComponentData(); componentData.componentName = component.GetType().ToString(); componentData.isEnabled = true; gameObjectData.components.Add(componentData); } sceneData.gameObjects.Add(gameObjectData); } var s = JsonConvert.SerializeObject(sceneData); Debug.Log(s); } #region 遍历查找场景所有物体并填充数据 static List FindChildDeeps(this Transform self) { List list = new List(); FindDeeps(self, "", ref list); if (list.Count <= 0) { Debug.LogError("未找到此组件"); } return list; } static void FindDeeps(Transform tran, string path, ref List list) { // if (tran != null) string scenePath; if (string.IsNullOrEmpty(path)) scenePath = $"{tran.name}"; else scenePath = $"{path}/{tran.name}"; GameObjectInfo info = new GameObjectInfo() { scenePath = scenePath, gameObject = tran.gameObject, }; list.Add(info); for (var i = 0; i < tran.childCount; i++) { FindDeeps(tran.GetChild(i), scenePath, ref list); } } #endregion } [System.Serializable] class GameObjectInfo { public GameObject gameObject; public string scenePath; } [System.Serializable] class SceneData { public string sceneName; public List gameObjects = new List(); } [System.Serializable] class GameObjectData { public string name; public string parentName; public string rootPath; public float[] localPosition; public float[] localRotation; public float[] localScale; public bool isActive; public List components = new List(); } [System.Serializable] class ComponentData { public string componentName; public string data; public bool isEnabled; } }