1. 程式人生 > >unity 播放視頻 WWW下載StreamingAssets文件

unity 播放視頻 WWW下載StreamingAssets文件

oge nsf nbsp sys resource text 紋理 engine ges

1.

技術分享

2.

技術分享

3.

技術分享

4.

技術分享

5.

技術分享

6.代碼如下

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class test : MonoBehaviour
{
public MovieTexture kk;
bool stopflag = false;
AudioClip _clip;
AudioSource _source;
// Use this for initialization
void Start()
{
//var _movie=Resources.Load<MovieTexture>("movie/movie");
//設置當前對象的主紋理為電影紋理
transform.GetComponent<Renderer>().material.mainTexture = kk;
//設置電影紋理播放模式為循環
kk.loop = true;
_source = Camera.main.GetComponent<AudioSource>();
_clip = kk.audioClip;
GameObject.Find("Playbtn").GetComponent<Button>().onClick.AddListener(() =>
{
kk.Play();
if(stopflag)
{
_source.Play();
}
});
GameObject.Find("Pausebtn").GetComponent<Button>().onClick.AddListener(() =>
{
kk.Pause();
});
GameObject.Find("Stopbtn").GetComponent<Button>().onClick.AddListener(() =>
{
kk.Stop();
stopflag = true;
});
}

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

}
}

7.我希望可以動態加載音頻文件,而不是拖動的

技術分享

8.代碼如下

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class test : MonoBehaviour
{
//public MovieTexture kk;
bool stopflag = false;
AudioClip _clip;
AudioSource _source;
// Use this for initialization
void Start()
{
var kk = Resources.Load<MovieTexture>("movie/movie");
//設置當前對象的主紋理為電影紋理
transform.GetComponent<Renderer>().material.mainTexture = kk;
//設置電影紋理播放模式為循環
kk.loop = true;
_source = Camera.main.GetComponent<AudioSource>();
_clip = kk.audioClip;
_source.clip = _clip;
GameObject.Find("Playbtn").GetComponent<Button>().onClick.AddListener(() =>
{
kk.Play();
_source.Play();
if (stopflag)
{
stopflag = false;
_source.Play();
}
});
GameObject.Find("Pausebtn").GetComponent<Button>().onClick.AddListener(() =>
{
kk.Pause();
});
GameObject.Find("Stopbtn").GetComponent<Button>().onClick.AddListener(() =>
{
kk.Stop();
stopflag = true;
});
}

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

}
}

9.

技術分享

技術分享

10.WWW加載StreamingAssets的文件

技術分享

技術分享

11.全部代碼

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class test : MonoBehaviour
{
bool stopflag = false;
AudioClip _clip;
AudioSource _source;
WWW www;
private MovieTexture kk;
// Use this for initialization
void Start()
{
StartCoroutine(Down());//異步操作
GameObject.Find("Playbtn").GetComponent<Button>().onClick.AddListener(() =>
{
kk.Play();
_source.Play();
if (stopflag)
{
stopflag = false;
_source.Play();
}
});
GameObject.Find("Pausebtn").GetComponent<Button>().onClick.AddListener(() =>
{
kk.Pause();
});
GameObject.Find("Stopbtn").GetComponent<Button>().onClick.AddListener(() =>
{
kk.Stop();
stopflag = true;
});
}

IEnumerator Down()//異步操作
{
www = new WWW("file:///" + Application.streamingAssetsPath + "/movie.ogg");
yield return www;//等待,知道www把資源下載完,執行下一步
if (string.IsNullOrEmpty(www.error))
{
Debug.LogError("錯誤為NULL!");
}
if(www.error=="")
{
Debug.LogError("錯誤為空!");//這個沒進來
}
if (www.error == null)
{
Debug.LogError("錯誤為null");
}
if (www.isDone)
{
Debug.LogError("down");
}
Debug.LogError(www.error);
kk = www.movie;
//設置當前對象的主紋理為電影紋理
transform.GetComponent<Renderer>().material.mainTexture = kk;
//設置電影紋理播放模式為循環
kk.loop = true;
_source = Camera.main.GetComponent<AudioSource>();
_clip = kk.audioClip;
_source.clip = _clip;
}

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

}
}

unity 播放視頻 WWW下載StreamingAssets文件