Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ScopedDisposable.cs 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using UnityEditor;
  3. namespace UnityEngine.InputSystem
  4. {
  5. // Utility allowing access to object T as well as a dispose delegate to clean-up any resources associated with it.
  6. // Useful with the 'using' keyword to provide scoped RAII-like cleanup of objects in tests without having a
  7. // dedicated fixture handling the clean-up.
  8. internal sealed class ScopedDisposable<T> : IDisposable
  9. where T : UnityEngine.Object
  10. {
  11. private Action<T> m_Dispose;
  12. public ScopedDisposable(T obj, Action<T> dispose)
  13. {
  14. value = obj;
  15. m_Dispose = dispose;
  16. }
  17. public T value
  18. {
  19. get;
  20. private set;
  21. }
  22. public void Dispose()
  23. {
  24. if (m_Dispose == null)
  25. return;
  26. if (value != null)
  27. m_Dispose(value);
  28. m_Dispose = null;
  29. value = null;
  30. }
  31. }
  32. // Convenience API for scoped objects.
  33. internal static class Scoped
  34. {
  35. public static ScopedDisposable<T> Object<T>(T obj) where T : UnityEngine.Object
  36. {
  37. #if UNITY_EDITOR
  38. return new ScopedDisposable<T>(obj, UnityEngine.Object.DestroyImmediate);
  39. #else
  40. return new ScopedDisposable<T>(obj, UnityEngine.Object.Destroy);
  41. #endif
  42. }
  43. #if UNITY_EDITOR
  44. public static ScopedDisposable<T> Asset<T>(T obj) where T : UnityEngine.Object
  45. {
  46. return new ScopedDisposable<T>(obj, (o) => AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(o)));
  47. }
  48. #endif
  49. }
  50. }