using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Valve.VR.InteractionSystem;
///<summary>
///
///</summary>
public class HeadGaze : MonoBehaviour
{
public Transform cursor;//初始外圈
public Transform headForRaycast;//射出射线的相机
public Image progressImage;//确认外圈
// 射线发送的位移,本示例中设置y轴向上一段距离,以符合”眼部“位置
public Vector3 rayPositionOffset;
public LayerMask layerMask;
// public float rayLength;
private GameObject currentInteractable;
private GameObject lastInteractable;
public float stareTimer;
// 激活点击时限
public float activateTime = 1;
// 标识是否启用凝视交互
public bool isEnabled;
private void Awake()
{
if (cursor != null)
{
// 隐藏光标
progressImage.gameObject.SetActive(false);
}
// 依情况设置,本示例中默认开启交互
isEnabled = true;
}
void Update()
{
// 必须要有发送射线的位置指定
if (headForRaycast == null)
// Debug.Log("222");
return;
if (progressImage != null)
{
progressImage.fillAmount = 0;
}
if (isEnabled)
EyeRaycast();
}
private void EyeRaycast()
{
Vector3 adjustedPosition = headForRaycast.position + (headForRaycast.right * rayPositionOffset.x) +
(headForRaycast.up * rayPositionOffset.y) +
(headForRaycast.forward * rayPositionOffset.z);
Ray ray = new Ray(headForRaycast.position, headForRaycast.forward);
RaycastHit raycastHit;
if (Physics.Raycast(ray, out raycastHit))
{
if (cursor != null)
{
cursor.gameObject.SetActive(true);
}
//从射线返回的信息获取button组件,获取不到说明没聚焦到Button
Button aButton = raycastHit.transform.GetComponent<Button>();
if (aButton == null)
{
DeactivateLastInteractable();
currentInteractable = null;
return;
}
currentInteractable = aButton.gameObject;
//如果当前对象不是上一个对象,把上一个赋值为这个
if (currentInteractable != lastInteractable)
{
DeactivateLastInteractable();
// 上一个交互对象为当前的交互对象
lastInteractable = currentInteractable;
}
if (currentInteractable && currentInteractable != lastInteractable)
{
InputModule.instance.HoverBegin(currentInteractable);
}
else if (currentInteractable == lastInteractable)
{
// 如果当前交互对象与上一个交互对象是同一个,此时认为是停留在当前交互对象上
// 凝视时间累加每一帧增加的时间
// Debug.Log("开始逐渐凝视");
progressImage.gameObject.SetActive(true);
if (Time.timeScale == 0)
{
stareTimer += 0.02f;
}
else
{
stareTimer += Time.deltaTime;
}
if (progressImage != null)
{
// progressImage实现逐渐填充的效果
progressImage.fillAmount = (stareTimer / activateTime);
}
// 凝视时间累加到限定的时间以上
if (stareTimer > activateTime)
{
// InputModule通知Button,此时为点击事件
InputModule.instance.Submit(currentInteractable);
SoundManager.instance.PlayAudio("Click-1");
// 重置凝视时间
stareTimer = 0;
// 取消选择UI
DeactivateLastInteractable();
// 凝视交互不可用
isEnabled = false;
// 1秒钟后开启凝视交互,防止出现多次点击
Invoke("ReEnable", 1f);
}
}
// 如果当前交互对象不是上一个交互对象,取消凝视交互
}
else// 射线没有碰撞任何交互对象
{
DeactivateLastInteractable();
currentInteractable = null;
}
}
/// <summary>
/// 全部初始化
/// </summary>
private void DeactivateLastInteractable()
{
// 凝视时间归零
stareTimer = 0;
// 进度图像归零
if (progressImage != null)
{
progressImage.fillAmount = 0;
}
// 上一个交互对象为空,则返回
if (lastInteractable == null)
{
return;
}
// InputModule通知上一个交互对象此时指针(射线)移出
InputModule.instance.HoverEnd(lastInteractable);
// 上一个交互对象为空
lastInteractable = null;
//隐藏用于标识视线的“光标”
if (cursor != null)
{
progressImage.gameObject.SetActive(false);
}
}
private void ReEnable()
{
isEnabled = true;
}
}