EditorTool3D/Assets/ZXL/Scripts/Helper/SceneHelper.cs

139 lines
4.5 KiB
C#

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<GameObjectInfo> list = new List<GameObjectInfo>();
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<Component> components = new List<Component>();
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<GameObjectInfo> FindChildDeeps(this Transform self)
{
List<GameObjectInfo> list = new List<GameObjectInfo>();
FindDeeps(self, "", ref list);
if (list.Count <= 0)
{
Debug.LogError("未找到此组件");
}
return list;
}
static void FindDeeps(Transform tran, string path, ref List<GameObjectInfo> 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<GameObjectData> gameObjects = new List<GameObjectData>();
}
[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<ComponentData> components = new List<ComponentData>();
}
[System.Serializable]
class ComponentData
{
public string componentName;
public string data;
public bool isEnabled;
}
}