EnvironmentLibrary.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine.UIElements;
  6. using System.IO;
  7. using UnityEditor;
  8. using UnityEditor.UIElements;
  9. namespace UnityEditor.Rendering.LookDev
  10. {
  11. /// <summary>
  12. /// Class containing a collection of Environment
  13. /// </summary>
  14. [HelpURL(Documentation.baseURLHDRP + Documentation.version + Documentation.subURL + "Environment-Library" + Documentation.endURL)]
  15. public class EnvironmentLibrary : ScriptableObject
  16. {
  17. [field: SerializeField]
  18. List<Environment> environments { get; set; } = new List<Environment>();
  19. /// <summary>
  20. /// Number of elements in the collection
  21. /// </summary>
  22. public int Count => environments.Count;
  23. /// <summary>
  24. /// Indexer giving access to contained Environment
  25. /// </summary>
  26. public Environment this[int index] => environments[index];
  27. /// <summary>
  28. /// Create a new empty Environment at the end of the collection
  29. /// </summary>
  30. /// <returns>The created Environment</returns>
  31. public Environment Add()
  32. {
  33. Undo.SetCurrentGroupName("Add Environment");
  34. int group = Undo.GetCurrentGroup();
  35. Environment environment = ScriptableObject.CreateInstance<Environment>();
  36. environment.name = "New Environment";
  37. Undo.RegisterCreatedObjectUndo(environment, "Add Environment");
  38. Undo.RecordObject(this, "Add Environment");
  39. environments.Add(environment);
  40. // Store this new environment as a subasset so we can reference it safely afterwards.
  41. AssetDatabase.AddObjectToAsset(environment, this);
  42. Undo.CollapseUndoOperations(group);
  43. // Force save / refresh. Important to do this last because SaveAssets can cause effect to become null!
  44. EditorUtility.SetDirty(this);
  45. AssetDatabase.SaveAssets();
  46. return environment;
  47. }
  48. /// <summary>
  49. /// Remove Environment of the collection at given index
  50. /// </summary>
  51. /// <param name="index">Index where to remove Environment</param>
  52. public void Remove(int index)
  53. {
  54. Undo.SetCurrentGroupName("Remove Environment");
  55. int group = Undo.GetCurrentGroup();
  56. Environment environment = environments[index];
  57. Undo.RecordObject(this, "Remove Environment");
  58. environments.RemoveAt(index);
  59. Undo.DestroyObjectImmediate(environment);
  60. Undo.CollapseUndoOperations(group);
  61. // Force save / refresh
  62. EditorUtility.SetDirty(this);
  63. AssetDatabase.SaveAssets();
  64. }
  65. /// <summary>
  66. /// Duplicate the Environment at given index and add it at the end of the Collection
  67. /// </summary>
  68. /// <param name="fromIndex">Index where to take data for duplication</param>
  69. /// <returns>The created Environment</returns>
  70. public Environment Duplicate(int fromIndex)
  71. {
  72. Undo.SetCurrentGroupName("Duplicate Environment");
  73. int group = Undo.GetCurrentGroup();
  74. Environment environment = ScriptableObject.CreateInstance<Environment>();
  75. Environment environmentToCopy = environments[fromIndex];
  76. environmentToCopy.CopyTo(environment);
  77. Undo.RegisterCreatedObjectUndo(environment, "Duplicate Environment");
  78. Undo.RecordObject(this, "Duplicate Environment");
  79. environments.Add(environment);
  80. // Store this new environment as a subasset so we can reference it safely afterwards.
  81. AssetDatabase.AddObjectToAsset(environment, this);
  82. Undo.CollapseUndoOperations(group);
  83. // Force save / refresh. Important to do this last because SaveAssets can cause effect to become null!
  84. EditorUtility.SetDirty(this);
  85. AssetDatabase.SaveAssets();
  86. return environment;
  87. }
  88. /// <summary>
  89. /// Compute position of given Environment in the collection
  90. /// </summary>
  91. /// <param name="environment">Environment to look at</param>
  92. /// <returns>Index of the searched environment. If not found, -1.</returns>
  93. public int IndexOf(Environment environment)
  94. => environments.IndexOf(environment);
  95. }
  96. [CustomEditor(typeof(EnvironmentLibrary))]
  97. class EnvironmentLibraryEditor : Editor
  98. {
  99. VisualElement m_Root;
  100. VisualElement m_OpenButton;
  101. public sealed override VisualElement CreateInspectorGUI()
  102. {
  103. var library = target as EnvironmentLibrary;
  104. m_Root = new VisualElement();
  105. m_OpenButton = new Button(() =>
  106. {
  107. if (!LookDev.open)
  108. LookDev.Open();
  109. LookDev.currentContext.UpdateEnvironmentLibrary(library);
  110. LookDev.currentEnvironmentDisplayer.Repaint();
  111. })
  112. {
  113. text = "Open in Look Dev window"
  114. };
  115. m_OpenButton.SetEnabled(LookDev.supported);
  116. m_Root.Add(m_OpenButton);
  117. return m_Root;
  118. }
  119. void OnEnable() => EditorApplication.update += Update;
  120. void OnDisable() => EditorApplication.update -= Update;
  121. void Update()
  122. {
  123. // Current SRP can be changed at any time so we need to do this at every update.
  124. if (m_OpenButton != null)
  125. m_OpenButton.SetEnabled(LookDev.supported);
  126. }
  127. // Don't use ImGUI
  128. public sealed override void OnInspectorGUI() { }
  129. }
  130. class EnvironmentLibraryCreator : ProjectWindowCallback.EndNameEditAction
  131. {
  132. ObjectField m_Field = null;
  133. public void SetField(ObjectField field)
  134. => m_Field = field;
  135. public override void Cancelled(int instanceId, string pathName, string resourceFile)
  136. => m_Field = null;
  137. public override void Action(int instanceId, string pathName, string resourceFile)
  138. {
  139. var newAsset = CreateInstance<EnvironmentLibrary>();
  140. newAsset.name = Path.GetFileName(pathName);
  141. AssetDatabase.CreateAsset(newAsset, pathName);
  142. ProjectWindowUtil.ShowCreatedAsset(newAsset);
  143. if (m_Field != null)
  144. m_Field.value = newAsset;
  145. m_Field = null;
  146. }
  147. [MenuItem("Assets/Create/LookDev/Environment Library", priority = 2000)]
  148. static void Create()
  149. {
  150. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon");
  151. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance<EnvironmentLibraryCreator>(), "New EnvironmentLibrary.asset", icon, null);
  152. }
  153. public static void CreateAndAssignTo(ObjectField field)
  154. {
  155. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon");
  156. var assetCreator = ScriptableObject.CreateInstance<EnvironmentLibraryCreator>();
  157. assetCreator.SetField(field);
  158. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(assetCreator.GetInstanceID(), assetCreator, "New EnvironmentLibrary.asset", icon, null);
  159. }
  160. }
  161. static class EnvironmentLibraryLoader
  162. {
  163. static Action<UnityEngine.Object> LoadCallback(Action onUpdate)
  164. {
  165. return (UnityEngine.Object newLibrary) =>
  166. {
  167. LookDev.currentContext.UpdateEnvironmentLibrary(newLibrary as EnvironmentLibrary);
  168. onUpdate?.Invoke();
  169. };
  170. }
  171. }
  172. }