Unity:读取本地图片

  //读取本地照片
    private async void LoadByIO(string url)
    {      
        if (url != null)
        {
            //创建文件读取流
            FileStream fileStream = new FileStream(url, 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 = 45;
            int height = 55;

            Texture2D texture = new Texture2D(width, height);
            texture.LoadImage(bytes);

            //创建Sprite--把Texture转成sprite
            Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
             //要显示图片的地方
            Photo.sprite = sprite;
        }
    
    }

你可能感兴趣的:(学习记录)