No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShadowCaster2DEditor.cs 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using UnityEditor.EditorTools;
  2. using UnityEditor.Rendering.Universal.Path2D;
  3. using UnityEngine;
  4. using UnityEngine.Rendering.Universal;
  5. namespace UnityEditor.Rendering.Universal
  6. {
  7. internal class ShadowCasterPath : ScriptablePath
  8. {
  9. internal Bounds GetBounds()
  10. {
  11. ShadowCaster2D shadowCaster = (ShadowCaster2D)owner;
  12. Renderer m_Renderer = shadowCaster.GetComponent<Renderer>();
  13. if (m_Renderer != null)
  14. {
  15. return m_Renderer.bounds;
  16. }
  17. else
  18. {
  19. Collider2D collider = shadowCaster.GetComponent<Collider2D>();
  20. if (collider != null)
  21. return collider.bounds;
  22. }
  23. return new Bounds(shadowCaster.transform.position, shadowCaster.transform.lossyScale);
  24. }
  25. public override void SetDefaultShape()
  26. {
  27. Clear();
  28. Bounds bounds = GetBounds();
  29. AddPoint(new ControlPoint(bounds.min));
  30. AddPoint(new ControlPoint(new Vector3(bounds.min.x, bounds.max.y)));
  31. AddPoint(new ControlPoint(bounds.max));
  32. AddPoint(new ControlPoint(new Vector3(bounds.max.x, bounds.min.y)));
  33. base.SetDefaultShape();
  34. }
  35. }
  36. [CustomEditor(typeof(ShadowCaster2D))]
  37. [CanEditMultipleObjects]
  38. internal class ShadowCaster2DEditor : PathComponentEditor<ShadowCasterPath>
  39. {
  40. [EditorTool("Edit Shadow Caster Shape", typeof(ShadowCaster2D))]
  41. class ShadowCaster2DShadowCasterShapeTool : ShadowCaster2DShapeTool {};
  42. private static class Styles
  43. {
  44. public static GUIContent shadowShape2DProvider = EditorGUIUtility.TrTextContent("Shadow Shape 2D Provider", "");
  45. public static GUIContent castsShadows = EditorGUIUtility.TrTextContent("Casts Shadows", "Specifies if this renderer will cast shadows");
  46. public static GUIContent castingSourcePrefixLabel = EditorGUIUtility.TrTextContent("Casting Source", "Specifies the source used for projected shadows");
  47. public static GUIContent sortingLayerPrefixLabel = EditorGUIUtility.TrTextContent("Target Sorting Layers", "Apply shadows to the specified sorting layers.");
  48. public static GUIContent shadowShapeTrim = EditorGUIUtility.TrTextContent("Trim Edge", "This contracts the edge of the shape given by the shape provider by the specified amount");
  49. public static GUIContent alphaCutoff = EditorGUIUtility.TrTextContent("Alpha Cutoff", "Required for correct unshadowed sprite overlap.");
  50. public static GUIContent castingOption = EditorGUIUtility.TrTextContent("Casting Option", "Specifies how to draw the shadow used with the ShadowCaster2D");
  51. public static GUIContent castingSource = EditorGUIUtility.TrTextContent("Casting Source", "Specifies the source of the shape used for projected shadows");
  52. }
  53. SerializedProperty m_CastingOption;
  54. SerializedProperty m_CastsShadows;
  55. SerializedProperty m_CastingSource;
  56. SerializedProperty m_ShadowMesh;
  57. SerializedProperty m_TrimEdge;
  58. SerializedProperty m_AlphaCutoff;
  59. SerializedProperty m_ShadowShape2DProvider;
  60. SortingLayerDropDown m_SortingLayerDropDown;
  61. CastingSourceDropDown m_CastingSourceDropDown;
  62. public void OnEnable()
  63. {
  64. m_CastingOption = serializedObject.FindProperty("m_CastingOption");
  65. m_CastsShadows = serializedObject.FindProperty("m_CastsShadows");
  66. m_CastingSource = serializedObject.FindProperty("m_ShadowCastingSource");
  67. m_ShadowMesh = serializedObject.FindProperty("m_ShadowMesh");
  68. m_AlphaCutoff = serializedObject.FindProperty("m_AlphaCutoff");
  69. m_TrimEdge = m_ShadowMesh.FindPropertyRelative("m_TrimEdge");
  70. m_ShadowShape2DProvider = serializedObject.FindProperty("m_ShadowShape2DProvider");
  71. m_SortingLayerDropDown = new SortingLayerDropDown();
  72. m_SortingLayerDropDown.OnEnable(serializedObject, "m_ApplyToSortingLayers");
  73. m_CastingSourceDropDown = new CastingSourceDropDown();
  74. }
  75. public void ShadowCaster2DSceneGUI()
  76. {
  77. ShadowCaster2D shadowCaster = target as ShadowCaster2D;
  78. Transform t = shadowCaster.transform;
  79. shadowCaster.DrawPreviewOutline();
  80. }
  81. public void ShadowCaster2DInspectorGUI<T>() where T : ShadowCaster2DShapeTool
  82. {
  83. DoEditButton<T>(PathEditorToolContents.icon, "Edit Shape");
  84. DoPathInspector<T>();
  85. DoSnappingInspector<T>();
  86. }
  87. public void OnSceneGUI()
  88. {
  89. if (m_CastsShadows.boolValue)
  90. ShadowCaster2DSceneGUI();
  91. }
  92. public override void OnInspectorGUI()
  93. {
  94. serializedObject.Update();
  95. m_CastingSourceDropDown.OnCastingSource(serializedObject, targets, Styles.castingSourcePrefixLabel);
  96. EditorGUILayout.PropertyField(m_CastingOption, Styles.castingOption);
  97. m_SortingLayerDropDown.OnTargetSortingLayers(serializedObject, targets, Styles.sortingLayerPrefixLabel, null);
  98. bool usingShapeProvider = m_CastingSource.intValue == (int)ShadowCaster2D.ShadowCastingSources.ShapeProvider;
  99. if (usingShapeProvider)
  100. {
  101. EditorGUILayout.PropertyField(m_TrimEdge, Styles.shadowShapeTrim);
  102. if (m_TrimEdge.floatValue < 0)
  103. m_TrimEdge.floatValue = 0;
  104. EditorGUILayout.PropertyField(m_AlphaCutoff, Styles.alphaCutoff);
  105. }
  106. if ((ShadowCaster2D.ShadowCastingSources)m_CastingSource.intValue == ShadowCaster2D.ShadowCastingSources.ShapeEditor)
  107. ShadowCaster2DInspectorGUI<ShadowCaster2DShadowCasterShapeTool>();
  108. else if (EditorToolManager.IsActiveTool<ShadowCaster2DShadowCasterShapeTool>())
  109. ToolManager.RestorePreviousTool();
  110. EditorGUILayout.PropertyField(m_ShadowShape2DProvider, Styles.shadowShape2DProvider, true);
  111. serializedObject.ApplyModifiedProperties();
  112. }
  113. }
  114. }