/// <summary> /// 替换自定义射线 /// </summary> public static void AddRay(ICustomRay ray) { if (!_rayList.Contains(ray)) { _rayList.Add(ray); Sort(); } }
/// <summary> /// Perform the raycast against the list of graphics associated with the Canvas. /// </summary> /// <param name="eventData">Current event data</param> /// <param name="resultAppendList">List of hit objects to append new results to.</param> public override void Raycast(PointerEventData eventData, List <RaycastResult> resultAppendList) { //<Ming> Modify : Canvas必不为空(若为空则为开发时异常!),节省canvas判空消耗(UnityEngine.Object重载了==和!=) //if (_canvas == null) // return; Camera cacheCamera = eventCamera; var canvasGraphics = GraphicRegistry.GetGraphicsForCanvas(_canvas); if (canvasGraphics == null || canvasGraphics.Count == 0) { return; } Vector2 eventPosition = eventData.position; ICustomRay customRay = RayManager.CurrentRay; if (customRay != null) { eventPosition = GetEventPosition(customRay.Ray, cacheCamera, eventData); } // Convert to view space Vector2 pos; if (cacheCamera == null) { float w = Screen.width; float h = Screen.height; pos = new Vector2(eventPosition.x / w, eventPosition.y / h); } else { pos = cacheCamera.ScreenToViewportPoint(eventPosition); } // If it's outside the camera's viewport, do nothing if (pos.x < 0f || pos.x > 1f || pos.y < 0f || pos.y > 1f) { return; } Ray ray = cacheCamera.ScreenPointToRay(eventPosition); _raycastResults.Clear(); float distance = 0; Graphic graphic = Raycast(_canvas, cacheCamera, eventPosition, canvasGraphics, ray, out distance); if (graphic != null) { var castResult = new RaycastResult { gameObject = graphic.gameObject, module = this, distance = distance, screenPosition = eventPosition, index = resultAppendList.Count, depth = graphic.depth, sortingLayer = _canvas.sortingLayerID, sortingOrder = _canvas.sortingOrder, worldPosition = ray.origin + ray.direction * distance, worldNormal = -graphic.transform.forward }; resultAppendList.Add(castResult); } }
/// <summary> /// Hide the default proxy ray/cone /// </summary> /// <param name="rayOrigin">The ray to hide or show</param> /// <param name="rayOnly">An optional parameter to hide or show only the ray</param> public static void HideDefaultRay(this ICustomRay customRay, Transform rayOrigin, bool rayOnly = false) { hideDefaultRay(rayOrigin, rayOnly); }
/// <summary> /// 销毁当前自定义射线 /// </summary> public static void RemoveRay(ICustomRay ray) { _rayList.Remove(ray); }