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

时间:2018-10-24 17:38:17

标签: c# unity3d tooltip

大家好,我正在尝试为游戏中的某些物品制作工具提示窗口。对于上帝的爱,我不知道如何为工具提示设置基于屏幕的偏移量。 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);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

类级var: 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;

    }

将此内容放置在要为其提供工具提示的对象上,然后可以进行一些调整。当您将鼠标悬停在对象上时,这将为您提供一个框,其中带有文本“ Tool Tip Text”。您的物体将需要对撞机。

更多问题,您正在寻找这条线:

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

要获得正确的y位置,您需要从mousePosition.y中减去Screen.Height 注意+2也是很重要的,就像您在鼠标上画图一样,它会在工具提示和光标之间停顿

就像我在下面说的,如果您将工具提示rect存储在变量中,那就是在Update()中设置

在您的OnGUI中,您可以执行以下操作:

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

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

的文件夹

您也可以

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

最好的部分是,您可以加载自定义GUI外观,设置自己的样式,并动态更改每个GUI元素。这意味着如果您想要白色标题,金色说明以及图像周围的边框,这很简单。如果您需要一些示例样式,请告诉我!

答案 1 :(得分:0)

好玩的时候我发现了一种修复代码的方法

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

rt在其锚点设置为在x和y上拉伸的组件上(Rect transform>图标在左侧> alt +图标在右下方)

似乎实际的屏幕宽度和对象的rect的宽度并不相同,即使它被拉伸以适合整个屏幕。它总是偏离+ -10-20%(不确定其画布设置的公元前还是什么)...好吧

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

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