我是靠谱客的博主 酷炫往事,最近开发中收集的这篇文章主要介绍VR游乐园关键代码,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

UI界面控制

1.CountDownCanvas

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CountDownCanvas : MonoBehaviour
{
public static CountDownCanvas Instance;
private Text txt_CountDown;
private int m_Time = 3;
private void Awake()
{
Instance = this;
txt_CountDown = transform.Find("txt_CountDown").GetComponent<Text>();
gameObject.SetActive(false);
}
public void ReduceTime()
{
if (m_Time > 0)
{
gameObject.SetActive(true);
txt_CountDown.text = m_Time.ToString();
m_Time--;
}
}
}

2.GameItemSelectPanel

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()
{
ReadGameItemNameText();
Init();
}
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>();
btn_Select.onClick.AddListener(() =>
{
LoadingPanel.Instance.LoadScene();
});
}
}

3.LoadingPanel

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LoadingPanel : MonoBehaviour
{
public static LoadingPanel Instance;
private Image img_LoadingBar;
private AsyncOperation m_Ao;
private bool m_IsLoad = false;
private void Awake()
{
Instance = this;
transform.localScale = Vector3.zero;
img_LoadingBar = transform.Find("img_LoadingBar").GetComponent<Image>();
}
public void LoadScene()
{
transform.DOScale(Vector3.one, 0.3f).OnComplete(() =>
{
StartCoroutine("Load");
});
}
IEnumerator Load()
{
int displayProgress = -1;
int toProgress = 100;
while (displayProgress < toProgress)
{
displayProgress++;
ShowProgress(displayProgress);
if (m_IsLoad == false)
{
m_Ao = SceneManager.LoadSceneAsync(2 + GameItemSpawn.Instance.Index);
m_Ao.allowSceneActivation = false;
m_IsLoad = true;
}
yield return new WaitForEndOfFrame();
}
if (displayProgress == 100)
{
m_Ao.allowSceneActivation = true;
StopCoroutine("Load");
}
}
private void ShowProgress(int progress)
{
img_LoadingBar.fillAmount = progress * 0.01f;
}
}

4.StartScene

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class StartScene : MonoBehaviour
{
private Button btn_Start;
private Button btn_Exit;
private void Awake()
{
btn_Start = transform.Find("btn_Start").GetComponent<Button>();
btn_Start.onClick.AddListener(() =>
{
SceneManager.LoadScene(1);
});
btn_Exit = transform.Find("btn_Exit").GetComponent<Button>();
btn_Exit.onClick.AddListener(OnExitButtonClick);
}
private void OnExitButtonClick()
{
Application.Quit();
}
}

5.AnimController

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class AnimController : MonoBehaviour
{
public AudioSource m_CheersAudio;
private Animation m_Anim;
/// <summary>
/// 动画片段的时间
/// </summary>
private float m_ClipTime;
/// <summary>
/// 是否播放欢呼音效
/// </summary>
private bool m_IsPlayCheersAudio = false;
/// <summary>
/// 动画是否播放完
/// </summary>
private bool m_IsEnd = false;
/// <summary>
/// 计时器
/// </summary>
private float m_Timer = 0f;
private void Awake()
{
m_Anim = GetComponent<Animation>();
m_ClipTime = m_Anim.clip.length;
}
private void Update()
{
m_ClipTime -= Time.deltaTime;
if (m_ClipTime <= 10 && m_IsPlayCheersAudio == false)
{
m_CheersAudio.Play();
m_IsPlayCheersAudio = true;
}
if (m_ClipTime <= 0 && m_IsEnd == false)
{
UnityEngine.XR.InputTracking.disablePositionalTracking = false;
SceneManager.LoadScene("MainScene");
m_IsEnd = true;
}
if (m_ClipTime <= 4)
{
m_Timer += Time.deltaTime;
if (m_Timer >= 1)
{
m_Timer = 0;
CountDownCanvas.Instance.ReduceTime();
}
}
}
}

6.DisablePositionTracking

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DisablePositionTracking : MonoBehaviour
{
public Vector3 pos;
private void Awake()
{
UnityEngine.XR.InputTracking.disablePositionalTracking = true;
transform.localPosition = pos;
}
}

7.GameItem

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using VRTK;
using System.IO;
public class GameItem : MonoBehaviour
{
public int Index;
private VideoPlayer m_VideoPlayer;
private void Awake()
{
m_VideoPlayer = GetComponent<VideoPlayer>();
GameObject.Find("ControllerRight").GetComponent<VRTK_ControllerEvents>().TouchpadReleased += GameItem_TouchpadReleased;
GameObject.Find("ControllerLeft").GetComponent<VRTK_ControllerEvents>().TouchpadReleased += GameItem_TouchpadReleased;
}
private 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;
}
}
/// <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();
}
}

8.GameItemSpawn

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class GameItemSpawn : MonoBehaviour
{
public static GameItemSpawn Instance;
public Material[] m_GameItemMatArr;
public GameObject go_GameItem;
public int Index = 0;
private 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>().SetVideoName(m_GameItemMatArr[i].name);
go.GetComponentInChildren<GameItem>().Index = i;
}
}
/// <summary>
/// 向前转
/// </summary>
public void RotateForward()
{
Index++;
if (Index >= m_GameItemMatArr.Length)
{
Index = 0;
}
transform.DORotate(new Vector3(0, -Index * m_Angle, 0), 0.3f);
}
/// <summary>
/// 向后转
/// </summary>
public void RotateBack()
{
Index--;
if (Index < 0)
{
Index = m_GameItemMatArr.Length - 1;
}
transform.DORotate(new Vector3(0, -Index * m_Angle, 0), 0.3f);
}
}

 

转载于:https://www.cnblogs.com/krystalstar/p/10823810.html

最后

以上就是酷炫往事为你收集整理的VR游乐园关键代码的全部内容,希望文章能够帮你解决VR游乐园关键代码所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部