Ejemplo n.º 1
0
    public Vector3 MapPointToWorldPoint(Vector3 mousePosition)
    {
        Rect screenSpaceRect = ZetanUtility.GetScreenSpaceRect(UI.mapRect);

        Vector3[] corners = new Vector3[4];
        UI.mapRect.GetWorldCorners(corners);
        Vector2 mapViewportPoint = new Vector2((mousePosition.x - corners[0].x) / screenSpaceRect.width, (mousePosition.y - corners[0].y) / screenSpaceRect.height);
        Vector3 worldPosition    = Camera.ViewportToWorldPoint(mapViewportPoint);

        return(use2D ? new Vector3(worldPosition.x, worldPosition.y) : worldPosition);
    }
Ejemplo n.º 2
0
 private void OnDrawGizmosSelected()
 {
     if (gizmos && MapManager.Instance && !Application.isPlaying)
     {
         if (MapManager.Instance.MapMaskRect)
         {
             var rect = ZetanUtility.GetScreenSpaceRect(MapManager.Instance.MapMaskRect);
             Gizmos.DrawCube(MapManager.Instance.MapMaskRect.position, iconSize * rect.width / MapManager.Instance.MapMaskRect.rect.width);
             if (showRange)
             {
                 ZetanUtility.DrawGizmosCircle(MapManager.Instance.MapMaskRect.position, rangeSize * rect.width / MapManager.Instance.MapMaskRect.rect.width,
                                               Vector3.forward, rangeColor);
             }
         }
     }
 }
Ejemplo n.º 3
0
    private void DrawMapIcon(Vector3 worldPosition, MapIcon icon, bool keepOnMap)
    {
        if (!icon || !UI || !UI.gameObject)
        {
            return;
        }
        //把相机视野内的世界坐标归一化为一个裁剪正方体中的坐标,其边长为1,就是说所有视野内的坐标都变成了x、z、y分量都在(0,1)以内的裁剪坐标
        Vector3 viewportPoint = Camera.WorldToViewportPoint(worldPosition);
        //这一步用于修正UI因设备分辨率不一样,在进行缩放后实际Rect信息变了而产生的问题
        Rect screenSpaceRect = ZetanUtility.GetScreenSpaceRect(UI.mapRect);

        //获取四个顶点的位置,顶点序号
        //  1 ┏━┓ 2
        //  0 ┗━┛ 3
        Vector3[] corners = new Vector3[4];
        UI.mapRect.GetWorldCorners(corners);
        //根据归一化的裁剪坐标,转化为相对于地图的坐标
        Vector3 screenPos = new Vector3(viewportPoint.x * screenSpaceRect.width + corners[0].x, viewportPoint.y * screenSpaceRect.height + corners[0].y, 0);
        Vector3 rangePos  = screenPos;

        if (keepOnMap)
        {
            //以遮罩的Rect为范围基准而不是地图的
            screenSpaceRect = ZetanUtility.GetScreenSpaceRect(MapMaskRect);
            float size = (screenSpaceRect.width < screenSpaceRect.height ? screenSpaceRect.width : screenSpaceRect.height) * 0.5f;//地图的一半尺寸
            UI.mapWindowRect.GetWorldCorners(corners);
            if (circle && !isViewingWorldMap)
            {
                //以下不使用UI.mapMaskRect.position,是因为该position值会受轴心(UI.mapMaskRect.pivot)位置的影响而使得最后的结果出现偏移
                Vector3 realCenter     = ZetanUtility.CenterBetween(corners[0], corners[2]);
                Vector3 positionOffset = Vector3.ClampMagnitude(screenPos - realCenter, radius * size);
                screenPos = realCenter + positionOffset;
            }
            else
            {
                float edgeSize = (isViewingWorldMap ? worldEdgeSize : this.edgeSize) * size;
                screenPos.x = Mathf.Clamp(screenPos.x, corners[0].x + edgeSize, corners[2].x - edgeSize);
                screenPos.y = Mathf.Clamp(screenPos.y, corners[0].y + edgeSize, corners[1].y - edgeSize);
            }
        }
        icon.transform.position = screenPos;
        if (icon.iconRange)
        {
            icon.iconRange.transform.position = rangePos;
        }
    }
Ejemplo n.º 4
0
    private void LeftOrRight(Vector2 position)
    {
        Rect  rectAgent = ZetanUtility.GetScreenSpaceRect(itemSlot.GetComponent <RectTransform>());
        float winWidth  = 0;

        for (int i = 0; i < windows.Count; i++)
        {
            var window = windows[i];
            if (window.gameObject.activeSelf)
            {
                Rect rectWin = ZetanUtility.GetScreenSpaceRect(windowPrefab.GetComponent <RectTransform>());
                winWidth += rectWin.width;
            }
        }
#if UNITY_ANDROID
        Rect rectButton = ZetanUtility.GetScreenSpaceRect(buttonArea.GetComponent <RectTransform>());
#elif UNITY_STANDALONE
        Rect rectButton = Rect.zero;
#endif
        if (Screen.width * 0.5f < position.x)//在屏幕右半边
        {
            foreach (var window in windows)
            {
                window.transform.SetAsFirstSibling();
            }
            buttonArea.transform.SetAsLastSibling();
            content.transform.position = new Vector2(position.x - rectAgent.width * 0.5f - (winWidth + rectButton.width) * 0.5f, content.transform.position.y);
        }
        else
        {
            foreach (var window in windows)
            {
                window.transform.SetAsLastSibling();
            }
            buttonArea.transform.SetAsFirstSibling();
            content.transform.position = new Vector2(position.x + rectAgent.width * 0.5f + (winWidth + rectButton.width) * 0.5f, content.transform.position.y);
        }
        if (keepCoroutine != null)
        {
            StopCoroutine(keepCoroutine);
        }
        keepCoroutine = StartCoroutine(KeepInScreen());
    }
Ejemplo n.º 5
0
 private void LeftOrRight(Vector2 position)
 {
     if (Screen.width * 0.5f < position.x)//在屏幕右半边
     {
         UI.subUI.window.transform.SetAsFirstSibling();
         UI.buttonsArea.transform.SetAsLastSibling();
         Rect rectAgent  = ZetanUtility.GetScreenSpaceRect(itemAgent.GetComponent <RectTransform>());
         Rect rectWin    = ZetanUtility.GetScreenSpaceRect(UI.subUI.GetComponent <RectTransform>());
         Rect rectButton = ZetanUtility.GetScreenSpaceRect(UI.buttonsArea.GetComponent <RectTransform>());
         UI.window.transform.position = new Vector2(position.x - rectAgent.width * 0.5f - rectWin.width - rectButton.width, UI.window.transform.position.y);
     }
     else
     {
         UI.subUI.window.transform.SetAsLastSibling();
         UI.buttonsArea.transform.SetAsFirstSibling();
         Rect rectAgent  = ZetanUtility.GetScreenSpaceRect(itemAgent.GetComponent <RectTransform>());
         Rect rectWin    = ZetanUtility.GetScreenSpaceRect(UI.subUI.GetComponent <RectTransform>());
         Rect rectButton = ZetanUtility.GetScreenSpaceRect(UI.buttonsArea.GetComponent <RectTransform>());
         UI.window.transform.position = new Vector2(position.x + rectAgent.width * 0.5f + rectWin.width + rectButton.width, UI.window.transform.position.y);
     }
     ZetanUtility.KeepInsideScreen(UI.window.GetComponent <RectTransform>(), true, true, false, false);
 }
Ejemplo n.º 6
0
    private void OnDrawGizmos()
    {
        if (!UI || !UI.gameObject || !MapMaskRect)
        {
            return;
        }
        Rect screenSpaceRect = ZetanUtility.GetScreenSpaceRect(MapMaskRect);

        Vector3[] corners = new Vector3[4];
        UI.mapMaskRect.GetWorldCorners(corners);
        if (circle && !isViewingWorldMap)
        {
            float radius = (screenSpaceRect.width < screenSpaceRect.height ? screenSpaceRect.width : screenSpaceRect.height) * 0.5f * this.radius;
            //ZetanUtility.DrawGizmosCircle(ZetanUtility.CenterBetween(corners[0], corners[2]), radius, radius / 1000, Color.white, false);
            ZetanUtility.DrawGizmosCircle(ZetanUtility.CenterBetween(corners[0], corners[2]), radius, Vector3.forward);
        }
        else
        {
            float   edgeSize = isViewingWorldMap ? worldEdgeSize : this.edgeSize;
            Vector3 size     = new Vector3(screenSpaceRect.width - edgeSize * (screenSpaceRect.width < screenSpaceRect.height ? screenSpaceRect.width : screenSpaceRect.height),
                                           screenSpaceRect.height - edgeSize * (screenSpaceRect.width < screenSpaceRect.height ? screenSpaceRect.width : screenSpaceRect.height), 0);
            Gizmos.DrawWireCube(ZetanUtility.CenterBetween(corners[0], corners[2]), size);
        }
    }