基于屏幕大小的工具提示,带偏移量



嗨,伙计们,我正在尝试为我制作的游戏中的某些项目制作工具提示窗口。为了上帝的爱,我不知道如何为工具提示设置基于屏幕的偏移量。Atm im 在 2 秒后使用它(代码 1(打开工具提示游戏对象(称为 StatDisplay(,它以 16x9 分辨率工作,但在更改分辨率后,工具提示离主对象太远。我用鼠标位置(代码 2(尝试过,我遇到了同样的偏移问题,它在更高的分辨率下会减少,而在较小的分辨率下偏移量太大。关于如何解决这个问题的任何想法?(我正在考虑制作一个根据屏幕尺寸变化的变量,但找不到方法......

代码 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);
}
}
}
}

类级别变量: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;
}

把它扔到你想要工具提示的对象上,然后你可以进行一些调整。 当您将鼠标悬停在对象上时,这将为您提供一个框,鼠标位置带有文本"工具提示文本"。 您的对象需要碰撞体。

更重要的是您的问题:您正在寻找这一行:

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 是画布设置还是什么(......好吧,无论什么

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

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

最新更新