带偏移的基于屏幕尺寸的工具提示

Screen size based tooltip with offset

大家好,我正在尝试为我正在制作的游戏中的某些物品制作工具提示 window。看在上帝的份上,我不知道如何为工具提示设置基于屏幕的偏移量。 Atm im 在 2 秒后使用此(代码 1)打开工具提示游戏对象(称为 StatDisplay)并且它在 16x9 分辨率下工作,但在更改分辨率后工具提示离主要对象太远。我用鼠标位置(代码 2)和 im 运行 尝试了它与偏移量相同的问题,它在更高分辨率和更小的分辨率下减小,偏移量太大。有想法该怎么解决这个吗 ? (我正在考虑制作一个根据屏幕尺寸变化的变量,但想不出办法...)

代码 1:

    bool hovering = false;
    public GameObject StatDisplay;



    IEnumerator StartCountdown()
    {
        RectTransform rc = StatDisplay.GetComponent<RectTransform>();
        RectTransform rc2 = this.GetComponent<RectTransform>();

        int time = 1;
        while ((time > 0) && (hovering == true))
        {
            yield return new WaitForSeconds(1.0f);
            time--;
        }
        if ((time == 0) && (hovering == true))
        {

            if (Input.mousePosition.x > (Screen.width / 2))
            {
                if (Input.mousePosition.y > (Screen.height / 2))
                {
                    //Debug.Log("Top right");

                    StatDisplay.transform.position = new Vector3(-rc.rect.width / 2 + this.transform.position.x + rc2.rect.width / 2, -rc.rect.height / 2 + this.transform.position.y - rc2.rect.height / 2, 0);
                }
                else
                {
                    //Debug.Log("Bottom right");
                    StatDisplay.transform.position = new Vector3(- rc.rect.width / 2 + this.transform.position.x + rc2.rect.width / 2, rc.rect.height / 2 + this.transform.position.y + rc2.rect.height / 2, 0);

                }
            }
            else
            {
                if (Input.mousePosition.y > (Screen.height / 2))
                {
                    //Debug.Log("Top Left");

                    StatDisplay.transform.position = new Vector3(rc.rect.width / 2 + this.transform.position.x - rc2.rect.width / 2, - rc.rect.height / 2 + this.transform.position.y - rc2.rect.height / 2, 0);
                }
                else
                {
                    //Debug.Log("Bottom Left");

                    StatDisplay.transform.position = new Vector3(rc.rect.width / 2 + this.transform.position.x - rc2.rect.width / 2, rc.rect.height / 2 + this.transform.position.y + rc2.rect.height / 2, 0);
                }
            }
            StatDisplay.SetActive(true);
        }
    }

    public void Enter()
    {
        hovering = true;
        StartCoroutine(StartCountdown());
    }

    public void Exit()
    {
        hovering = false;
        StatDisplay.SetActive(false);

    }

代码 2:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HoverAtCursorPosition : MonoBehaviour
{
    RectTransform rt;
    CanvasGroup cg;

    float Obwidth;
    float Obheight;

    Vector3 MousePoz = new Vector3();

    private void Start()
    {
        rt = GetComponent<RectTransform>();
        cg = GetComponent<CanvasGroup>();
    }


    private void OnEnable()
    {
        PositionFunction();
    }

    private void OnDisable()
    {
        cg.alpha = 0;
    }


    void Update()
    {

        Obwidth = rt.rect.width + 20f;
        Obheight = rt.rect.height + 20f;

        //Debug.Log("X: " + Screen.width / rt.rect.width);
        //Debug.Log("Y: " + Screen.height / rt.rect.height);

        MousePoz = Camera.main.ScreenToViewportPoint(Input.mousePosition);

        PositionFunction();

        if (cg.alpha != 1)
        {
            if (gameObject.activeSelf == true)
            {
                cg.alpha += 0.1f;
            }
        }
    }


    void PositionFunction()
    {
        if (Input.mousePosition.x > (Screen.width / 2))
        {
            if (Input.mousePosition.y > (Screen.height / 2))
            {
                //Debug.Log("Top right");

                transform.position = new Vector3(MousePoz.x * Screen.width - Obwidth / 2, MousePoz.y * Screen.height - Obheight / 2);
            }
            else
            {
                //Debug.Log("Bottom right");
                transform.position = new Vector3(MousePoz.x * Screen.width - Obwidth / 2, MousePoz.y * Screen.height + Obheight / 2);
            }
        }
        else
        {
            if (Input.mousePosition.y > (Screen.height / 2))
            {
                //Debug.Log("Top Left");

                transform.position = new Vector3(MousePoz.x * Screen.width + Obwidth / 2, MousePoz.y * Screen.height - Obheight / 2);
            }
            else
            {
                //Debug.Log("Bottom Left");

                transform.position = new Vector3(MousePoz.x * Screen.width + Obwidth / 2, MousePoz.y * Screen.height + Obheight / 2);
            }
        }
    }
}

class 级别变量: bool isVis=false;

void OnGUI(){
 if (isVis)
        {
    GUI.Box(new Rect(Input.mousePosition.x + 2, Screen.height - Input.mousePosition.y + 2, 128, 72), "Tool Tip Text");
               } 
    }



private void OnMouseEnter()
    {
        isVis = true;

    }
    private void OnMouseExit()
    {
        isVis = false;

    }

将此添加到您想要工具提示的 object 上,然后您可以进行一些调整。当您将鼠标悬停在 object 上时,这将在鼠标位置为您提供一个带有文本 "Tool Tip Text" 的框。你的 object 需要一个对撞机。

更切合您的问题:您正在寻找这一行:

new Rect(Input.mousePosition.x + 2, Screen.height - Input.mousePosition.y + 2, 128, 72)

为了在您的努力中获得正确的 y 位置,您需要从 Screen.Height 中减去 mousePosition.y 同样重要的是要注意 +2,就好像你在鼠标上画画一样,它会在工具提示和光标之间断断续续

正如我在下面所说,如果您将工具提示矩形存储在一个变量中,那就是在 Update() 中设置的

在您的 OnGUI 中,您可以这样做:

GUI.Box(myRect, "Tool Tip Text");
GUI.DrawTexture(new Rect(myRect.x,myRect.y+20,64,64),Resources.Load("Sprites/sample.png"))  ;   

(加载方法将从您的 Resources/Sprites 文件夹中获取图像 sample.png,如果您没有该文件夹,您可以创建它,或者更改路径,但路径必须是在名为 Resources

的文件夹中

你也可以

GUI.Label(new Rect(myRect.x,myRect.y+84,myRect.width,24),"Item Description")) ;    

最好的部分是,您可以加载自定义 GUI 皮肤,设置自己的样式,并动态更改每个 GUI 元素。这意味着如果你想要一个白色的标题、金色的描述和图像周围的边框,这是一件简单的事情。如果您想要一些示例样式,请告诉我!

好的,在玩的过程中我找到了修复代码的方法

rt = this.GetComponent<RectTransform>();
        float RealWidth = rt.rect.width / Screen.width;
        float RealHeight = rt.rect.height / Screen.height;

rt 在其锚点设置为在 x 和 y 上拉伸的组件上(矩形变换 > 左侧图标 > alt + 右下角图标)

看起来实际屏幕宽度和对象矩形的宽度不一样,即使它被拉伸以适合整个屏幕。它总是关闭 +-10-20%(不确定它的 bc of canvas 设置或什么)......好吧无论如何

new Vector3(Input.mousePosition.x * RealWidth, Input.mousePosition.y * RealHeight, 0)

此向量将为您提供所有分辨率的正确位置。很好,它对我有用...它可能会帮助遇到类似问题的人