ReflectionMethodsCache.cs 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. namespace UnityEngine.UI
  5. {
  6. internal class ReflectionMethodsCache
  7. {
  8. #if PACKAGE_PHYSICS
  9. public delegate bool Raycast3DCallback(Ray r, out RaycastHit hit, float f, int i);
  10. public delegate RaycastHit[] RaycastAllCallback(Ray r, float f, int i);
  11. public delegate int GetRaycastNonAllocCallback(Ray r, RaycastHit[] results, float f, int i);
  12. public Raycast3DCallback raycast3D = null;
  13. public RaycastAllCallback raycast3DAll = null;
  14. public GetRaycastNonAllocCallback getRaycastNonAlloc = null;
  15. #endif
  16. #if PACKAGE_PHYSICS2D
  17. public delegate RaycastHit2D Raycast2DCallback(Vector2 p1, Vector2 p2, float f, int i);
  18. public delegate RaycastHit2D[] GetRayIntersectionAllCallback(Ray r, float f, int i);
  19. public delegate int GetRayIntersectionAllNonAllocCallback(Ray r, RaycastHit2D[] results, float f, int i);
  20. public Raycast2DCallback raycast2D = null;
  21. public GetRayIntersectionAllCallback getRayIntersectionAll = null;
  22. public GetRayIntersectionAllNonAllocCallback getRayIntersectionAllNonAlloc = null;
  23. #endif
  24. // We call Physics.Raycast and Physics2D.Raycast through reflection to avoid creating a hard dependency from
  25. // this class to the Physics/Physics2D modules, which would otherwise make it impossible to make content with UI
  26. // without force-including both modules.
  27. //
  28. // *NOTE* If other methods are required ensure to add [RequiredByNativeCode] to the bindings for that function. It prevents
  29. // the function from being stripped if required. See Dynamics.bindings.cs for examples (search for GraphicRaycaster.cs).
  30. public ReflectionMethodsCache()
  31. {
  32. #if PACKAGE_PHYSICS
  33. var raycast3DMethodInfo = typeof(Physics).GetMethod("Raycast", new[] {typeof(Ray), typeof(RaycastHit).MakeByRefType(), typeof(float), typeof(int)});
  34. if (raycast3DMethodInfo != null)
  35. raycast3D = (Raycast3DCallback)Delegate.CreateDelegate(typeof(Raycast3DCallback), raycast3DMethodInfo);
  36. var raycastAllMethodInfo = typeof(Physics).GetMethod("RaycastAll", new[] {typeof(Ray), typeof(float), typeof(int)});
  37. if (raycastAllMethodInfo != null)
  38. raycast3DAll = (RaycastAllCallback)Delegate.CreateDelegate(typeof(RaycastAllCallback), raycastAllMethodInfo);
  39. var getRaycastAllNonAllocMethodInfo = typeof(Physics).GetMethod("RaycastNonAlloc", new[] { typeof(Ray), typeof(RaycastHit[]), typeof(float), typeof(int) });
  40. if (getRaycastAllNonAllocMethodInfo != null)
  41. getRaycastNonAlloc = (GetRaycastNonAllocCallback)Delegate.CreateDelegate(typeof(GetRaycastNonAllocCallback), getRaycastAllNonAllocMethodInfo);
  42. #endif
  43. #if PACKAGE_PHYSICS2D
  44. var raycast2DMethodInfo = typeof(Physics2D).GetMethod("Raycast", new[] { typeof(Vector2), typeof(Vector2), typeof(float), typeof(int) });
  45. if (raycast2DMethodInfo != null)
  46. raycast2D = (Raycast2DCallback)Delegate.CreateDelegate(typeof(Raycast2DCallback), raycast2DMethodInfo);
  47. var getRayIntersectionAllMethodInfo = typeof(Physics2D).GetMethod("GetRayIntersectionAll", new[] {typeof(Ray), typeof(float), typeof(int)});
  48. if (getRayIntersectionAllMethodInfo != null)
  49. getRayIntersectionAll = (GetRayIntersectionAllCallback)Delegate.CreateDelegate(typeof(GetRayIntersectionAllCallback), getRayIntersectionAllMethodInfo);
  50. var getRayIntersectionAllNonAllocMethodInfo = typeof(Physics2D).GetMethod("GetRayIntersectionNonAlloc", new[] { typeof(Ray), typeof(RaycastHit2D[]), typeof(float), typeof(int) });
  51. if (getRayIntersectionAllNonAllocMethodInfo != null)
  52. getRayIntersectionAllNonAlloc = (GetRayIntersectionAllNonAllocCallback)Delegate.CreateDelegate(typeof(GetRayIntersectionAllNonAllocCallback), getRayIntersectionAllNonAllocMethodInfo);
  53. #endif
  54. }
  55. private static ReflectionMethodsCache s_ReflectionMethodsCache = null;
  56. public static ReflectionMethodsCache Singleton
  57. {
  58. get
  59. {
  60. if (s_ReflectionMethodsCache == null)
  61. s_ReflectionMethodsCache = new ReflectionMethodsCache();
  62. return s_ReflectionMethodsCache;
  63. }
  64. }
  65. }
  66. }