Skip to content

Instantly share code, notes, and snippets.

@Y4er
Created February 28, 2019 06:51
Show Gist options
  • Save Y4er/769a7a518ce3d440bae25f8f63bd1704 to your computer and use it in GitHub Desktop.
Save Y4er/769a7a518ce3d440bae25f8f63bd1704 to your computer and use it in GitHub Desktop.
VR开发的Point控制脚本和一个样例脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRStandardAssets.Utils;
public class Example : MonoBehaviour {
VRInteractiveItem vritem;
//动态添加vritem
void Awake(){
if (!gameObject.GetComponent <VRInteractiveItem> ()) {
gameObject.AddComponent <VRInteractiveItem> ();
}
vritem = gameObject.GetComponent <VRInteractiveItem> ();
}
//两个生命周期函数注册Handle事件
void OnEnable(){
vritem.OnOver += HandleOver;
vritem.OnOut += HandleOut;
vritem.OnClick += HandleClick;
}
void OnDisable(){
vritem.OnOver -= HandleOver;
vritem.OnOut -= HandleOut;
vritem.OnClick -= HandleClick;
}
//判断触发条件 如果视线在当前物体上并且点击事件触发
void Update(){
if(vritem.IsOver&&PointCtl.point.isclick){
HandleClick ();
}
}
//Handle事件处理
//视线在当前物体上时开启计时
void HandleOver(){
print ("[进入]"+name);
PointCtl.point.isover = true;
}
//视线在当前物体上时开启计时
void HandleOut(){
print ("[退出]"+name);
PointCtl.point.isover = false;
}
//倒计时结束后的点击事件
void HandleClick(){
print ("[单击]"+name);
PointCtl.point.Init ();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using VRStandardAssets.Utils;
public class PointCtl : MonoBehaviour {
[SerializeField]Image ring; //环
[SerializeField]VRInteractiveItem vritem; //vritem
public static PointCtl point; //单例
public float timer=3.0f; //计时
public bool isover=false; //isover标志位来开启计时
public bool isclick= false; //计时完毕之后触发HandleClick事件
void Awake(){
point = this;
if (!gameObject.GetComponent <VRInteractiveItem> ()) {
gameObject.AddComponent <VRInteractiveItem> ();
}
vritem = GetComponent <VRInteractiveItem> ();
}
void Update(){
//视线进入物体
if(isover){
ring.fillAmount += Time.deltaTime / timer;
if(ring.fillAmount>=1){
isclick = true;
}
}
//视线从物体上移开
else{
Init ();
}
}
public void Init(){
ring.fillAmount = 0;
isover = false;
isclick = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment