1. 程式人生 > >Unity3d外部加載音頻,視頻,圖片等資源 及根據路徑獲取制定格式的文件

Unity3d外部加載音頻,視頻,圖片等資源 及根據路徑獲取制定格式的文件

filename hpa https 制定文件 text 圖片 turn .get 路徑

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://"[email protected];

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://"[email protected]_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://"[email protected]_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外部加載音頻,視頻,圖片等資源 及根據路徑獲取制定格式的文件