最近做一个简单VR项目,只需要用Cardboard,交互用利用中心圆点,下面是用EventSystem做的触发的机制
想法是,创建一个委托事件,中心圆点进入交互区域时,改变委托事件的委托方法,当圆圈转满时,执行委托
首先导入谷歌的SDK,本人之前下载的SDK都有或多或少的问题,头疼好长时间,后来找到一个没有问题的(暂时没有问题,后续就不保证了)有需要的可以下载
点击打开链接 密码:bo43
第一步在场景中拖入三个预制体GvrEditorEmulator、GvrControllerMain、GvrEventSystem
GvrEditorEmulator 可在电脑上模拟VR模式,按下Ctrl键同时移动鼠标移动视角
GvrControllerMain 它允许从Unity Inspector访问调整控制器的模型深度和旋转
GvrEventSystem 相当于Unity的EventSystem
简单的环境已经搭配完成。
接着创建一个空物体,将主相机拖入,设置Transform参数为0,然后将GvrReticlePointer预制体拖入,作为主相机的子物体

再次运行项目会发现屏幕中间多了一个点,
然后在场景中创建一个Cube,并挂载EventTrigger组件,添加PointerEnter和PointerExet事件,在主相机上添加一个PhysicsRaycaster组件,现在运行项目当圆点对准Cbue的时候,圆点会变成圆圈,如图:


然后在主相机下创建一个3D UI界面创建两个Image,一个图片为圆,一个图片为点,将图片的RaycastTarget选项设为False,
GvrReticlePointer的MeshRenderer组件设为False
如图

接下来是脚本,
圆圈触发事件,挂在Player上,创建一个委托事件,当圆圈进入Cube时,执行委托事件
public class GazeObject : MonoBehaviour { public Image Ima_C; private bool isEnter; private float time; private float waitTime; public GameObject Cube; public delegate void onDown(); public static onDown OnDown; // Use this for initialization void Start () { time = 0; waitTime = 3; } // Update is called once per frame void Update () { if (isEnter) { time += Time.deltaTime; Ima_C.fillAmount = (time / waitTime); if (time>waitTime) { OnDown(); time = 0; isEnter = false; Ima_C.fillAmount = 0; } } } public void PointEnter() { isEnter = true; } public void PointExit() { isEnter = false; Ima_C.fillAmount = 0; time = 0; Cube.GetComponent<MeshRenderer>().material.color = Color.red; } }
创建一个抽象类和一个抽象方法
public abstract class EventEnter : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public abstract void OnwaitDown();
}
写一个改变Cube颜色的类,继承上边的抽象类,在抽象方法中,写一个改变颜色的方法
public class ChangeColor : EventEnter {
public GameObject Cube;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public override void OnwaitDown()
{
Cube.GetComponent<MeshRenderer>().material.color = Color.blue;
}
}
脚本基本完成,在Cube的EventTrigger面板上添加事件

事件基本完成
前期虽然比较麻烦,但后期操作就比较简单,只需要在再有交互的地方编写脚本,继承EventEnter抽象类就行
写的不太好,希望大家能看懂