unity 播放视频 WWW下载StreamingAssets文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity 播放视频 WWW下载StreamingAssets文件相关的知识,希望对你有一定的参考价值。
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文件的主要内容,如果未能解决你的问题,请参考以下文章