标签:setpath mon www unity3 音视频 generic nload ini app
1.根据路径获取制定文件类型的文件:
这里写一个类,调用了打开路径的方法:
using UnityEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
public class SetPath : MonoBehaviour
{
public static string pic;
string path;
void Awake ()
{
//设置路径,这里的路径是导出包的exe路径下//
path=Application.dataPath+@"/../Resources";
//这里新建队列,调用访问文件的方法,获取其返回的文件名队列//
//第一个参是访问路径,第二个参是文件格式//
List<String> list_pic=GetDirFiles(path+@"/Picture/test","*.png");
foreach (string item in list_pic)
{
//item是文件名,获取文件名后返回完整路径//
pic=path+@"/Picture/GiftCard/"+item.ToString()+".png";
}
}
public List<string> GetDirFiles(string DirFullPath, string SearchPattern)
{
if (Directory.Exists(DirFullPath) == true)
{
List<string> list = new List<string>();
//获取当前目录下指定文件类型的文件列表//
string[] stringList = Directory.GetFiles(DirFullPath,SearchPattern);
foreach (string str in stringList)
{
string fileName;
fileName = System.IO.Path.GetFileNameWithoutExtension(str);
list.Add(fileName);
}
return list;
}
else
{
return null;
}
}
}
2.外部加载音频
using UnityEngine;
using System.Collections;
public class Exposure_voice : MonoBehaviour {
public string url_voice;
public static AudioClip myclip;//设置静态声音资源,便于传递//
void Start () {
//外部加载声音的路径,拼接www下载文件路径//
//注意,www加载的外部音视频都必须是ogg格式额//
url_voice="file://"+@SetPath.pic;
StartCoroutine("DownloadVoice");
}
IEnumerator DownloadVoice()
{
WWW w=new WWW(url_voice);
yield return w;
//将声音资源赋值为外部加载的声音即可//
myclip=w.audioClip;
}
}
3.外部加载视频
using UnityEngine;
using System.Collections;
public class Exposure_video : MonoBehaviour {
public GameObject video;
public string video_url;
MovieTexture movie_tex;
Material tex;
AudioSource audio;
float temp=0;
void Start ()
{
video_url="file://"+@SetPath.video_name_through;
StartCoroutine("VideoDownload");
Invoke("Initial_effect",9f);
}
IEnumerator VideoDownload()
{
WWW www=new WWW(video_url);
yield return www;
movie_tex=www.movie;
video.renderer.material.mainTexture = movie_tex;
audio.clip=www.GetAudioClip(false,true,AudioType.OGGVORBIS);
audio.Play();
movie_tex.Play();
}
}
4.外部加载图片
using UnityEngine;
using System.Collections;
public class Exposure_Pic : MonoBehaviour {
public GameObject pic;
public string url_pic_Discount;
#region Use this for initialization
void Start ()
{
url_pic_Discount="file://"+@SetPath.pic_name_gift;
StartCoroutine("Pic_Download");
}
#endregion
IEnumerator Pic_Download()
{
WWW www=new WWW(url_pic_Discount);
yield return www;
pic.renderer.material.mainTexture=www.texture;
}
}
这样就可以很容易的加载外部可更换的资源,并且不需要等待长时间的导入了。。。。
---------------------
作者:御雪妃舞
来源:CSDN
原文:https://blog.csdn.net/nanggong/article/details/38276521
版权声明:本文为博主原创文章,转载请附上博文链接!
Unity3d外部加载音频,视频,图片等资源 及根据路径获取制定格式的文件
标签:setpath mon www unity3 音视频 generic nload ini app
原文地址:https://www.cnblogs.com/huwenya/p/9860815.html