CinemachinePostProcessingEditor.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #if CINEMACHINE_POST_PROCESSING_V2
  2. using UnityEngine;
  3. using UnityEditor;
  4. using UnityEngine.Rendering.PostProcessing;
  5. using UnityEditor.Rendering.PostProcessing;
  6. using System.Collections.Generic;
  7. #endif
  8. namespace Cinemachine.PostFX.Editor
  9. {
  10. #if CINEMACHINE_POST_PROCESSING_V2
  11. [CustomEditor(typeof(CinemachinePostProcessing))]
  12. public sealed class CinemachinePostProcessingEditor : Cinemachine.Editor.BaseEditor<CinemachinePostProcessing>
  13. {
  14. SerializedProperty m_Profile;
  15. SerializedProperty m_FocusTracking;
  16. EffectListEditor m_EffectList;
  17. GUIContent m_ProfileLabel;
  18. void OnEnable()
  19. {
  20. Texture texture = AssetDatabase.LoadAssetAtPath<Texture>(
  21. Cinemachine.Editor.ScriptableObjectUtility.CinemachineRealativeInstallPath
  22. + "/Editor/EditorResources/PostProcessLayer.png");
  23. m_ProfileLabel = new GUIContent("Profile", texture, "A reference to a profile asset");
  24. m_FocusTracking = FindProperty(x => x.m_FocusTracking);
  25. m_Profile = FindProperty(x => x.m_Profile);
  26. m_EffectList = new EffectListEditor(this);
  27. RefreshEffectListEditor(Target.m_Profile);
  28. }
  29. void OnDisable()
  30. {
  31. if (m_EffectList != null)
  32. m_EffectList.Clear();
  33. }
  34. void RefreshEffectListEditor(PostProcessProfile asset)
  35. {
  36. if (m_EffectList == null)
  37. m_EffectList = new EffectListEditor(this);
  38. m_EffectList.Clear();
  39. if (asset != null)
  40. m_EffectList.Init(asset, new SerializedObject(asset));
  41. }
  42. /// <summary>Get the property names to exclude in the inspector.</summary>
  43. /// <param name="excluded">Add the names to this list</param>
  44. protected override void GetExcludedPropertiesInInspector(List<string> excluded)
  45. {
  46. base.GetExcludedPropertiesInInspector(excluded);
  47. var mode = (CinemachinePostProcessing.FocusTrackingMode)m_FocusTracking.intValue;
  48. if (mode != CinemachinePostProcessing.FocusTrackingMode.CustomTarget)
  49. excluded.Add(FieldPath(x => x.m_FocusTarget));
  50. if (mode == CinemachinePostProcessing.FocusTrackingMode.None)
  51. excluded.Add(FieldPath(x => x.m_FocusOffset));
  52. excluded.Add(FieldPath(x => x.m_Profile));
  53. }
  54. public override void OnInspectorGUI()
  55. {
  56. BeginInspector();
  57. DrawRemainingPropertiesInInspector();
  58. var focusMode = (CinemachinePostProcessing.FocusTrackingMode)m_FocusTracking.intValue;
  59. if (focusMode != CinemachinePostProcessing.FocusTrackingMode.None)
  60. {
  61. bool valid = false;
  62. DepthOfField dof;
  63. if (Target.m_Profile != null && Target.m_Profile.TryGetSettings(out dof))
  64. valid = dof.enabled && dof.active && dof.focusDistance.overrideState;
  65. if (!valid)
  66. EditorGUILayout.HelpBox(
  67. "Focus Tracking requires an active DepthOfField/FocusDistance effect in the profile",
  68. MessageType.Warning);
  69. }
  70. EditorGUI.BeginChangeCheck();
  71. DrawProfileInspectorGUI();
  72. if (EditorGUI.EndChangeCheck())
  73. {
  74. Target.InvalidateCachedProfile();
  75. serializedObject.ApplyModifiedProperties();
  76. }
  77. }
  78. void DrawProfileInspectorGUI()
  79. {
  80. EditorGUILayout.Space();
  81. bool assetHasChanged = false;
  82. bool showCopy = m_Profile.objectReferenceValue != null;
  83. // The layout system sort of break alignement when mixing inspector fields with custom
  84. // layouted fields, do the layout manually instead
  85. int buttonWidth = showCopy ? 45 : 60;
  86. float indentOffset = EditorGUI.indentLevel * 15f;
  87. var lineRect = GUILayoutUtility.GetRect(1, EditorGUIUtility.singleLineHeight);
  88. var labelRect = new Rect(lineRect.x, lineRect.y, EditorGUIUtility.labelWidth - indentOffset, lineRect.height);
  89. var fieldRect = new Rect(labelRect.xMax, lineRect.y, lineRect.width - labelRect.width - buttonWidth * (showCopy ? 2 : 1), lineRect.height);
  90. var buttonNewRect = new Rect(fieldRect.xMax, lineRect.y, buttonWidth, lineRect.height);
  91. var buttonCopyRect = new Rect(buttonNewRect.xMax, lineRect.y, buttonWidth, lineRect.height);
  92. EditorGUI.PrefixLabel(labelRect, m_ProfileLabel);
  93. using (var scope = new EditorGUI.ChangeCheckScope())
  94. {
  95. m_Profile.objectReferenceValue
  96. = (PostProcessProfile)EditorGUI.ObjectField(
  97. fieldRect, m_Profile.objectReferenceValue, typeof(PostProcessProfile), false);
  98. assetHasChanged = scope.changed;
  99. }
  100. if (GUI.Button(
  101. buttonNewRect,
  102. EditorUtilities.GetContent("New|Create a new profile."),
  103. showCopy ? EditorStyles.miniButtonLeft : EditorStyles.miniButton))
  104. {
  105. // By default, try to put assets in a folder next to the currently active
  106. // scene file. If the user isn't a scene, put them in root instead.
  107. var targetName = Target.name;
  108. var scene = Target.gameObject.scene;
  109. var asset = CreatePostProcessProfile(scene, targetName);
  110. m_Profile.objectReferenceValue = asset;
  111. assetHasChanged = true;
  112. }
  113. if (showCopy && GUI.Button(
  114. buttonCopyRect,
  115. EditorUtilities.GetContent("Clone|Create a new profile and copy the content of the currently assigned profile."),
  116. EditorStyles.miniButtonRight))
  117. {
  118. // Duplicate the currently assigned profile and save it as a new profile
  119. var origin = (PostProcessProfile)m_Profile.objectReferenceValue;
  120. var path = AssetDatabase.GetAssetPath(origin);
  121. path = AssetDatabase.GenerateUniqueAssetPath(path);
  122. var asset = Instantiate(origin);
  123. asset.settings.Clear();
  124. AssetDatabase.CreateAsset(asset, path);
  125. foreach (var item in origin.settings)
  126. {
  127. var itemCopy = Instantiate(item);
  128. itemCopy.hideFlags = HideFlags.HideInInspector | HideFlags.HideInHierarchy;
  129. itemCopy.name = item.name;
  130. asset.settings.Add(itemCopy);
  131. AssetDatabase.AddObjectToAsset(itemCopy, asset);
  132. }
  133. AssetDatabase.SaveAssets();
  134. AssetDatabase.Refresh();
  135. m_Profile.objectReferenceValue = asset;
  136. assetHasChanged = true;
  137. }
  138. if (m_Profile.objectReferenceValue == null)
  139. {
  140. if (assetHasChanged && m_EffectList != null)
  141. m_EffectList.Clear(); // Asset wasn't null before, do some cleanup
  142. EditorGUILayout.HelpBox(
  143. "Assign an existing Post-process Profile by choosing an asset, or create a new one by "
  144. + "clicking the \"New\" button.\nNew assets are automatically put in a folder next "
  145. + "to your scene file. If your scene hasn't been saved yet they will be created "
  146. + "at the root of the Assets folder.",
  147. MessageType.Info);
  148. }
  149. else
  150. {
  151. if (assetHasChanged)
  152. RefreshEffectListEditor((PostProcessProfile)m_Profile.objectReferenceValue);
  153. if (m_EffectList != null)
  154. m_EffectList.OnGUI();
  155. }
  156. }
  157. // Copied from UnityEditor.Rendering.PostProcessing.ProfileFactory.CreatePostProcessProfile() because it's internal
  158. static PostProcessProfile CreatePostProcessProfile(UnityEngine.SceneManagement.Scene scene, string targetName)
  159. {
  160. var path = string.Empty;
  161. if (string.IsNullOrEmpty(scene.path))
  162. {
  163. path = "Assets/";
  164. }
  165. else
  166. {
  167. var scenePath = System.IO.Path.GetDirectoryName(scene.path);
  168. var extPath = scene.name + "_Profiles";
  169. var profilePath = scenePath + "/" + extPath;
  170. if (!AssetDatabase.IsValidFolder(profilePath))
  171. AssetDatabase.CreateFolder(scenePath, extPath);
  172. path = profilePath + "/";
  173. }
  174. path += targetName + " Profile.asset";
  175. path = AssetDatabase.GenerateUniqueAssetPath(path);
  176. var profile = ScriptableObject.CreateInstance<PostProcessProfile>();
  177. AssetDatabase.CreateAsset(profile, path);
  178. AssetDatabase.SaveAssets();
  179. AssetDatabase.Refresh();
  180. return profile;
  181. }
  182. }
  183. #endif
  184. }