UIEdgeFadeEffectEditor.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Reflection;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using EdgeType = UIEdgeFadeEffect.EdgeType;
  6. [CustomEditor(typeof(UIEdgeFadeEffect), true)]
  7. [CanEditMultipleObjects]
  8. public class UIRawImageEdgeFadeEditor : Editor
  9. {
  10. SerializedProperty m_EdgeType;
  11. SerializedProperty m_EdgeValue;
  12. GUIContent m_EdgeTypeContent;
  13. MethodInfo methodInfo;
  14. protected virtual void OnEnable()
  15. {
  16. m_EdgeType = serializedObject.FindProperty("m_EdgeType");
  17. m_EdgeValue = serializedObject.FindProperty("m_EdgeValue");
  18. m_EdgeTypeContent = EditorGUIUtility.TrTextContent("Edge Type");
  19. methodInfo = typeof(UIEdgeFadeEffect).GetMethod("SetDirty", BindingFlags.NonPublic | BindingFlags.Instance);
  20. }
  21. public override void OnInspectorGUI()
  22. {
  23. serializedObject.Update();
  24. EditorGUI.BeginChangeCheck();
  25. int tempValueIndex = EditorGUILayout.Popup(m_EdgeTypeContent, (m_EdgeType.hasMultipleDifferentValues ? -1 : m_EdgeType.enumValueIndex), m_EdgeType.enumDisplayNames);
  26. if (EditorGUI.EndChangeCheck())
  27. {
  28. if (tempValueIndex != m_EdgeType.enumValueIndex)
  29. {
  30. m_EdgeType.enumValueIndex = tempValueIndex;
  31. }
  32. }
  33. EditorGUI.BeginChangeCheck();
  34. var edgeType = (EdgeType)Enum.GetValues (typeof (EdgeType)).GetValue (m_EdgeType.enumValueIndex);
  35. float tempEdgeValue = m_EdgeValue.floatValue;
  36. if (edgeType == EdgeType.UV)
  37. {
  38. tempEdgeValue = EditorGUILayout.Slider("Edge Value", Mathf.Clamp01(tempEdgeValue), 0, 1);
  39. }
  40. else
  41. {
  42. tempEdgeValue = EditorGUILayout.FloatField("Edge Value", m_EdgeValue.floatValue);
  43. }
  44. if (EditorGUI.EndChangeCheck())
  45. {
  46. if (tempEdgeValue != m_EdgeValue.floatValue)
  47. {
  48. m_EdgeValue.floatValue = tempEdgeValue;
  49. }
  50. }
  51. if (serializedObject.ApplyModifiedProperties())
  52. {
  53. if (methodInfo != null)
  54. {
  55. foreach (UIEdgeFadeEffect item in targets)
  56. {
  57. if (item)
  58. {
  59. methodInfo.Invoke(item, null);
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }