using System; using System.IO; using UnityEditor; using UnityEditor.SceneManagement; using UnityEngine; using UnityEngine.SceneManagement; using Seengene.XDKUnityPluginCloud; public class SceneSetupObject { static GameObject SceneRoot; static GameObject MapRootDemo = null; static Scene scene; public static void ChangeCoordinate(GameObject sourcePLY, Material customMaterial) { if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) { scene = EditorSceneManager.OpenScene(EditorConfigs.Path_Scene); SceneRoot = GameObject.Find("SceneRoot"); if (SceneRoot != null) { Transform transMapRootDemo = SceneRoot.transform.Find("MapRootDemo"); if (transMapRootDemo != null) { GameObject.DestroyImmediate(transMapRootDemo.gameObject); } MapRootDemo = new GameObject("MapRootDemo"); MapRootDemo.transform.SetParent(SceneRoot.transform); MapRootDemo.transform.localEulerAngles = new Vector3(0, 0, 0); MapRootDemo.transform.localScale = new Vector3(1, 1, 1); MapRootDemo.transform.localScale = new Vector3(1, 1, 1); GameObject goPLY = GameObject.Instantiate(sourcePLY); goPLY.name = sourcePLY.name; Renderer renderer = goPLY.GetComponent(); if (renderer != null) { renderer.material = customMaterial; } goPLY.transform.SetParent(MapRootDemo.transform); goPLY.transform.localEulerAngles = new Vector3(0, 0, 0); goPLY.transform.localScale = new Vector3(1, 1, 1); SceneSetupObject.SetPlyTransform(EditorConfigs.Path_Scale, MapRootDemo); } } } public static void DeleteMapRootDemo() { if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) { scene = EditorSceneManager.OpenScene(EditorConfigs.Path_Scene); SceneRoot = GameObject.Find("SceneRoot"); if (SceneRoot != null) { Transform transMapRootDemo = SceneRoot.transform.Find("MapRootDemo"); if (transMapRootDemo != null) { GameObject.DestroyImmediate(transMapRootDemo.gameObject); //保存新scene EditorSceneManager.SaveScene(scene); } } } } static void SetPlyTransform(string scalePath, GameObject mapRoot) { string m_Str = ""; string[] strs = File.ReadAllLines(scalePath); for (int i = 0; i < strs.Length; i++) { m_Str += strs[i];//读取每一行,并连起来 m_Str += "\n";//每一行末尾换行 } string[] str = m_Str.Split(','); float[] f = new float[str.Length]; for (int i = 0; i < str.Length; i++) { f[i] = float.Parse(str[i]); } m_Str = ""; float W = f[0], X = f[1], Y = f[2], Z = f[3], Scale = f[4]; Vector3 v = new Quaternion(X, Y, Z, W).eulerAngles; float change_X = 360 - v.x; float change_Y = v.y; float change_Z = 360 - v.z; mapRoot.transform.localEulerAngles = new Vector3(change_X, change_Y, change_Z); mapRoot.transform.localScale = new Vector3(Scale, Scale, Scale); try { bool isSuccessed = false; if (File.Exists(EditorConfigs.Path_PrefabPly)) File.Delete(EditorConfigs.Path_PrefabPly); string assetPath = XDKTools.FormatAssetPath(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_PrefabPly)); GameObject prefabPly = PrefabUtility.SaveAsPrefabAsset(mapRoot, assetPath, out isSuccessed); if (isSuccessed) { Debug.Log("Create Prefab Successed!"); GameObject.DestroyImmediate(MapRootDemo); MapRootDemo = GameObject.Instantiate(prefabPly, SceneRoot.transform); MapRootDemo.name = prefabPly.name; } else { Debug.LogWarning("Create Prefab Failed!"); } } catch (Exception e) { Debug.LogWarning("Create Prefab Failed!\r\n" + e.Message); } AssetDatabase.Refresh(); //保存新scene EditorSceneManager.SaveScene(scene); } }