当前位置:   article > 正文

easyar unity3d播放视频_unity easy arbof播放视频

unity easy arbof播放视频
  public class ImageTarget_DynamicLoad_ManualPlay : ImageTargetBehaviour
    {
        private bool loaded;
        private bool found;
        private System.EventHandler videoReayEvent;
        private VideoPlayerBaseBehaviour videoPlayer;
        private string video = "transparentvideo.mp4";

        protected override void Awake()
        {
            base.Awake();
            TargetFound += OnTargetFound;
            TargetLost += OnTargetLost;
            TargetLoad += OnTargetLoad;
            TargetUnload += OnTargetUnload;
        }

        protected override void Start()
        {
            videoReayEvent = OnVideoReady;
            base.Start();
            LoadVideo();
        }

        public void LoadVideo()
        {
            GameObject subGameObject = Instantiate(Resources.Load("TransparentVideo", typeof(GameObject))) as GameObject;
            subGameObject.transform.parent = this.transform;
            subGameObject.transform.localPosition = new Vector3(0, 0.1f, 0);
            subGameObject.transform.localRotation = new Quaternion();
            subGameObject.transform.localScale = new Vector3(0.5f, 0.2f, 0.3154205f);

            videoPlayer = subGameObject.GetComponent<VideoPlayerBaseBehaviour>();
            if (videoPlayer)
            {
                videoPlayer.Storage = StorageType.Assets;
                videoPlayer.Path = video;
                videoPlayer.EnableAutoPlay = false;
                videoPlayer.EnableLoop = true;
                videoPlayer.Type = VideoPlayerBaseBehaviour.VideoType.TransparentSideBySide;
                videoPlayer.VideoReadyEvent += videoReayEvent;
                videoPlayer.Open();
            }
        }

        public void UnLoadVideo()
        {
            if (!videoPlayer)
                return;
            videoPlayer.VideoReadyEvent -= videoReayEvent;
            videoPlayer.Close();
            loaded = false;
        }

        void OnVideoReady(object sender, System.EventArgs e)
        {
            Debug.Log("Load video success");
            VideoPlayerBaseBehaviour player = sender as VideoPlayerBaseBehaviour;
            loaded = true;
            if (player && found)
                player.Play();
        }

        void OnTargetFound(TargetAbstractBehaviour behaviour)
        {
            found = true;
            if (videoPlayer && loaded)
                videoPlayer.Play();
            Debug.Log("Found: " + Target.Id);
        }

        void OnTargetLost(TargetAbstractBehaviour behaviour)
        {
            found = false;
            if (videoPlayer && loaded)
                videoPlayer.Pause();
            Debug.Log("Lost: " + Target.Id);
        }

        void OnTargetLoad(ImageTargetBaseBehaviour behaviour, ImageTrackerBaseBehaviour tracker, bool status)
        {
            Debug.Log("Load target (" + status + "): " + Target.Id + " (" + Target.Name + ") " + " -> " + tracker);
        }

        void OnTargetUnload(ImageTargetBaseBehaviour behaviour, ImageTrackerBaseBehaviour tracker, bool status)
        {
            Debug.Log("Unload target (" + status + "): " + Target.Id + " (" + Target.Name + ") " + " -> " + tracker);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91

这里写图片描述

这张图中对应的ImageTarget-StreamingVideo-DynamicLoad中的一个组件是ImageTarget_DynamicLoad_ManualPlay,代码就是上面的代码。会看到里面有 path name storage等属性,这些属性是被ImageTargetBehaviour暴露出来的。
有注意到ImageTarget-StreamingVideo-DynamicLoad 这个gameObject下面没有追踪的对象,那这个对象是怎么添加的。答案就是ImageTarget_DynamicLoad_ManualPlay脚本的组件中动态加载的,可以看上面的代码。

这里写图片描述

在ImageTarget_DynamicLoad_ManualPlay脚本的代码中,看到有
videoPlayer = subGameObject.GetComponent<VideoPlayerBaseBehaviour>();
这个方法是获取一个videoPlayer组件,注意这个方法是GetComponent,既然是getxxx,那么gameObject中肯定会有这个组件,上图中左红箭头,可以看到确实有这个组件。

动态加载GameObject

//=============================================================================================================================
//
// Copyright (c) 2015-2017 VisionStar Information Technology (Shanghai) Co., Ltd. All Rights Reserved.
// EasyAR is the registered trademark or trademark of VisionStar Information Technology (Shanghai) Co., Ltd in China
// and other countries for the augmented reality technology developed by VisionStar Information Technology (Shanghai) Co., Ltd.
//
//=============================================================================================================================

using UnityEngine;
using EasyAR;

namespace EasyARSample
{
    public class HelloARVideo : MonoBehaviour
    {
        private const string title = "Please enter KEY first!";
        private const string boxtitle = "===PLEASE ENTER YOUR KEY HERE===";
        private const string keyMessage = ""
            + "Steps to create the key for this sample:\n"
            + "  1. login www.easyar.com\n"
            + "  2. create app with\n"
            + "      Name: HelloARVideo (Unity)\n"
            + "      Bundle ID: cn.easyar.samples.unity.helloarvideo\n"
            + "  3. find the created item in the list and show key\n"
            + "  4. replace all text in TextArea with your key";

        private ImageTarget_DynamicLoad_ManualPlay target;

        private void Awake()
        {
            if (FindObjectOfType<EasyARBehaviour>().Key.Contains(boxtitle))
            {
#if UNITY_EDITOR
                UnityEditor.EditorUtility.DisplayDialog(title, keyMessage, "OK");
#endif
                Debug.LogError(title + " " + keyMessage);
            }
        }

        private void Start()
        {
            if (!target)
            {
                GameObject go = new GameObject("ImageTarget-TransparentVideo-DynamicLoad");
                target = go.AddComponent<ImageTarget_DynamicLoad_ManualPlay>();
            }
            target.ActiveTargetOnStart = false;
            target.Bind(ARBuilder.Instance.ImageTrackerBehaviours[0]);
            target.SetupWithImage("idback.jpg", EasyAR.StorageType.Assets, "idback", new Vector2(8.56f, 5.4f));
        }

        private void OnDestory()
        {
            if (!target)
                return;
            target.Bind(null);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

这里写图片描述

这个画面中会加载上面的脚本,上面脚本中会动态创建一个类型为ImageTarget-TransparentVideo-DynamicLoad的gameObject
如下:

 GameObject go = new GameObject("ImageTarget-TransparentVideo-DynamicLoad");
  • 1

然后再添加一个组件ImageTarget_DynamicLoad_ManualPlay,代码如下:

target = go.AddComponent<ImageTarget_DynamicLoad_ManualPlay>();
  • 1

这次为什么是AddComponent而不是GetComponent?
很明显因为这次是完全动态创建gameObject,上一个是gameObject存在,而且里面也有一个component,所以上一个不用add了,直接get就行了。
接下来配置这个组件:

target.ActiveTargetOnStart = false;
         //绑定tracker,不然没法追踪识别图片。   target.Bind(ARBuilder.Instance.ImageTrackerBehaviours[0]);
            target.SetupWithImage("idback.jpg", EasyAR.StorageType.Assets, "idback", new Vector2(8.56f, 5.4f));
  • 1
  • 2
  • 3

剩下的事就是ImageTarget_DynamicLoad_ManualPlay脚本来处理了。

上面的事情做完后,tracker就会去追踪camera,如果识别出来了配置的图片,就会调用ImageTarget_DynamicLoad_ManualPlay中的OnTargetFound,然后就会播放视频了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/118060
推荐阅读
相关标签
  

闽ICP备14008679号