赞
踩
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);
}
}
}
这张图中对应的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);
}
}
}
这个画面中会加载上面的脚本,上面脚本中会动态创建一个类型为ImageTarget-TransparentVideo-DynamicLoad的gameObject
如下:
GameObject go = new GameObject("ImageTarget-TransparentVideo-DynamicLoad");
然后再添加一个组件ImageTarget_DynamicLoad_ManualPlay,代码如下:
target = go.AddComponent<ImageTarget_DynamicLoad_ManualPlay>();
这次为什么是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));
剩下的事就是ImageTarget_DynamicLoad_ManualPlay脚本来处理了。
上面的事情做完后,tracker就会去追踪camera,如果识别出来了配置的图片,就会调用ImageTarget_DynamicLoad_ManualPlay中的OnTargetFound,然后就会播放视频了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。