Exemplo n.º 1
0
        public void UpdateCameraPosition(UILaser uiLaser)
        {
            if (uiLaser.isActiveAndEnabled)
            {
                UICamera.enabled            = true;
                UICamera.transform.position = uiLaser.transform.position;
                UICamera.transform.rotation = uiLaser.transform.rotation;

                uiLaser.GetPointerEvent().position =
                    new Vector2(UICamera.pixelWidth * 0.5f, UICamera.pixelHeight * 0.5f);
            }
            else if (UICamera != null)
            {
                UICamera.enabled = false;
            }
        }
Exemplo n.º 2
0
        // Use this for initialization
        protected override void Start()
        {
            base.Start();

            foreach (LaserBase laser in GetComponentsInChildren <LaserBase>(true))
            {
                if (!lasers.Contains(laser))
                {
                    lasers.Add(laser);
                }
                if (laser.GetComponent <UILaser>() != null)
                {
                    uiLaser = laser.GetComponent <UILaser> ();
                }
            }
        }
Exemplo n.º 3
0
        public bool ScanForObjects(UILaser uiLaser, float maxDistance)
        {
            //				if (uiLaser.IsConnectedBeam())
            //				{
            //					#if UNITY_EDITOR
            //					if (debugLaserTracking)
            //					{
            //						Debug.Log("Is Connected Beam. Ignore");
            //					}
            //					#endif
            ////					controller.ClearButtonFlags ();
            //					continue;
            //				}

            // Test if UICamera is looking at a GUI element
            UpdateCameraPosition(uiLaser);

            if (uiLaser.pointerEvent == null)
            {
                uiLaser.pointerEvent = new PointerEventData(eventSystem);
            }
            else
            {
                uiLaser.pointerEvent.Reset();
            }

            uiLaser.pointerEvent.delta = Vector2.zero;

            if (UICamera != null && UICamera.enabled)
            {
                                #if UNITY_EDITOR
                if (debugLaserTracking)
                {
//					Debug.Log("UI Camera is enabled");
                }
                                #endif
                uiLaser.pointerEvent.position = new Vector2(UICamera.pixelWidth * 0.5f, UICamera.pixelHeight * 0.5f);
            }
            else
            {
                                #if UNITY_EDITOR
                if (debugLaserTracking)
                {
//					Debug.Log("No UI Camera is enabled");
                }
                                #endif
                ClearSelection();
                uiLaser.pointerEvent.position = new Vector2(-1, -1);
            }

            // trigger a raycast
            eventSystem.RaycastAll(uiLaser.pointerEvent, m_RaycastResultCache);

                        #if UNITY_EDITOR
            if (debugLaserTracking)
            {
//				Debug.Log ("Raycast size: " + m_RaycastResultCache.Count);
            }
                        #endif

            if (m_RaycastResultCache.Count > 0)
            {
                // Finds the shortest raycast if there are multiple
                //
                RaycastResult tempRaycast = m_RaycastResultCache [0];
                for (int i = 1; i < m_RaycastResultCache.Count; i++)
                {
                    if (!IsSelectableObject(tempRaycast.gameObject) ||
                        (m_RaycastResultCache [i].distance < tempRaycast.distance))
                    {
                        if (IsSelectableObject(m_RaycastResultCache [i].gameObject))
                        {
                            if (m_RaycastResultCache[i].distance <= maxDistance)
                            {
                                tempRaycast = m_RaycastResultCache[i];
#if UNITY_EDITOR
                                if (debugLaserTracking)
                                {
                                    Debug.Log("Raycast: " + m_RaycastResultCache[i].gameObject.transform.parent.name);
                                }
#endif
                            }
                        }
                    }
                }

                uiLaser.pointerEvent.pointerCurrentRaycast = tempRaycast;
            }
            else
            {
                uiLaser.pointerEvent.pointerCurrentRaycast = FindFirstRaycast(m_RaycastResultCache);
            }

            m_RaycastResultCache.Clear();

            //				 stop if no UI element was hit
            if (uiLaser.pointerEvent.pointerCurrentRaycast.gameObject == null)
            {
                uiLaser.hitObject         = null;
                uiLaser.hitObjectDistance = float.MaxValue;
                return(false);
            }

            uiLaser.hitObject         = uiLaser.pointerEvent.pointerCurrentRaycast.gameObject;
            uiLaser.hitObjectDistance = uiLaser.pointerEvent.pointerCurrentRaycast.distance;
            return(true);
        }