|
{ /// <summary> /// 视频播放模块 /// </summary> private VideoPlayerBehaviour videoPlayer; /// <summary> /// 视频文件路径 /// </summary> public string VideoPath; /// <summary> /// 是否自动播放视频 /// </summary> public bool VideoEnableAutoPlay = true; /// <summary> /// 是否允许视频循环 /// </summary> public bool VideoEnableLoop = true; /// <summary> /// 视频类型 /// </summary> public VideoPlayer.VideoType VideoType = VideoPlayer.VideoType.TransparentSideBySide; /// <summary> /// 视频资源类型 /// </summary> public StorageType VideoStorage = StorageType.Assets; /// <summary> /// 视频是否加载 /// </summary> private bool isVideoLoaded; protected override void Start() { //在Start方法中加载视频、隐藏模型 base.Start(); LoadVideo(); HideObjects(transform); } /// <summary> /// 加载视频 /// </summary> private void LoadVideo() { //创建子物体VideoObject并为其添加视频组件 GameObject VideoObject = new GameObject("VideoObject"); videoPlayer = VideoObject.AddComponent<VideoPlayerBehaviour>(); VideoObject.transform.SetParent(transform); VideoObject.transform.localPosition = Vector3.zero; VideoObject.transform.localRotation = Quaternion.identity; VideoObject.transform.localScale = Vector3.one; //设置视频组件相关参数 videoPlayer.Storage = VideoStorage; videoPlayer.Path = VideoPath; videoPlayer.EnableAutoPlay = VideoEnableAutoPlay; videoPlayer.EnableLoop = VideoEnableLoop; videoPlayer.Type = VideoType; videoPlayer.VideoReadyEvent+=videoPlayer_VideoReadyEvent; videoPlayer.VideoReachEndEvent+=videoPlayer_VideoReachEndEvent; videoPlayer.VideoErrorEvent+=videoPlayer_VideoErrorEvent; videoPlayer.Open(); videoPlayer.Play(); } #region 视频组件相关事件定义 public virtual void videoPlayer_VideoErrorEvent(object sender, System.EventArgs e) { } public virtual void videoPlayer_VideoReachEndEvent(object sender, System.EventArgs e) { } public virtual void videoPlayer_VideoReadyEvent(object sender, System.EventArgs e) { } #endregion /// <summary> /// 隐藏模型的方法 /// </summary> /// <param name="trans">要隐藏的Transform</param> void HideObjects(Transform trans) { for (int i = 0; i < trans.childCount; ++i) HideObjects(trans.GetChild(i)); if (transform != trans) gameObject.SetActive(false); } /// <summary> /// 显示模型的方法 /// </summary> /// <param name="trans">要显示的Transform</param> public void ShowObjects(Transform trans) { for (int i = 0; i < trans.childCount; ++i) ShowObjects(trans.GetChild(i)); if (transform != trans) gameObject.SetActive(true); } /// <summary> /// 实现ITargetEventHandler接口中的OnTargetFound方法 /// </summary> /// <param name="target">识别目标</param> void ITargetEventHandler.OnTargetFound(Target target) { if (videoPlayer) videoPlayer.Play(); ShowObjects(transform); } /// <summary> /// 实现ITargetEventHandler接口中的OnTargetLost方法 /// </summary> /// <param name="target">识别目标</param> void ITargetEventHandler.OnTargetLost(Target target) { if (videoPlayer) videoPlayer.Pause(); HideObjects(transform); } /// <summary> /// 实现ITargetEventHandler接口中的OnTargetLoad方法 /// </summary> /// <param name="target">识别目标</param> void ITargetEventHandler.OnTargetLoad(Target target, bool status) { } /// <summary> /// 实现ITargetEventHandler接口中的OnTargetUnload方法 /// </summary> /// <param name="target">识别目标</param> void ITargetEventHandler.OnTargetUnload(Target target, bool status) { } } |
* 在Assets/EasyAR/Prefabs目录下找到EasyAR这个预制体,添加EasyARConfig组件,然后填写KEY。
* 在Assets/EasyAR/Prefabs目录中找到ImageTarget这个预制体,然后使用VideoTargetBaseBehaviour组件替换默认的ImageTargetBehaviour组件。