GrassLoaderEditor.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditor.SceneManagement;
  4. using UnityEngine.SceneManagement;
  5. namespace MTE
  6. {
  7. [CustomEditor(typeof(GrassLoader))]
  8. internal class GrassLoaderEditor : Editor
  9. {
  10. /// <summary>
  11. /// Remove lightmap data from the grass GameObject, so it won't be influenced by the possibly invalid lightmap data.
  12. /// </summary>
  13. private static void ClearLightmapData()
  14. {
  15. var items = GrassMap.GetAllGrassItems();
  16. foreach (var item in items)
  17. {
  18. if (item == null)
  19. {
  20. MTEDebug.LogWarning("A grass item is null.");
  21. continue;
  22. }
  23. var obj = item.gameObject;
  24. if (obj == null)
  25. {
  26. MTEDebug.LogWarning(
  27. "A grass object is not available: null or the referenced object is missing.");
  28. continue;
  29. }
  30. var renderer = obj.GetComponent<MeshRenderer>();
  31. if (renderer == null)
  32. {
  33. MTEDebug.LogWarning(
  34. "Cannot access lightmap data because the grass GameObject" +
  35. " don't have a MeshRenderer.");
  36. continue;
  37. }
  38. renderer.lightmapIndex = -1;
  39. renderer.lightmapScaleOffset = new Vector4(1, 1, 0, 0);
  40. }
  41. }
  42. private static void SaveLightmapData()
  43. {
  44. var items = GrassMap.GetAllGrassItems();
  45. foreach (var item in items)
  46. {
  47. if (item == null)
  48. {
  49. MTEDebug.LogWarning("A grass item is null.");
  50. continue;
  51. }
  52. var obj = item.gameObject;
  53. if (obj == null)
  54. {
  55. MTEDebug.LogWarning(
  56. "A grass GameObject is not available: null or the referenced object is missing.");
  57. continue;
  58. }
  59. var renderer = obj.gameObject.GetComponent<MeshRenderer>();
  60. if (renderer == null)
  61. {
  62. MTEDebug.LogWarning(
  63. "A grass object is broken: not MeshRenderer attached." +
  64. " Please *Rebuild Grasses*.");
  65. continue;
  66. }
  67. var lightmapIndex = renderer.lightmapIndex;
  68. var lightmapScaleOffset = renderer.lightmapScaleOffset;
  69. //check if lightmap data is valid
  70. if (lightmapIndex == -1)
  71. {
  72. MTEDebug.LogWarning(
  73. "Lightmap data for some grass is invalid: lightmap index is -1.");
  74. continue;
  75. }
  76. var quad = item.Quad;
  77. var star = item.Star;
  78. if (quad != null)
  79. {
  80. quad.SaveLightmapData(lightmapIndex, lightmapScaleOffset);
  81. }
  82. else if (star != null)
  83. {
  84. star.SaveLightmapData(lightmapIndex, lightmapScaleOffset);
  85. }
  86. else
  87. {
  88. MTEDebug.LogError(
  89. "A grass object is not a quad nor a star: the data for this grass is invalid." +
  90. " The grass asset file may have been corrupted.");
  91. return;
  92. }
  93. }
  94. // Save grass asset file
  95. EditorUtility.SetDirty(MTEContext.TheGrassLoader.grassInstanceList);
  96. Utility.ShowNotification(StringTable.Get(C.Info_LightmapDataSaved));
  97. }
  98. public override void OnInspectorGUI()
  99. {
  100. base.OnInspectorGUI();
  101. GUI.enabled = !Application.isPlaying;
  102. GUILayout.Space(10);
  103. var thisGameObject = ((GrassLoader) this.target).gameObject;
  104. if (CompatibilityUtil.IsPrefab(thisGameObject))
  105. {
  106. EditorGUILayout.HelpBox("Cannot use prefab for GrassLoader. Click the button below to fix it:", MessageType.Error);
  107. if (GUILayout.Button("Revert to normal GameObject."))
  108. {
  109. Utility.ConvertPrefabToGameObject(thisGameObject);
  110. }
  111. return;
  112. }
  113. if (GUILayout.Button(StringTable.Get(C.ReloadFromFile), GUILayout.Height(60)))
  114. {
  115. GrassEditorUtil.ReloadGrassesFromFile((GrassLoader)target);
  116. }
  117. GUILayout.Space(5);
  118. if(GUILayout.Button(StringTable.Get(C.BakeLightmap), GUILayout.Height(60)))
  119. {
  120. Lightmapping.BakeAsync();
  121. }
  122. GUILayout.Space(5);
  123. if (GUILayout.Button(StringTable.Get(C.SaveLightmapData), GUILayout.Height(60)))
  124. {
  125. SaveLightmapData();
  126. }
  127. GUILayout.Space(5);
  128. if (GUILayout.Button(StringTable.Get(C.ClearPreviewGrasssesAndSaveScene), GUILayout.Height(60)))
  129. {
  130. ClearGrassesFromScene();
  131. var activeScene = SceneManager.GetActiveScene();
  132. var result = EditorSceneManager.SaveScene(activeScene);
  133. if (!result)
  134. {
  135. MTEDebug.LogError("Failed to save the active scene. Please try to save the scene manually.");
  136. }
  137. }
  138. EditorGUILayout.HelpBox(StringTable.Get(C.Warning_HowToSaveASceneWithGrasses), MessageType.Warning);
  139. GUILayout.Space(100);
  140. GUI.enabled = true;
  141. if (Settings.DebugMode)
  142. {
  143. if (GUILayout.Button(StringTable.Get(C.ShowGrassObjects)))
  144. {
  145. var grassLoader = (GrassLoader)target;
  146. var loaderObject = grassLoader.gameObject;
  147. var childCount = loaderObject.transform.childCount;
  148. var transform = loaderObject.transform;
  149. for (int i = 0; i < childCount; i++)
  150. {
  151. var gameObject = transform.GetChild(i).gameObject;
  152. gameObject.hideFlags &= ~HideFlags.HideInHierarchy;
  153. }
  154. }
  155. }
  156. }
  157. private void ClearGrassesFromScene()
  158. {
  159. //clear grass GameObjects
  160. var grassLoader = (GrassLoader)target;
  161. grassLoader.RemoveOldGrasses();
  162. }
  163. }
  164. }