unity的图片处理操作

unity中常常用的图片格式就是texture/texture/sprite了。其中当满足unity解析格式的图片被拖入进去的话,就会自动被解析成了sprite类型的文件了。
平时的扩展常用的导入unity中的就是.png和.jpg格式了,拖入到unity中后会被自动解析成texture格式了,这个就是模型上可以用的贴图格式了,如果想将之改编成UI可用的格式的话,那就是将之转换成了Sprite格式的文件了。

texture和sprite的区别
区别在于:
Sprite 只能用在 Image 组件上.
其他格式使用 Raw Image.
sprite多了一些关于动画的控制.
Sprite可以做九宫.
Sprite一般用在小图.可以打Atlas.只能用默认压缩格式.

Texture可以选择Advance来控制更多选项.比如是否生成MipMaps.选择压缩格式等.可以通过 WWW 显示一张Texture


(1)传统IO操作
①将图片从某位置(如果是从网络,那么就需要socket,如果是本地,直接进行操作,总之就是将图片转换成流的形式进行操作了)加载入unity并且转换成sprite(中间会将图片转换成Texture或者Texture2D的格式)


//读取图片并转换为byte数组
    byte[] ReadPicture()
    {
        FileStream fs = new FileStream("e:/tsj.jpg", FileMode.Open);
        byte[] binary = new byte[fs.Length];
        fs.Read(binary, 0, (int)fs.Length);
        fs.Close();
        fs.Dispose();
        return binary;
    }
//上述得到的数组转换成Sprite
IEnumerator WritePicture()
    {
        Texture2D texture = new Texture2D(1024, 1024);
        texture.LoadImage(ReadPicture());
        Sprite prite = Sprite.Create(texture, new Rect(0, 0, 800, 531), new Vector2(0.5f, 0.5f));
        ima.sprite = prite;
        yield return 0;
        Resources.UnloadUnusedAssets();
    }
②将图片字节数组转换成图片并保存到某目录下。
//上述得到的数组写到资源目录下
   void WritePictureByte(byte [] picByte)
    {
        FileStream fs = new FileStream(Application.dataPath+"/aaa.jpg",FileMode.Create,FileAccess.Write);
        fs.Write(picByte,0,picByte.Length);
        fs.Close();
        fs.Dispose();
    }
③将unity中的Texture保存到某目录下
  private void ImageSaveLocal(Texture tex)
    {
        string path = null;
#if UNITY_
        path=Application.persistentDataPath+"/"+pic_fileName;
#elif UNITY_IPHONE
        path=Application.persistentDataPath+"/"+pic_fileName;
#elif UNITY_EDITOR
        path = Application.dataPath + "/" + "这里写图片名字.jpg";
#endif
        Texture2D saveImageTex = tex as Texture2D;
        FileStream newFs = new FileStream(path, FileMode.Create, FileAccess.Write);
        byte[] bytes = saveImageTex.EncodeToJPG();
        newFs.Write(bytes, 0, bytes.Length);
        newFs.Close();
        newFs.Dispose();
    }
④将Unity中的Sprite保存到某目录下(sprite-texture-字节流)
其实前面已经实现了大部分的功能,还没有实现的功能就是怎么从sprite转换成texture2d

//复杂的转换方法
public Texture2D SpriteToTexture(Sprite sprite)
    {
        Texture2D tex = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height, sprite.texture.format, false);
        tex.SetPixels(sprite.texture.GetPixels((int )sprite.rect.xMin,(int)sprite.rect.yMin,
            (int)sprite.rect.width,(int)sprite.rect.height));
        tex.Apply();
        return tex;
    }

当然也可以直接从要转换的Sprite上点出Texture,比如sprite.texture
目前尚且不知道区别在哪里。

当遇到权限问题的话,在图片的下面有一个Advance的下拉按钮,点击之后有一个Read/Write Enabled选项,勾上之后就可以正常的读写了。

(2)WWW的操作(代码相对简单,但是效率比较低)
用WWW进行加载的时候其实就直接省去了字节流的转换步骤,而直接用www.texture就可以拿到相应的jpg或者png格式的图片来了,而且因为省去了字节操作,Texture2D的创建也不用填入width和height了

    // 以WWW方式进行加载
    private void LoadByWWW()
    {
        StartCoroutine(Load());
    }

    IEnumerator Load()
    {
        double startTime = (double)Time.time;
        //请求WWW
        WWW www = new WWW("file://D:\\test.jpg");
        yield return www;        
        if(www != null && string.IsNullOrEmpty(www.error))
        {
            //获取Texture
            Texture2D texture=www.texture;

            //创建Sprite
            Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
            image.sprite = sprite;

            startTime = (double)Time.time - startTime;
            Debug.Log("WWW加载用时:" + startTime);
        }
    }

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值