BaseEditor.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Linq.Expressions;
  3. using UnityEngine;
  4. using UnityEngine.Rendering.PostProcessing;
  5. namespace UnityEditor.Rendering.PostProcessing
  6. {
  7. /// <summary>
  8. /// Small wrapper on top of <see cref="Editor"/> to ease the access of the underlying component
  9. /// and its serialized fields.
  10. /// </summary>
  11. /// <typeparam name="T">The type of the target component to make an editor for</typeparam>
  12. /// <example>
  13. /// <code>
  14. /// public class MyMonoBehaviour : MonoBehaviour
  15. /// {
  16. /// public float myProperty = 1.0f;
  17. /// }
  18. ///
  19. /// [CustomEditor(typeof(MyMonoBehaviour))]
  20. /// public sealed class MyMonoBehaviourEditor : BaseEditor&lt;MyMonoBehaviour&gt;
  21. /// {
  22. /// SerializedProperty m_MyProperty;
  23. ///
  24. /// void OnEnable()
  25. /// {
  26. /// m_MyProperty = FindProperty(x => x.myProperty);
  27. /// }
  28. ///
  29. /// public override void OnInspectorGUI()
  30. /// {
  31. /// EditorGUILayout.PropertyField(m_MyProperty);
  32. /// }
  33. /// }
  34. /// </code>
  35. /// </example>
  36. public class BaseEditor<T> : Editor
  37. where T : MonoBehaviour
  38. {
  39. /// <summary>
  40. /// The target component.
  41. /// </summary>
  42. protected T m_Target
  43. {
  44. get { return (T)target; }
  45. }
  46. /// <summary>
  47. ///
  48. /// </summary>
  49. /// <typeparam name="TValue"></typeparam>
  50. /// <param name="expr"></param>
  51. /// <returns></returns>
  52. protected SerializedProperty FindProperty<TValue>(Expression<Func<T, TValue>> expr)
  53. {
  54. return serializedObject.FindProperty(RuntimeUtilities.GetFieldPath(expr));
  55. }
  56. }
  57. }