School StartsFirstProject~UnityVR(HTCVive设备开发)(二)

简介: School StartsFirstProject~UnityVR(HTCVive设备开发)

ItemManager、EventManager 脚本


实现的功能:创建动态图片、左右点击按钮相关代码

ItemManager

usingUnityEngine;
usingDG.Tweening;
publicclassItemManager : MonoBehaviour {
//单例publicstaticItemManagerinstance;
//获取图片材质球publicMaterial[] materials;
//图片预制体publicGameObjectprefabe;
//旋转的角度privatefloatangle;
//旋转方向publicintrisitionDirection;
privatevoidAwake () {
//单例instance=this;
//旋转的方向risitionDirection=0;
//计算图片与图片的角度angle=360f/materials.Length;
for (inti=0; i<materials.Length; i++)
    {
//创建图片预制体GameObjectitemObj=Instantiate(prefabe, transform);
//设置角度itemObj.transform.localEulerAngles=newVector3(0, i*angle, 0);
//设置其图片itemObj.GetComponentInChildren<MeshRenderer>().material=materials[i];
//播放该图的AudioitemObj.GetComponentInChildren<AudioManager>().SetVideoName(materials[i].name);
//判断射线只能触发当前的页面itemObj.GetComponentInChildren<AudioManager>().Index=i;
        }
    }
//右旋转publicvoidRightRisition()
  {
risitionDirection++;
if (risitionDirection>=materials.Length)
    {
risitionDirection=0;
        }
transform.DORotate(newVector3(0, -risitionDirection*angle, 0), 0.3f);
    }
//左旋转publicvoidLeftRisition()
    {
risitionDirection--;
if (risitionDirection<0)
        {
risitionDirection=materials.Length;
        }
transform.DORotate(newVector3(0, -risitionDirection*angle, 0), 0.3f);
    }
}

EventManager

usingUnityEngine;
usingUnityEngine.UI;
publicclassEventManager : MonoBehaviour {
//场景名字privatestring[] sceneNameSText;
//名字文本publicTextsceneNameStext;
privatevoidStart()
  {
//右按钮transform.Find("Right").GetComponent<Button>().onClick.AddListener(() =>    {
ItemManager.instance.RightRisition();
    });
//左按钮transform.Find("Left").GetComponent<Button>().onClick.AddListener(() =>    {
ItemManager.instance.LeftRisition();
    });
//中间事件transform.Find("Center").GetComponent<Button>().onClick.AddListener(() =>    {
Loading.Instance.LoadScene();
    });
//获取Resources文件夹里面的Text文本ReadSceneName();
  }
privatevoidUpdate()
  {
//同步文本sceneNameStext.text=sceneNameSText[ItemManager.instance.risitionDirection];
  }
/// <summary>/// 获取resourcesText/// </summary>privatevoidReadSceneName()
  {
TextAssettextAsset=Resources.Load<TextAsset>("unityVR");
sceneNameSText=textAsset.text.Split('\n');
  }
}

效果如下:

image.png

AudioManager脚本


实现的功能:实现射线碰撞到UI触发相关视频

usingUnityEngine;
usingUnityEngine.Video;
usingVRTK;
usingSystem.IO;
publicclassAudioManager : MonoBehaviour {
//获取VideoPlayer组件privateVideoPlayervideo;
publicintIndex;
privatevoidAwake()
  {
//获取VideoPlayer组件video=GetComponent<VideoPlayer>();
//获取VRTK_ControllerEventsGameObject.Find("Right").GetComponent<VRTK_ControllerEvents>();
  }
privatevoidUpdate()
  {
//判断射线只能触发当前的页面if (Index==ItemManager.instance.risitionDirection)
    {
GetComponent<MeshCollider>().enabled=true;
GetComponent<MeshRenderer>().material.color=Color.white;
    }
else    {
GetComponent<MeshCollider>().enabled=false;
GetComponent<MeshRenderer>().material.color=Color.gray;
    }
  }
/// <summary>/// 初始化视屏名字/// </summary>publicvoidSetVideoName(stringvideoName)
  {
video.url=GetVideoPath(videoName);
  }
/// <summary>/// 获取视频路径、播放路径/// </summary>privatestringGetVideoPath(stringvideoName)
  {
returnApplication.dataPath+"/StreamingAssets/"+videoName+".mp4";
  }
//满足触发条件执行privatevoidOnTriggerEnter(Colliderother)
  {
//代表文件不存在 if (File.Exists(video.url) ==false) return;
video.Play();
  }
privatevoidOnTriggerExit(Colliderother)
  {
video.Pause();
  }
}

效果如下:

image.png

Loading 脚本


实现的功能:实现加载相关逻辑

usingSystem.Collections;
usingUnityEngine;
usingDG.Tweening;
usingUnityEngine.UI;
usingUnityEngine.SceneManagement;
publicclassLoading : MonoBehaviour{
//单例publicstaticLoadingInstance;
//加载页UIprivateImageloading;
//异步加载场景AsyncOperationprivateAsyncOperationasyncOn;
//是否加载完成privateboolisLoad=false;
privatevoidAwake()
  {
//单例Instance=this;
//找到对应的组件loading=transform.Find("Loading").GetComponent<Image>();
transform.localScale=Vector3.zero;
  }
//加载场景publicvoidLoadScene()
  {
//DOTween相关使用transform.DOScale(Vector3.one, 0.3f).OnComplete(() =>    {
//开启携程StartCoroutine("LoadingS");
    });
  }
IEnumeratorLoadingS()
  {
//startValueintdisplayProgress=-1;
//endValueinttoProgress=100;
while (displayProgress<toProgress)
    {
displayProgress++;
//同步UI进度条ShowProgress(displayProgress);
if (isLoad==false)
      {
//场景中有两个初始场景所以从第二个开始算asyncOn=SceneManager.LoadSceneAsync(2+ItemManager.instance.risitionDirection);
//加载完成不让跳转asyncOn.allowSceneActivation=false;
isLoad=true;
      }
yieldreturnnewWaitForEndOfFrame();
    }
if (displayProgress==100)
    {
//跳转asyncOn.allowSceneActivation=true;
StopCoroutine("LoadingS");
    }
  }
privatevoidShowProgress(intprogress)
  {
loading.fillAmount=progress*0.01f;
  }
}

效果如下:

image.png

AntorController 脚本


实现的功能:实现火车到达终点播放声音

usingUnityEngine;
usingUnityEngine.SceneManagement;
publicclassAntorController : MonoBehaviour{
publicAudioSourceaudioS;
privateAnimationanim;
/// <summary>/// 动画片段的时间/// </summary>privatefloatclipTime;
/// <summary>/// 是否播放欢呼音效/// </summary>privateboolisPlay=false;
/// <summary>/// 动画是否播放完/// </summary>privateboolisEnd=false;
/// <summary>/// 计时器/// </summary>privatefloattimer=0f;
privatevoidAwake()
  {
//获取相关组件anim=GetComponent<Animation>();
clipTime=anim.clip.length;
  }
privatevoidUpdate()
  {
clipTime-=Time.deltaTime;
if (clipTime<=10&&isPlay==false)
    {
//到达终点播放声音audioS.Play();
isPlay=true;
    }
if (clipTime<=0&&isEnd==false)
    {
UnityEngine.XR.InputTracking.disablePositionalTracking=false;
//调整场景SceneManager.LoadScene("StartScene");
//结束游戏isEnd=true;
    }
  }
}

最后


本文到这里就结束了,觉得不错的请给我专栏点点订阅,你的支持是我们更新的动力,感谢大家的支持,希望这篇文章能帮到大家

qaajrr4nrhqw4_ec4595923d864f9fb3c9717aec8c2200.png

下篇文章再见ヾ( ̄▽ ̄)ByeBye

image.png

相关文章
|
Android开发
AppsFlyer 研究(十一)配置 Google Ads MCC 获取 LINK ID
AppsFlyer 研究(十一)配置 Google Ads MCC 获取 LINK ID
149 0
|
前端开发 程序员 vr&ar
School StartsFirstProject~UnityVR(HTCVive设备开发)(一)
School StartsFirstProject~UnityVR(HTCVive设备开发)
School StartsFirstProject~UnityVR(HTCVive设备开发)(一)
|
C语言
chapter 9用户自己建立数据类型(下)
chapter 9用户自己建立数据类型
97 0
chapter 9用户自己建立数据类型(下)
|
JavaScript 前端开发 容器
[译]Functor 与 Category (软件编写)(第六部分)
本文讲的是[译]Functor 与 Category (软件编写)(第六部分),所谓 functor(函子),是能够对其进行 map 操作的对象。换言之,functor 可以被认为是一个容器,该容器容纳了一个值,并且暴露了一个接口(译注:即 map 接口),该接口使得外界的函数能够获取容器中的值。
1166 0