码迷,mamicode.com
首页 > 其他好文 > 详细

本地资源加载方法

时间:2015-05-29 11:25:57      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

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";
 

           FileStream fs = new FileStream(filePath,FileMode.Open,FileAccess.Read);
            System.Drawing.Image img = System.Drawing.Image.FromStream(fs);
//System.Drawing.Image.FromFile(filePath);
 
4.   
            MemoryStream ms = new MemoryStream();
            img.Save(ms,System.Drawing.Imaging.ImageFormat.Png);

            Texture2D _tex2 = new Texture2D(128, 128);
            _tex2.LoadImage(ms.ToArray());

            //此处为GameObject的材质类附上读取的纹理;
            _newObj.renderer.material.mainTexture = _tex2;
5  创建和读取资源
                CreateFile(Global.LOCAL_RES_DIR, "version.bin", System.Text.Encoding.Default.GetBytes(Download.VersionLocal.Aggregate(string.Empty, (str, kv) => str + kv.Key + "," + kv.Value + "\n")));

            CreateFile(Global.LOCAL_RES_DIR, "version.bin", null, false);

 public void CreateFile(string path, string name, byte [] content = null, bool replace = true)
        {
            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);
            }
        }
 

本地资源加载方法

标签:

原文地址:http://www.cnblogs.com/mengdi520123/p/4537824.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!