返回介绍

GUILayer.HitTest 位置检测

发布于 2019-12-18 15:37:46 字数 2666 浏览 1336 评论 0 收藏 0

JavaScript => public function HitTest(screenPosition: Vector3): GUIElement;
C# => public GUIElement HitTest(Vector3 screenPosition);

Parameters 参数

Description 描述

Get the GUI element at a specific screen position.

在特定的屏幕位置,获取GUI元素。

Returns the GUIElement at a specific point on screen. If screenPosition is inside some GUIElement, that element is returned. Returns null if the position is not inside any GUI element. GUI elements that belong to Ignore Raycast layer will be ignored, as if they would not exist.

返回屏幕上在特定点的GUIElement。如果screenPosition在GUIElement里面,则该元素被返回。如果screenPosition不在GUI元素里面,返回null。GUI元素属于忽略光线投射层将被忽略,就像他们不存在。

screenPosition is measured in screen coordinates, like the values returned by Input.mousePosition property.

screenPosition是基于屏幕坐标,例如Input.mousePosition属性返回的值。

Note: GUILayer.HitTest only finds old-school GUI components (made up of the classes GUIElement, GUITexture, GUIText, GUILayer), and will not work with the “new” one (referred to as “UnityGUI” and made up of all the other GUIAnything classes, and the OnGUI() call). So if you're using UnityGUI, HitTest won't find anything.

注意:GUILayer.HitTest只查找老式GUI组件(由类GUIElement, GUITexture, GUIText, GUILayer组成),并不会工作在新的组件上(被称为UnityGUI和由所有其他GUI什么的类组成,和OnGUI()调用)。因此如果你使用UnityGUI,HitTest将不会找到任何东西。

JavaScript:

// Tests if the mouse is touching a GUIElement.
	// Add a GUITexture and put the mouse over it and
	// it will print the GUITexture name.
	private var test : GUILayer;
	test = Camera.main.GetComponent.<GUILayer>();
	function Update() {
		if(test.HitTest(Input.mousePosition) != null) {
			Debug.Log(test.HitTest(Input.mousePosition).name);
		}
	}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    private GUILayer test;
    void Update() {
        if (test.HitTest(Input.mousePosition) != null)
            Debug.Log(test.HitTest(Input.mousePosition).name);
 
    }
    void Example() {
        test = Camera.main.GetComponent<GUILayer>();
    }
}

guilayer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文