本地资源加载方法

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);
            }
        }
 

转载于:https://www.cnblogs.com/mengdi520123/p/4537824.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值