Unity之使用Assetbundle更新視頻文件

承接之前講過的Assetbundle的內容。這次要講解更新遊戲中的CG的視頻案例。

開發內容:點擊按鈕加載視頻數據,按鼠標右鍵播放加載完成的視頻。

第一步,新建一個unity的場景。

場景非常簡單,只有一個RawImage來播放視頻。

RawImage播放視頻的方法,在Textrue的位置需要一個rendertexture,所以新建一個放到這裏。

第二步:在項目新建一個文件夾名叫Resourceshaha。多麼接地氣的名字~~就要哈哈哈。然後裏面放了一些相關腳本和要被播放的視頻。

然後給這個整個文件夾做成assetbundle,命名爲reso.assetbundle。如何打包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位置要放到這個位置。如圖;

之後看播放代碼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製作的按鈕,點擊該按鈕進行加載東西,如圖:

 

這時候會看到rawimage上的loadmoviesassets的腳本里加載到了reso.assetbundle裏的所有文件,所有的後面都加了一個(Clone)。

最後點擊鼠標右鍵進行播放,如圖:

本文只講方法,其中有些代碼未做優化,參考學習而已。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章