效果就是这样的
using UnityEngine;
using DG.Tweening;
public class GameItemSpawn : MonoBehaviour
{
public static GameItemSpawn Instance;
public int Index = 0;
public Material[] m_GameItemMatArr;
public GameObject go_GameItem;
public float m_Angle;
private void Awake()
{
Instance = this;
m_Angle = 360.0f / m_GameItemMatArr.Length;
for (int i = 0; i < m_GameItemMatArr.Length; i++)
{
GameObject go = Instantiate(go_GameItem, transform);
go.transform.localEulerAngles = new Vector3(0, i * m_Angle, 0);
go.GetComponentInChildren<MeshRenderer>().material = m_GameItemMatArr[i];
go.GetComponentInChildren<GameItem>().SetPath(m_GameItemMatArr[i].name);
go.GetComponentInChildren<GameItem>().index=i;
}
}
public void RetateForward()
{
Index+=1;
if (Index >= m_GameItemMatArr.Length)
{
Index = 0;
}
transform.DORotate(new Vector3(0, -Index * m_Angle, 0), 0.3f);
}
public void RetateBack()
{
Index-=1;
if (Index < 0)
{
Index = m_GameItemMatArr.Length - 1;
}
transform.DORotate(new Vector3(0, -Index * m_Angle, 0), 0.3f);
}
}
GameItem 有 Unity VRTK的按钮监听方法 不过要先添加 VRTK_ControllerEvents脚本
绑定在VRTK_Manager脚本上
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using VRTK;
public class GameItem : MonoBehaviour
{
public string m_VideoName;
public string GetVideoPath;
public VideoPlayer m_videoPlayer;
public int index = 0;
void Awake()
{
m_videoPlayer = GetComponent<VideoPlayer>();
GameObject.Find("VRControllerRight").GetComponent<VRTK_ControllerEvents>().TouchpadReleased += GameItem_Tpuch;
GameObject.Find("VRControllerLeft").GetComponent<VRTK_ControllerEvents>().TouchpadReleased += GameItem_Tpuch;
}
void Update()
{
if (index == GameItemSpawn.Instance.Index)
{
GetComponent<MeshCollider>().enabled = true;
GetComponent<MeshRenderer>().material.color=Color.white;
}
else
{
GetComponent<MeshCollider>().enabled = false;
GetComponent<MeshRenderer>().material.color=Color.gray;
}
}
//圆盘按钮抬起
private void GameItem_Tpuch(object sender, ControllerInteractionEventArgs e)
{
m_videoPlayer.Pause();
}
public void SetPath(string videoNam)
{
m_videoPlayer.url = Application.streamingAssetsPath + "/" + videoNam + ".mp4";
}
private void OnTriggerEnter(Collider other)
{
if(System.IO.File.Exists(m_videoPlayer.url)==false) return;
m_videoPlayer.Play();
}
private void OnTriggerExit(Collider other)
{
m_videoPlayer.Pause();
}
}
按钮脚本就很简单了
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameSelectPanel : MonoBehaviour
{
private Button btn_Forward;
private Button btn_Break;
private Button btn_Start;
private string[] m_GameItemName;
private Text txt_Title;
private void ReadGameItemText()
{
TextAsset textAsset = Resources.Load<TextAsset>("游乐项目名字");
m_GameItemName = textAsset.text.Split('\n');
}
private void Awake()
{
ReadGameItemText();
btn_Forward = transform.Find("Next").GetComponent<Button>();
btn_Forward.onClick.AddListener(() =>
{
Debug.Log("btn_Forward");
GameItemSpawn.Instance.RetateForward();
});
btn_Break = transform.Find("Last").GetComponent<Button>();
btn_Break.onClick.AddListener(() =>
{
Debug.Log("btn_Break");
GameItemSpawn.Instance.RetateBack();
});
btn_Start = transform.Find("Btn_Start").GetComponent<Button>();
btn_Start.onClick.AddListener(Setlec);
txt_Title = transform.Find("Title").GetComponent<Text>();
}
void Update()
{
txt_Title.text = m_GameItemName[GameItemSpawn.Instance.Index];
}
private void Setlec()
{
LodingPanel.Intsance.LoadScene();
}
}
隐藏手柄和不让改变位置信息的代码是这样的
if (SceneManager.GetActiveScene().name == "MainScene")
{
UnityEngine.XR.InputTracking.disablePositionalTracking = false;
}
else
{
UnityEngine.XR.InputTracking.disablePositionalTracking = true;
}