SceneSetupObject.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.IO;
  3. using UnityEditor;
  4. using UnityEditor.SceneManagement;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. using Seengene.XDKUnityPluginCloud;
  8. public class SceneSetupObject {
  9. static GameObject SceneRoot;
  10. static GameObject MapRootDemo = null;
  11. static Scene scene;
  12. public static void ChangeCoordinate(GameObject sourcePLY, Material customMaterial) {
  13. if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) {
  14. scene = EditorSceneManager.OpenScene(EditorConfigs.Path_Scene);
  15. SceneRoot = GameObject.Find("SceneRoot");
  16. if (SceneRoot != null) {
  17. Transform transMapRootDemo = SceneRoot.transform.Find("MapRootDemo");
  18. if (transMapRootDemo != null) {
  19. GameObject.DestroyImmediate(transMapRootDemo.gameObject);
  20. }
  21. MapRootDemo = new GameObject("MapRootDemo");
  22. MapRootDemo.transform.SetParent(SceneRoot.transform);
  23. MapRootDemo.transform.localEulerAngles = new Vector3(0, 0, 0);
  24. MapRootDemo.transform.localScale = new Vector3(1, 1, 1);
  25. MapRootDemo.transform.localScale = new Vector3(1, 1, 1);
  26. GameObject goPLY = GameObject.Instantiate(sourcePLY);
  27. goPLY.name = sourcePLY.name;
  28. Renderer renderer = goPLY.GetComponent<Renderer>();
  29. if (renderer != null) {
  30. renderer.material = customMaterial;
  31. }
  32. goPLY.transform.SetParent(MapRootDemo.transform);
  33. goPLY.transform.localEulerAngles = new Vector3(0, 0, 0);
  34. goPLY.transform.localScale = new Vector3(1, 1, 1);
  35. SceneSetupObject.SetPlyTransform(EditorConfigs.Path_Scale, MapRootDemo);
  36. }
  37. }
  38. }
  39. public static void DeleteMapRootDemo() {
  40. if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) {
  41. scene = EditorSceneManager.OpenScene(EditorConfigs.Path_Scene);
  42. SceneRoot = GameObject.Find("SceneRoot");
  43. if (SceneRoot != null) {
  44. Transform transMapRootDemo = SceneRoot.transform.Find("MapRootDemo");
  45. if (transMapRootDemo != null) {
  46. GameObject.DestroyImmediate(transMapRootDemo.gameObject);
  47. //保存新scene
  48. EditorSceneManager.SaveScene(scene);
  49. }
  50. }
  51. }
  52. }
  53. static void SetPlyTransform(string scalePath, GameObject mapRoot) {
  54. string m_Str = "";
  55. string[] strs = File.ReadAllLines(scalePath);
  56. for (int i = 0; i < strs.Length; i++) {
  57. m_Str += strs[i];//读取每一行,并连起来
  58. m_Str += "\n";//每一行末尾换行
  59. }
  60. string[] str = m_Str.Split(',');
  61. float[] f = new float[str.Length];
  62. for (int i = 0; i < str.Length; i++) {
  63. f[i] = float.Parse(str[i]);
  64. }
  65. m_Str = "";
  66. float W = f[0], X = f[1], Y = f[2], Z = f[3], Scale = f[4];
  67. Vector3 v = new Quaternion(X, Y, Z, W).eulerAngles;
  68. float change_X = 360 - v.x;
  69. float change_Y = v.y;
  70. float change_Z = 360 - v.z;
  71. mapRoot.transform.localEulerAngles = new Vector3(change_X, change_Y, change_Z);
  72. mapRoot.transform.localScale = new Vector3(Scale, Scale, Scale);
  73. try {
  74. bool isSuccessed = false;
  75. if (File.Exists(EditorConfigs.Path_PrefabPly)) File.Delete(EditorConfigs.Path_PrefabPly);
  76. string assetPath = XDKTools.FormatAssetPath(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_PrefabPly));
  77. GameObject prefabPly = PrefabUtility.SaveAsPrefabAsset(mapRoot, assetPath, out isSuccessed);
  78. if (isSuccessed) {
  79. Debug.Log("Create Prefab Successed!");
  80. GameObject.DestroyImmediate(MapRootDemo);
  81. MapRootDemo = GameObject.Instantiate(prefabPly, SceneRoot.transform);
  82. MapRootDemo.name = prefabPly.name;
  83. } else {
  84. Debug.LogWarning("Create Prefab Failed!");
  85. }
  86. } catch (Exception e) {
  87. Debug.LogWarning("Create Prefab Failed!\r\n" + e.Message);
  88. }
  89. AssetDatabase.Refresh();
  90. //保存新scene
  91. EditorSceneManager.SaveScene(scene);
  92. }
  93. }