使用Unity开发,动态加载一个识别对象,手动控制视频的Open和Close,但是使用过程中出现一个问题,TargetFound时,没有触发OnVideoReady事件,所以就无法播放视频。具体代码如下:
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.OpenWhenStart = false;
videoPlayer.EnableLoop = true;
videoPlayer.Type = VideoPlayerBaseBehaviour.VideoType.TransparentSideBySide;
videoPlayer.VideoReadyEvent += videoReayEvent;
}
}
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.Open();
videoPlayer.Play();
}
Debug.Log("Found: " + Target.Id);
}
void OnTargetLost(TargetAbstractBehaviour behaviour)
{
found = false;
if (videoPlayer && loaded)
{
videoPlayer.Stop();
videoPlayer.Close();
}
Debug.Log("Lost: " + Target.Id);
}
这个是我用ImageTarget_DynamicLoad_ManualPlay脚本中的代码修改后的主要代码,其他脚本没有改动,不知道是什么原因?