UICamera.Raycast C# (CSharp) Méthode

Raycast() static public méthode

Returns the object under the specified position.
static public Raycast ( Vector3 inPos, RaycastHit &hit ) : bool
inPos Vector3
hit UnityEngine.RaycastHit
Résultat bool
	static public bool Raycast (Vector3 inPos, ref RaycastHit hit)
	{
		for (int i = 0; i < mList.Count; ++i)
		{
			UICamera cam = mList[i];
			
			// Skip inactive scripts
			if (!cam.enabled || !NGUITools.GetActive(cam.gameObject)) continue;

			// Convert to view space
			currentCamera = cam.cachedCamera;
			Vector3 pos = currentCamera.ScreenToViewportPoint(inPos);

			// If it's outside the camera's viewport, do nothing
			if (pos.x < 0f || pos.x > 1f || pos.y < 0f || pos.y > 1f) continue;

			// Cast a ray into the screen
			Ray ray = currentCamera.ScreenPointToRay(inPos);

			// Raycast into the screen
			int mask = currentCamera.cullingMask & (int)cam.eventReceiverMask;
			float dist = (cam.rangeDistance > 0f) ? cam.rangeDistance : currentCamera.farClipPlane - currentCamera.nearClipPlane;

			// If raycasts should be clipped by panels, we need to find a panel for each hit
			if (cam.clipRaycasts)
			{
				RaycastHit[] hits = Physics.RaycastAll(ray, dist, mask);

				if (hits.Length > 1)
				{
					System.Array.Sort(hits, delegate(RaycastHit r1, RaycastHit r2) { return r1.distance.CompareTo(r2.distance); });

					for (int b = 0, bmax = hits.Length; b < bmax; ++b)
					{
						if (IsVisible(ref hits[b]))
						{
							hit = hits[b];
							return true;
						}
					}
				}
				else if (hits.Length == 1 && IsVisible(ref hits[0]))
				{
					hit = hits[0];
					return true;
				}
				continue;
			}
			if (Physics.Raycast(ray, out hit, dist, mask)) return true;
		}
		return false;
	}

Usage Example

Exemple #1
0
    void Update()
    {
        if (mapControl != null)
        {
            mapControl.Update();

            _mouseStatus.Update();

            if (_mouseStatus.GetMouseJustDown(MouseStatus.KEY.LEFT) &&
                !UICamera.Raycast(Input.mousePosition))
            {
                Vector3 worldPoint;
                Vector3 mousePosition = _mouseStatus.GetMouseJustDownPos();
                bool    raycast       = _campaignCamera.ProjectScreenPointToPlane(out worldPoint, mousePosition);
                if (raycast)
                {
                    int tileIndex = mapControl.DetectTileIndex(worldPoint);
                    if (tileIndex >= 0)
                    {
                        if (_campaignPanel != null)
                        {
                            _campaignPanel.OnSelectMission(tileIndex + 1);
                        }
                    }
                }
            }
        }
    }
All Usage Examples Of UICamera::Raycast