/// Helper function to find the Event Executor that is part of /// the input module if one exists in the scene. public static PvrEventExecutor FindEventExecutor() { PvrInputMoudle pvrInputModule = FindInputModule(); if (pvrInputModule == null) { return(null); } return(pvrInputModule.EventExecutor); }
/// Helper function to find the input module if one exists in the /// scene and it is the active module. public static PvrInputMoudle FindInputModule() { if (EventSystem.current == null) { return(null); } EventSystem eventSystem = EventSystem.current; if (eventSystem == null) { return(null); } PvrInputMoudle pvrInputModule = eventSystem.GetComponent <PvrInputMoudle>(); return(pvrInputModule); }
/// <summary> /// 禁用该组件 /// </summary> public void SetDisable() { gameObject.GetComponent <RayController>().enabled = false; IsShowRay(false); PvrInputMoudle.FindInputModule().onRefreshCursor -= RefreshCursor; }
public override void Raycast(PointerEventData eventData, List <RaycastResult> resultAppendList) { if (canvas == null) { Debug.LogError("PvrGraphicRaycaster requires that the game object needs 'Canvas' componet !"); return /*false*/; } if (eventCamera == null) { Debug.LogError("PvrGraphicRaycaster requires that the eventCamera is not null"); return; } if (canvas.renderMode != RenderMode.WorldSpace) { Debug.LogError("PvrGraphicRaycaster requires that the canvas renderMode is set to WorldSpace."); return /*false*/; } Ray ray = PvrInputMoudle.ray; if (!PUI_UnityAPI.isControllerConnected()) { ray = new Ray(eventCamera.transform.position, eventCamera.transform.forward); PvrInputMoudle.ray = ray; } PvrInputMoudle.FindInputModule().Impl.RayDirection = ray.direction; float dist = 20f; MaxPointerEndPoint = ray.GetPoint(dist); Debug.DrawLine(ray.origin, ray.origin + (ray.direction * 5F), Color.red); float hitDistance = float.MaxValue; if (blockingObjects != BlockingObjects.None) /**标记为 None 标签的不需要处理*/ { if (blockingObjects == BlockingObjects.ThreeD || blockingObjects == BlockingObjects.All) { RaycastHit hit; if (Physics.Raycast(ray, out hit, dist, m_BlockingMask)) { hitDistance = hit.distance; //Debug.Log("Hit postition : " + hit.point); //Debug.Log("ditance : " + Vector3.Distance(hit.point, ray.origin) + " Hitdistance : " + hitDistance); } } if (blockingObjects == BlockingObjects.TwoD || blockingObjects == BlockingObjects.All) { RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, dist, m_BlockingMask); if (hit.collider != null) { hitDistance = hit.fraction * dist; } } } m_RaycastResults.Clear(); Raycast(canvas, ray, eventCamera, dist, m_RaycastResults); //Debug.Log("RaycastResult 数量 : " + m_RaycastResults.Count); for (var index = 0; index < m_RaycastResults.Count; index++) { //Debug.Log("RaycastResult 数量 : " + m_RaycastResults.Count + " name "+m_RaycastResults[index].gameObject.name); var go = m_RaycastResults[index].gameObject; bool appendGraphic = true; if (ignoreReversedGraphics) { // If we have a camera compare the direction against the cameras forward. Vector3 cameraFoward = eventCamera.transform.rotation * Vector3.forward; Vector3 dir = go.transform.rotation * Vector3.forward; appendGraphic = Vector3.Dot(cameraFoward, dir) > 0; } if (appendGraphic) { float resultDistance = 0; Transform trans = go.transform; Vector3 transForward = trans.forward; // http://geomalgorithms.com/a06-_intersect-2.html float transDot = Vector3.Dot(transForward, trans.position - ray.origin); float rayDot = Vector3.Dot(transForward, ray.direction); resultDistance = transDot / rayDot; Vector3 hitPosition = ray.origin + (ray.direction * resultDistance); resultDistance = resultDistance + 0; //Debug.Log("resultDistance : "+ resultDistance+ " hitDistance : "+ hitDistance + " dist "+dist); // Check to see if the go is behind the camera. if (resultDistance < 0 || resultDistance >= hitDistance || resultDistance > dist) { continue; } //Transform pointerTransform = // GvrPointerInputModule.Pointer.PointerTransform; //float delta = (hitPosition - pointerTransform.position).magnitude; //if (delta < pointerRay.distanceFromStart) //{ // continue; //} RaycastResult castResult = new RaycastResult { gameObject = go, module = this, distance = resultDistance, worldPosition = hitPosition, screenPosition = eventCamera.WorldToScreenPoint(hitPosition), index = resultAppendList.Count, depth = m_RaycastResults[index].depth, sortingLayer = canvas.sortingLayerID, sortingOrder = canvas.sortingOrder }; resultAppendList.Add(castResult); } } //Debug.Log("resultAppendList.Count : " + resultAppendList.Count); }
// void RefreshCursor(UnityEngine.EventSystems.RaycastResult raycastResult) // { // if(raycastResult.gameObject != null) // { //// if (!point.activeInHierarchy) //// { // point.SetActive(true); // point.transform.position = raycastResult.worldPosition; // float distance = Vector3.Distance(point.transform.position, head.position); // float scale = 0.012f * distance / 3.3f; // point.transform.localScale = scale * Vector3.one; // dot.gameObject.SetActive(false); //// } // } // else // { //// if (point.activeSelf) //// { // point.transform.localScale = Vector3.zero; // point.SetActive(false); // dot.gameObject.SetActive(true); //// } // } // } /// <summary> /// 启用该组件 /// </summary> public void SetEnable() { gameObject.GetComponent <RayController>().enabled = true; IsShowRay(true); PvrInputMoudle.FindInputModule().onRefreshCursor += RefreshCursor; }