项目主场景的搭建主要参照了VR游乐场这个项目VR游乐园(Unity2018.1.0)_哔哩哔哩_bilibili
你可以参考这个项目的视频去按部就班的实现你需要的功能。在这里我会把我用到的代码分享给你。
1、这是实现UI绕轴前后转的一段代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using System.IO;
public class GameItemSpawn : MonoBehaviour
{
public static GameItemSpawn Instance;
public Material[] m_GameItemMatArr; // 材质球
public GameObject go_GameItem;
public int Index = 0;
public float m_Angle; // 角度
// 初始化生成GameItem,有多少个取决于有多少个游乐项目,就是看材质球数量
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>().SetVideoName(m_GameItemMatArr[i].name);
go.GetComponentInChildren<GameItem>().Index = i;
}
}
// 向前转
public void RotateForward()
{
Index++;
if (Index >= m_GameItemMatArr.Length)
{
Index = 0;
}
transform.DORotate(new Vector3(0, -Index * m_Angle, 0), 0.3f);
}
// 向后转
public void RotateBack()
{
Index--;
if (Index < 0)
{
Index = m_GameItemMatArr.Length - 1;
}
transform.DORotate(new Vector3(0, -Index * m_Angle, 0), 0.3f);
}
}
2、这是实现显示当前UI内容名称的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameItemSelectPanel : MonoBehaviour
{
private Button btn_Forward;
private Button btn_Back;
private Button btn_Select;
private Text txt_Title;
private string[] m_GameItemNameArr;
private void Awake()
{
Init();
TextAsset textAsset = Resources.Load<TextAsset>("项目名字");
m_GameItemNameArr = textAsset.text.Split('\n');
}
private void Update()
{
txt_Title.text = m_GameItemNameArr[GameItemSpawn.Instance.Index];
}
private void ReadGameItemNameText()
{
TextAsset textAsset = Resources.Load<TextAsset>("项目名字");
m_GameItemNameArr = textAsset.text.Split('\n');
}
private void Init()
{
txt_Title = transform.Find("txt_Title").GetComponent<Text>();
btn_Forward = transform.Find("btn_Forward").GetComponent<Button>();
btn_Forward.onClick.AddListener(() =>
{
GameItemSpawn.Instance.RotateForward();
});
btn_Back = transform.Find("btn_Back").GetComponent<Button>();
btn_Back.onClick.AddListener(() =>
{
GameItemSpawn.Instance.RotateBack();
});
btn_Select = transform.Find("btn_Select").GetComponent<Button>();
}
}
3、这是实现播放当前UI对应视频资源的代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using VRTK;
public class GameItem : MonoBehaviour
{
public int Index;
private VideoPlayer m_VideoPlayer;
private void Awake()
{
m_VideoPlayer = GetComponent<VideoPlayer>();
}
/// <summary>
/// 圆盘按钮抬起
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
//private void GameItem_TouchpadReleased(object sender, ControllerInteractionEventArgs e)
//{
// m_VideoPlayer.Pause();
//}
/// <summary>
/// 设置视频名称
/// </summary>
/// <param name="videoName"></param>
public void SetVideoName(string videoName)
{
m_VideoPlayer.url = GetVideoPath(videoName);
}
/// <summary>
/// 获取视频路径
/// </summary>
/// <param name="videoName"></param>
/// <returns></returns>
private string GetVideoPath(string videoName)
{
return Application.dataPath + "/StreamingAssets/" + videoName + ".mp4";
}
private void OnTriggerEnter(Collider other)
{
//代表文件不存在
//if (File.Exists(m_VideoPlayer.url) == false) return;
m_VideoPlayer.Play();
}
private void OnTriggerExit(Collider other)
{
m_VideoPlayer.Pause();
}
}
4、这是实现为项目增添切换场景也不会被销毁的背景音频的代码
using UnityEngine;
using System.Collections;
public class audio : MonoBehaviour
{
static audio _instance;
// Use this for initialization
void Start()
{
}
public static audio instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType<audio>();
DontDestroyOnLoad(_instance.gameObject);
}
return _instance;
}
}
void Awake()
{
//此脚本永不消毁,并且每次进入初始场景时进行判断,若存在重复的则销毁
if (_instance == null)
{
_instance = this;
DontDestroyOnLoad(this);
}
else if (this != _instance)
{
Destroy(gameObject);
}
}
}