unity 文件流读取图片和www读取图片的比较

IO流代码:

void LoadByIO() {
        float time = Time.time;
        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
        fs.Seek(0, SeekOrigin.Begin);
        byte[] bytes = new byte[fs.Length];
        fs.Read(bytes, 0, (int)fs.Length);
        fs.Close();
        fs.Dispose();
        fs = null;

        Texture2D t = new Texture2D(1,1);
        t.LoadImage(bytes);
        img.texture = t;
        Debug.Log("IO读取图片用时:" + (Time.time-time));
    }

WWW代码:

IEnumerator LoadByWWW() {
        float time = Time.time;
        WWW w = new WWW("file://" + path);
        yield return w;
        if (string.IsNullOrEmpty(w.error) == false)
        {
            Debug.Log("error");
        }
        else {
            img1.texture = w.texture;
        }
        Debug.Log("www读取图片用时:" + (Time.time - time));
    }

结果截图:

unity 文件流读取图片和www读取图片的比较_第1张图片

你可能感兴趣的:(unity)