Renderer2DDataEditor.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using UnityEngine;
  2. using UnityEngine.Experimental.Rendering.Universal;
  3. namespace UnityEditor.Experimental.Rendering.Universal
  4. {
  5. [CustomEditor(typeof(Renderer2DData), true)]
  6. internal class Renderer2DDataEditor : Editor
  7. {
  8. class Styles
  9. {
  10. public static readonly GUIContent transparencySortMode = EditorGUIUtility.TrTextContent("Transparency Sort Mode", "Default sorting mode used for transparent objects");
  11. public static readonly GUIContent transparencySortAxis = EditorGUIUtility.TrTextContent("Transparency Sort Axis", "Axis used for custom axis sorting mode");
  12. public static readonly GUIContent hdrEmulationScale = EditorGUIUtility.TrTextContent("HDR Emulation Scale", "Describes the scaling used by lighting to remap dynamic range between LDR and HDR");
  13. public static readonly GUIContent lightBlendStyles = EditorGUIUtility.TrTextContent("Light Blend Styles", "A Light Blend Style is a collection of properties that describe a particular way of applying lighting.");
  14. public static readonly GUIContent defaultMaterialType = EditorGUIUtility.TrTextContent("Default Material Type", "Material to use when adding new objects to a scene");
  15. public static readonly GUIContent defaultCustomMaterial = EditorGUIUtility.TrTextContent("Default Custom Material", "Material to use when adding new objects to a scene");
  16. public static readonly GUIContent name = EditorGUIUtility.TrTextContent("Name");
  17. public static readonly GUIContent maskTextureChannel = EditorGUIUtility.TrTextContent("Mask Texture Channel", "Which channel of the mask texture will affect this Light Blend Style.");
  18. public static readonly GUIContent renderTextureScale = EditorGUIUtility.TrTextContent("Render Texture Scale", "The resolution of the lighting buffer relative to the screen resolution. 1.0 means full screen size.");
  19. public static readonly GUIContent blendMode = EditorGUIUtility.TrTextContent("Blend Mode", "How the lighting should be blended with the main color of the objects.");
  20. public static readonly GUIContent customBlendFactors = EditorGUIUtility.TrTextContent("Custom Blend Factors");
  21. public static readonly GUIContent blendFactorMultiplicative = EditorGUIUtility.TrTextContent("Multiplicative");
  22. public static readonly GUIContent blendFactorAdditive = EditorGUIUtility.TrTextContent("Additive");
  23. public static readonly GUIContent useDepthStencilBuffer = EditorGUIUtility.TrTextContent("Use Depth/Stencil Buffer", "Uncheck this when you are certain you don't use any feature that requires the depth/stencil buffer (e.g. Sprite Mask). Not using the depth/stencil buffer may improve performance, especially on mobile platforms.");
  24. public static readonly GUIContent postProcessData = EditorGUIUtility.TrTextContent("Post-processing Data", "Resources (textures, shaders, etc.) required by post-processing effects.");
  25. }
  26. struct LightBlendStyleProps
  27. {
  28. public SerializedProperty name;
  29. public SerializedProperty maskTextureChannel;
  30. public SerializedProperty renderTextureScale;
  31. public SerializedProperty blendMode;
  32. public SerializedProperty blendFactorMultiplicative;
  33. public SerializedProperty blendFactorAdditive;
  34. }
  35. SerializedProperty m_TransparencySortMode;
  36. SerializedProperty m_TransparencySortAxis;
  37. SerializedProperty m_HDREmulationScale;
  38. SerializedProperty m_LightBlendStyles;
  39. LightBlendStyleProps[] m_LightBlendStylePropsArray;
  40. SerializedProperty m_UseDepthStencilBuffer;
  41. SerializedProperty m_PostProcessData;
  42. SerializedProperty m_DefaultMaterialType;
  43. SerializedProperty m_DefaultCustomMaterial;
  44. Analytics.Renderer2DAnalytics m_Analytics = Analytics.Renderer2DAnalytics.instance;
  45. Renderer2DData m_Renderer2DData;
  46. bool m_WasModified;
  47. void SendModifiedAnalytics(Analytics.IAnalytics analytics)
  48. {
  49. if (m_WasModified)
  50. {
  51. Analytics.RendererAssetData modifiedData = new Analytics.RendererAssetData();
  52. modifiedData.instance_id = m_Renderer2DData.GetInstanceID();
  53. modifiedData.was_create_event = false;
  54. modifiedData.blending_layers_count = 0;
  55. modifiedData.blending_modes_used = 0;
  56. analytics.SendData(Analytics.AnalyticsDataTypes.k_Renderer2DDataString, modifiedData);
  57. }
  58. }
  59. void OnEnable()
  60. {
  61. m_WasModified = false;
  62. m_Renderer2DData = (Renderer2DData)serializedObject.targetObject;
  63. m_TransparencySortMode = serializedObject.FindProperty("m_TransparencySortMode");
  64. m_TransparencySortAxis = serializedObject.FindProperty("m_TransparencySortAxis");
  65. m_HDREmulationScale = serializedObject.FindProperty("m_HDREmulationScale");
  66. m_LightBlendStyles = serializedObject.FindProperty("m_LightBlendStyles");
  67. int numBlendStyles = m_LightBlendStyles.arraySize;
  68. m_LightBlendStylePropsArray = new LightBlendStyleProps[numBlendStyles];
  69. for (int i = 0; i < numBlendStyles; ++i)
  70. {
  71. SerializedProperty blendStyleProp = m_LightBlendStyles.GetArrayElementAtIndex(i);
  72. ref LightBlendStyleProps props = ref m_LightBlendStylePropsArray[i];
  73. props.name = blendStyleProp.FindPropertyRelative("name");
  74. props.maskTextureChannel = blendStyleProp.FindPropertyRelative("maskTextureChannel");
  75. props.renderTextureScale = blendStyleProp.FindPropertyRelative("renderTextureScale");
  76. props.blendMode = blendStyleProp.FindPropertyRelative("blendMode");
  77. props.blendFactorMultiplicative = blendStyleProp.FindPropertyRelative("customBlendFactors.multiplicative");
  78. props.blendFactorAdditive = blendStyleProp.FindPropertyRelative("customBlendFactors.additive");
  79. if (props.blendFactorMultiplicative == null)
  80. props.blendFactorMultiplicative = blendStyleProp.FindPropertyRelative("customBlendFactors.modulate");
  81. if (props.blendFactorAdditive == null)
  82. props.blendFactorAdditive = blendStyleProp.FindPropertyRelative("customBlendFactors.additve");
  83. }
  84. m_UseDepthStencilBuffer = serializedObject.FindProperty("m_UseDepthStencilBuffer");
  85. m_PostProcessData = serializedObject.FindProperty("m_PostProcessData");
  86. m_DefaultMaterialType = serializedObject.FindProperty("m_DefaultMaterialType");
  87. m_DefaultCustomMaterial = serializedObject.FindProperty("m_DefaultCustomMaterial");
  88. }
  89. private void OnDestroy()
  90. {
  91. SendModifiedAnalytics(m_Analytics);
  92. }
  93. public override void OnInspectorGUI()
  94. {
  95. serializedObject.Update();
  96. EditorGUI.BeginChangeCheck();
  97. EditorGUILayout.PropertyField(m_TransparencySortMode, Styles.transparencySortMode);
  98. if(m_TransparencySortMode.intValue == (int)TransparencySortMode.CustomAxis)
  99. EditorGUILayout.PropertyField(m_TransparencySortAxis, Styles.transparencySortAxis);
  100. EditorGUILayout.PropertyField(m_HDREmulationScale, Styles.hdrEmulationScale);
  101. if (EditorGUI.EndChangeCheck() && m_HDREmulationScale.floatValue < 1.0f)
  102. m_HDREmulationScale.floatValue = 1.0f;
  103. EditorGUILayout.LabelField(Styles.lightBlendStyles);
  104. EditorGUI.indentLevel++;
  105. EditorGUI.BeginChangeCheck();
  106. int numBlendStyles = m_LightBlendStyles.arraySize;
  107. for (int i = 0; i < numBlendStyles; ++i)
  108. {
  109. SerializedProperty blendStyleProp = m_LightBlendStyles.GetArrayElementAtIndex(i);
  110. ref LightBlendStyleProps props = ref m_LightBlendStylePropsArray[i];
  111. EditorGUILayout.BeginHorizontal();
  112. blendStyleProp.isExpanded = EditorGUILayout.Foldout(blendStyleProp.isExpanded, props.name.stringValue, true);
  113. EditorGUILayout.EndHorizontal();
  114. if (blendStyleProp.isExpanded)
  115. {
  116. EditorGUI.indentLevel++;
  117. EditorGUILayout.PropertyField(props.name, Styles.name);
  118. EditorGUILayout.PropertyField(props.maskTextureChannel, Styles.maskTextureChannel);
  119. EditorGUILayout.PropertyField(props.renderTextureScale, Styles.renderTextureScale);
  120. EditorGUILayout.PropertyField(props.blendMode, Styles.blendMode);
  121. if (props.blendMode.intValue == (int)Light2DBlendStyle.BlendMode.Custom)
  122. {
  123. EditorGUILayout.BeginHorizontal();
  124. EditorGUI.indentLevel++;
  125. EditorGUILayout.LabelField(Styles.customBlendFactors, GUILayout.MaxWidth(200.0f));
  126. EditorGUI.indentLevel--;
  127. int oldIndentLevel = EditorGUI.indentLevel;
  128. EditorGUI.indentLevel = 0;
  129. EditorGUIUtility.labelWidth = 80.0f;
  130. EditorGUILayout.PropertyField(props.blendFactorMultiplicative, Styles.blendFactorMultiplicative, GUILayout.MinWidth(110.0f));
  131. GUILayout.Space(10.0f);
  132. EditorGUIUtility.labelWidth = 50.0f;
  133. EditorGUILayout.PropertyField(props.blendFactorAdditive, Styles.blendFactorAdditive, GUILayout.MinWidth(90.0f));
  134. EditorGUIUtility.labelWidth = 0.0f;
  135. EditorGUI.indentLevel = oldIndentLevel;
  136. EditorGUILayout.EndHorizontal();
  137. }
  138. EditorGUI.indentLevel--;
  139. }
  140. }
  141. EditorGUI.indentLevel--;
  142. EditorGUILayout.PropertyField(m_UseDepthStencilBuffer, Styles.useDepthStencilBuffer);
  143. EditorGUILayout.PropertyField(m_PostProcessData, Styles.postProcessData);
  144. EditorGUILayout.PropertyField(m_DefaultMaterialType, Styles.defaultMaterialType);
  145. if(m_DefaultMaterialType.intValue == (int)Renderer2DData.Renderer2DDefaultMaterialType.Custom)
  146. EditorGUILayout.PropertyField(m_DefaultCustomMaterial, Styles.defaultCustomMaterial);
  147. m_WasModified |= serializedObject.hasModifiedProperties;
  148. serializedObject.ApplyModifiedProperties();
  149. }
  150. }
  151. }