SerializableObjHelper.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Linq.Expressions;
  3. namespace UnityEditor.Recorder
  4. {
  5. static class SerializableObjHelper
  6. {
  7. public static SerializedProperty FindPropertyRelative(this SerializedProperty obj, Expression<Func<object>> exp)
  8. {
  9. var body = exp.Body as MemberExpression;
  10. if (body == null)
  11. {
  12. var ubody = (UnaryExpression)exp.Body;
  13. body = ubody.Operand as MemberExpression;
  14. }
  15. var name = body.Member.Name;
  16. return obj.FindPropertyRelative(name);
  17. }
  18. }
  19. class PropertyFinder<TType> where TType : class
  20. {
  21. SerializedObject m_Obj;
  22. public PropertyFinder(SerializedObject obj)
  23. {
  24. m_Obj = obj;
  25. }
  26. public delegate TResult FuncX<TResult>(TType x);
  27. public SerializedProperty Find( Expression<FuncX<object>> exp)
  28. {
  29. var body = exp.Body as MemberExpression;
  30. if (body == null)
  31. {
  32. var ubody = (UnaryExpression)exp.Body;
  33. body = ubody.Operand as MemberExpression;
  34. }
  35. var name = body.Member.Name;
  36. return m_Obj.FindProperty(name);
  37. }
  38. }
  39. }