本地资源加载方法
1. private AudioClip SetAudio(string StrPath)
{
AudioClip clip=null;
clip = (AudioClip)Resources.Load(StrPath, typeof(AudioClip));
return clip;
}
这个是加载声音的 注意这里的strpath 如果用了Resources.Load 那么就需要assets文件夹下有Resources文件夹 资源必须在Resources文件夹里面 strpath指定Resources文件夹以下的路径
2. 路径加载 用www
public static IEnumerator CoroutineLoad(string url, Action<WWW> action = null) //url=Global.LOCAL_RES_URL + "card/" + cardBase.cardID + ".png" Global.LOCAL_RES_URL="file://" + Application.dataPath + "/Cache/";
{
WWW www = new WWW(url);
float time_start = Time.time;
_retry_times = 0;
while (!www.isDone)
{
if (Time.time - time_start > RETRY_LAG)
{
if (_retry_times == 0)
{
_download_progress = www.progress;
GameLog.LogError("[DOWNLOAD]first retry: " + url + ", progress: " + _download_progress);
time_start = Time.time;
_retry_times ++;
continue;
}
if (_download_progress == www.progress)
{
if (_retry_times > 1)
{
GameLog.LogError("[DOWNLOAD]download failed: " + url + ", progress: " + _download_progress);
www.Dispose();
yield break;
}
www.InitWWW(url, null, null);
time_start = Time.time;
_retry_times ++;
continue;
}
_retry_times = 0;
}
yield return null;
}
//yield return www;
if (!string.IsNullOrEmpty(www.error))
{
Download.hasError = true;
GameLog.LogError(www.error);
}
if (action != null) action(www);
www.Dispose(); 这个很重要 忘写了 流就会不关闭 这样虚拟机跑不起来
}
3. filePath = Application.dataPath + @"/_Image/grid.png";
System.Drawing.Image img = System.Drawing.Image.FromStream(fs);
//System.Drawing.Image.FromFile(filePath);
img.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
Texture2D _tex2 = new Texture2D(128, 128);
_tex2.LoadImage(ms.ToArray());
//此处为GameObject的材质类附上读取的纹理;
_newObj.renderer.material.mainTexture = _tex2;
{
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
string fileName = path + "/" + name;
if (File.Exists(fileName) && !replace) return;
try
{
FileStream fileStream = new FileStream(fileName, FileMode.Create);
if (content != null) fileStream.Write(content, 0, content.Length);
fileStream.Flush();
fileStream.Close();
}
catch (IOException ex)
{
GameLog.LogError(ex.Message);
}
}
浙公网安备 33010602011771号