Unity 加载帧动画(加载PC图片和手机端(android)图片)

今天来说说,unity加载pc端图片和手机端图片的两种方法: WWW加载 和 传统IO加载

  1. 首先说说 WWW 加载
public void loadByWww(){
      StartCoroutine (Load());

  }
    IEnumerator Load()
    {
        double startTime = (double)Time.time;
        //+ currentIndex.ToString ("D5")
        WWW www = new WWW ("file://F:\\imageTest\\jiaoao1-2guodu" + currentIndex.ToString ("D5") + ".jpg" );
        yield return www;

        if(www != null && string.IsNullOrEmpty(www.error)){
            GetComponent().material.mainTexture = www.texture;
        }

    }

首先www 既可以加载网络图片 也可以加载本地图片 本地图片路径:
1.pc端本地 : file://F:\imageTest\jiaoao1-2guodu.png
2.android手机端SD卡:/storage/emulated/0/ImageTest/jiaoao1-2guodu.png

2.传统IO流加载

          FileStream fileStream = new FileStream("F:\\imageTest\\jiaoao1-2guodu"+ currentIndex.ToString ("D5") + ".jpg", FileMode.Open, FileAccess.Read);

//          FileStream fileStream = new FileStream("/storage/emulated/0/ImageTest/jiaoao1-2guodu" + currentIndex.ToString ("D5") + ".jpg" , FileMode.Open, FileAccess.Read);

          fileStream.Seek(0, SeekOrigin.Begin);   
          //创建文件长度缓冲区
          byte[] bytes = new byte[fileStream.Length];
          //读取文件
          fileStream.Read(bytes, 0, (int)fileStream.Length);
          //释放文件读取流
          fileStream.Close();
          fileStream.Dispose();
          fileStream = null;
          //创建Texture
          int width=1280;
          int height=800;
          Texture2D texture = new Texture2D(width, height);
          texture.LoadImage(bytes);

          GetComponent().material.mainTexture = texture;

传统IO流加载本地图片路径:
1.pc端本地 :F:\imageTest\jiaoao1-2guodu.png
2.android手机端SD卡:/storage/emulated/0/ImageTest/jiaoao1-2guodu.png

总结:
经测试IO加载要比WWW加载更快,效率更高。

你可能感兴趣的:(Unity 加载帧动画(加载PC图片和手机端(android)图片))