在 Gaze Unity 上显示对象
Showing Object on Gaze Unity
当我注视一个物体时,我有这个点击信息。物体在注视时出现,但在移开视线时消失。我希望对象在观看后显示 10 秒。我走的路对吗?
float timeLeft = 10.0f;
timeLeft -= Time.deltaTime;
if (Hit == InfoCube)
{
GameObject.Find("GUIv2").transform.localScale = new Vector3(0.02f, 0.2f, 0.8f) // Shows object
}
else if (timeLeft < 0)
{
GameObject.Find("GUIv2").transform.localScale = new Vector3(0, 0, 0);
GameObject.Find("GUIv2").SetActive(false);
}
这段代码放在哪里?我假设它在 Update() 方法中。
无论如何我都可以看到代码存在一些问题。以下是我的建议:
- 缓存 "GUIv2" 对象,这样您就不必每帧 "find" 它。
- 您不需要更改 localScale 值。只需使用 SetActive。
- 不要每次都初始化
timeLeft
变量。它永远不会达到 0。
下面是一些示例代码,应该可以实现您正在寻找的内容:
float timeLeft = 10f;
bool isGazed;
GameObject GUIv2;
void Start()
{
GUIv2 = GameObject.Find("GUIv2");
}
void Update()
{
if (Hit == InfoCube)
{
if (!isGazed)
{
// first frame where gaze was detected.
isGazed = true;
GUIv2.SetActive(true);
}
// Gaze on object.
return;
}
if (Hit != InfoCube && isGazed)
{
// first frame where gaze was lost.
isGazed = false;
timeLeft = 10f;
return;
}
timeLeft -= Time.deltaTime;
if (timeLeft < 0)
{
GUIv2.SetActive(false);
}
}
当我注视一个物体时,我有这个点击信息。物体在注视时出现,但在移开视线时消失。我希望对象在观看后显示 10 秒。我走的路对吗?
float timeLeft = 10.0f;
timeLeft -= Time.deltaTime;
if (Hit == InfoCube)
{
GameObject.Find("GUIv2").transform.localScale = new Vector3(0.02f, 0.2f, 0.8f) // Shows object
}
else if (timeLeft < 0)
{
GameObject.Find("GUIv2").transform.localScale = new Vector3(0, 0, 0);
GameObject.Find("GUIv2").SetActive(false);
}
这段代码放在哪里?我假设它在 Update() 方法中。
无论如何我都可以看到代码存在一些问题。以下是我的建议:
- 缓存 "GUIv2" 对象,这样您就不必每帧 "find" 它。
- 您不需要更改 localScale 值。只需使用 SetActive。
- 不要每次都初始化
timeLeft
变量。它永远不会达到 0。
下面是一些示例代码,应该可以实现您正在寻找的内容:
float timeLeft = 10f;
bool isGazed;
GameObject GUIv2;
void Start()
{
GUIv2 = GameObject.Find("GUIv2");
}
void Update()
{
if (Hit == InfoCube)
{
if (!isGazed)
{
// first frame where gaze was detected.
isGazed = true;
GUIv2.SetActive(true);
}
// Gaze on object.
return;
}
if (Hit != InfoCube && isGazed)
{
// first frame where gaze was lost.
isGazed = false;
timeLeft = 10f;
return;
}
timeLeft -= Time.deltaTime;
if (timeLeft < 0)
{
GUIv2.SetActive(false);
}
}