Unity之使用Assetbundle更新视频文件

时间:2023-01-05 12:59:09


承接之前讲过的Assetbundle的内容。这次要讲解更新游戏中的CG的视频案例。

开发内容:点击按钮加载视频数据,按鼠标右键播放加载完成的视频。

第一步,新建一个unity的场景。

场景非常简单,只有一个RawImage来播放视频。

Unity之使用Assetbundle更新视频文件

RawImage播放视频的方法,在Textrue的位置需要一个rendertexture,所以新建一个放到这里。

Unity之使用Assetbundle更新视频文件

第二步:在项目新建一个文件夹名叫Resourceshaha。多么接地气的名字~~就要哈哈哈。然后里面放了一些相关脚本和要被播放的视频。

Unity之使用Assetbundle更新视频文件

然后给这个整个文件夹做成assetbundle,命名为reso.assetbundle。如何打包assetbundle之前的文章讲过此处不再赘述。

Unity之使用Assetbundle更新视频文件

接下来分析一下思路和代码。上述文件夹里只有两个脚本,一个是加载视频文件的脚本,另一个是播放视频文件的脚本。

首先讲loadmoviesassets脚本,该脚本负责加载reso.assetbundle这个东西。只要加载到这个东西就可以找到里面的视频文件了。如下:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class loadmoviesassets : MonoBehaviour {


public List<Object> goes=new List<Object>();
private string url;
string PathA;
// Use this for initialization
private void Awake()
{
PathA = System.Environment.CurrentDirectory;

//因为测试是使用的本地路径,本地路径的斜杠是“\”所以需要用“/”来代替

if (PathA.Contains("\\"))
{
PathA = PathA.Replace("\\", "/");
}

url = "file://" + PathA + "/AllAssets/reso.assetbundle";
Debug.Log(url);
}

private void OnGUI()
{
Rect rect = new Rect(100f, 100f, 200f, 200f);
if (GUI.Button(rect, "Load"))
{
StartCoroutine(LoadAssetsFun());
}
}

IEnumerator LoadAssetsFun()
{

using (WWW www = new WWW(url))

{
yield return www;

if (www.error != null)
{
print("wrong");
}
else
{

AssetBundle bundle = www.assetBundle;
string[] aa = bundle.GetAllAssetNames();
foreach (string allname in aa)
{
Debug.Log("物体名字:" + allname);

}


Object[] obj = bundle.LoadAllAssets();

foreach (Object ass in obj)
{
Debug.Log(ass.name);
Object kk = Instantiate(ass)as Object;//
goes.Add(kk);//把加载过后生成到的物体添加到数组里
}


AssetBundle manifesAB = AssetBundle.LoadFromFile("AllAssets/AllAssets");
AssetBundleManifest manifest = manifesAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
//GetAllDependencies 获得所有依赖对象

string[] strs = manifest.GetAllDependencies("reso.assetbundle");
//将所有依赖对象加载出来
foreach (var name in strs)
{
AssetBundle anim = AssetBundle.LoadFromFile("AllAssets/" + name);
Debug.Log(name.ToString());
anim.Unload(false);
}

bundle.Unload(false);
manifesAB.Unload(false);
}
}

}

// Update is called once per frame
void Update () {

}
}

注意:上述代码中的路径设置的和unity的asset的目录是同一级。所以制作好的assetbundle位置要放到这个位置。如图;

Unity之使用Assetbundle更新视频文件

之后看播放代码playvideo。如下:

using UnityEngine;
using System.Collections;
using System.IO;
using UnityEngine.UI;
using System.Collections.Generic;

public class playvideo : MonoBehaviour
{

private AudioSource audio;
private MovieTexture movieTexture;

void Start()
{



}

/// <summary>
/// 播放
/// </summary>
public void OnPlayerVideo()
{
//从加载到的数组里查找名字叫做HeroVideo(Clone)的物体,这个物体就是视频
foreach (Object go in this.GetComponent<loadmoviesassets>().goes)
{

if (go.name == "HeroVideo(Clone)")
{
movieTexture = go as MovieTexture;
Debug.Log("播放啦啦啦");

//播放视频
if (!movieTexture.isPlaying)
{
this.GetComponent<RawImage>().texture = movieTexture;
movieTexture.Play();
}
}
else

{
Debug.Log("没有视频");
}

}

//加载视频的声音并播放
audio = this.GetComponent<AudioSource>();
audio.clip = movieTexture.audioClip;
audio.Play();


}

// Update is called once per frame
void Update()
{
//点击右键播放
if (Input.GetMouseButtonDown(1))
{
OnPlayerVideo();
}
}
}

上述代码中是使用视频的名字来作为条件进行判断是否为视频文件的。这个视频的名字是自己定义的以后就不会改变了,即使视频内容改变了,视频文件的名字也必须叫做HeroVideo(Clone)这个名字。因为加载出来的物体都有(Clone)。

最后我们运行测试:

当运行的时候首先会有一个GUI制作的按钮,点击该按钮进行加载东西,如图:

Unity之使用Assetbundle更新视频文件

 

这时候会看到rawimage上的loadmoviesassets的脚本里加载到了reso.assetbundle里的所有文件,所有的后面都加了一个(Clone)。

Unity之使用Assetbundle更新视频文件

最后点击鼠标右键进行播放,如图:

Unity之使用Assetbundle更新视频文件

本文只讲方法,其中有些代码未做优化,参考学习而已。