diff options
author | chai <chaifix@163.com> | 2021-05-08 23:15:13 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-05-08 23:15:13 +0800 |
commit | d07e14add74e017b52ab2371efeea1aa4ea10ced (patch) | |
tree | efd07869326e4c428f5bfe43fad0c2583d32a401 /Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ReflectionMethodsCache.cs |
+init
Diffstat (limited to 'Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ReflectionMethodsCache.cs')
-rw-r--r-- | Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ReflectionMethodsCache.cs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ReflectionMethodsCache.cs b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ReflectionMethodsCache.cs new file mode 100644 index 0000000..638fa03 --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ReflectionMethodsCache.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Reflection; + +namespace UnityEngine.UI +{ + internal class ReflectionMethodsCache + { + public delegate bool Raycast3DCallback(Ray r, out RaycastHit hit, float f, int i); + public delegate RaycastHit2D Raycast2DCallback(Vector2 p1, Vector2 p2, float f, int i); + public delegate RaycastHit[] RaycastAllCallback(Ray r, float f, int i); + public delegate RaycastHit2D[] GetRayIntersectionAllCallback(Ray r, float f, int i); + + // We call Physics.Raycast and Physics2D.Raycast through reflection to avoid creating a hard dependency from + // this class to the Physics/Physics2D modules, which would otherwise make it impossible to make content with UI + // without force-including both modules. + public ReflectionMethodsCache() + { + var raycast3DMethodInfo = typeof(Physics).GetMethod("Raycast", new[] {typeof(Ray), typeof(RaycastHit).MakeByRefType(), typeof(float), typeof(int)}); + if (raycast3DMethodInfo != null) + raycast3D = (Raycast3DCallback)UnityEngineInternal.ScriptingUtils.CreateDelegate(typeof(Raycast3DCallback), raycast3DMethodInfo); + + var raycast2DMethodInfo = typeof(Physics2D).GetMethod("Raycast", new[] {typeof(Vector2), typeof(Vector2), typeof(float), typeof(int)}); + if (raycast2DMethodInfo != null) + raycast2D = (Raycast2DCallback)UnityEngineInternal.ScriptingUtils.CreateDelegate(typeof(Raycast2DCallback), raycast2DMethodInfo); + + var raycastAllMethodInfo = typeof(Physics).GetMethod("RaycastAll", new[] {typeof(Ray), typeof(float), typeof(int)}); + if (raycastAllMethodInfo != null) + raycast3DAll = (RaycastAllCallback)UnityEngineInternal.ScriptingUtils.CreateDelegate(typeof(RaycastAllCallback), raycastAllMethodInfo); + + var getRayIntersectionAllMethodInfo = typeof(Physics2D).GetMethod("GetRayIntersectionAll", new[] {typeof(Ray), typeof(float), typeof(int)}); + if (getRayIntersectionAllMethodInfo != null) + getRayIntersectionAll = (GetRayIntersectionAllCallback)UnityEngineInternal.ScriptingUtils.CreateDelegate(typeof(GetRayIntersectionAllCallback), getRayIntersectionAllMethodInfo); + } + + public Raycast3DCallback raycast3D = null; + public RaycastAllCallback raycast3DAll = null; + public Raycast2DCallback raycast2D = null; + public GetRayIntersectionAllCallback getRayIntersectionAll = null; + + private static ReflectionMethodsCache s_ReflectionMethodsCache = null; + + public static ReflectionMethodsCache Singleton + { + get + { + if (s_ReflectionMethodsCache == null) + s_ReflectionMethodsCache = new ReflectionMethodsCache(); + return s_ReflectionMethodsCache; + } + } + }; +} |