--------------------------------------Raycast相关----------------------------
------------------光线投射结果--------------- 1 光线投射的对象 2 光线投射的模块 3 光线投射的距离,深度,层,层内号,
4 光线投射的世界坐标,世界坐标法线,屏幕坐标 5 检测光线投射结果是否有效 6 清空光线投射 public struct RaycastResult {
private GameObject m_gameObject; public GameObject gameObject { get{return
m_gameObject;} set{m_gameObject=value;} } public BaseRaycaster module; public
float distance; public float index; public int depth; public int sortingLayer;
public int sortingOrder; public Vector3 worldPosition; public Vector3
worldNormal; public Vector2 screenPosition; public bool isValid{ get{return
module!=null&&m_gameobject!=null} } } ------------------光线投射管理器-------------- 1
对所有光线投射类进行注册移除注册 2 获取所有光线投射 internal static class RaycasterManager private
static readonly List<BaseRaycaster> s_Raycasters = new List<BaseRaycaster>();
public static void AddRaycaster(BaseRaycaster baseRaycaster); public static
List<BaseRaycaster> GetRaycasters() public static void
RemoveRaycaster(BaseRaycaster baseRaycaster)
-----------BaseRaycaster射线碰撞检测基类-------- 1 光线投射基类 2 光线投射相机 光线检测碰撞,层号,渲染号 public
abstract class BaseRaycaster : UIBehaviour public abstract void
Raycaster(PointerEventData eventData,List<RaycastResult> resultAppendList)
public abstarct Camera eventCamera{get;} public virtual int sortOrderPriority
public virtual int renderOrderPriority public string ToString()
--------------------检测Graphic类型对象的射线碰撞------------------ 1
Canvas需要和GraphicRaycaster同时存在,通过不同的Canvas将Graphic进行存储 2 类GraphicRegistry中只读字段
private readonly Dictionary<Canvas,IndexSet<Graphic>> m_Graphics
中以Canvas为组将Graphic对象进行分别存储,以便获取每个Graphic对象 3 GraphicRegistry中提供静态函数
RegisterGraphicForCanvas,UnregisterGraphicForCanvas
,GetGraphicsForCanvas对Canvas下的Graphic进行管理 4
在Raycast方法中检测射线的碰撞时使用反射获取类的实例方法这个点需要注意,其次使用unity中不常用的 类创建委托
UnityEngineInternal.ScriptingUtils.CreateDelegate(typeof(RaycastAllCallback),
raycastAllMethodInfo); public class GraphicRaycaster:BaseRaycaster 属性 1 Canvas
canvas{get;} //获取Canvas组件 2 int sortOrderPriority //获取Canvas的分类层级顺序 3 int
renderOrderPriority //获取渲染层级顺序 4 bool ignoreReversedGraphics //是否忽略反面的事件检测 5
public override Camera evnetCamera //获取光线投射的事件相机 6 public BlockingObjects
blockingObjects //设置碰撞检测的是2d,3d还是两者都检测 字段 protected LayerMask m_BlockingMask =
kNoEventMaskSet; //设置射线碰撞检测的层 private List<Graphic> m_RaycastResults = new
List<Graphic>(); //经过射线碰撞检测的结果存储在该集合中 static readonly List<Graphic>
s_SortedGraphics = new List<Graphic>(); 方法 //重写基类的方法 public override void
Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
//判断符合条件的Graphic对象 private static void Raycast(Canvas canvas, Camera
eventCamera, Vector2 pointerPosition, IList<Graphic> foundGraphics,
List<Graphic> results)
热门工具 换一换