[Unity] Unity3D如何实现 Sprite和Texture2D互转

查看:869 |回复:3 | 2021-2-23 18:16:14

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 源数之力 于 2021-3-2 14:35 编辑

Unity3D如何实现 Sprite和Texture2D互转
2021-2-23 18:16:14  
 赞 赞 0

使用道具 登录

3个回答,把该问题分享到群,邀请大神一起回答。
2#
1:克隆Texture2D //originTex为克隆对象 Texture2D newTex; newTex = new Texture2D(originTex.width, originTex.height); Color[] colors = originTex.GetPixels(0, 0, originTex.width, originTex.height); newTex.SetPixels(colors); newTex.Apply();//必须apply才生效
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
2:Sprite转Texture2D//sprite为图集中的某个子Sprite对象        var targetTex = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height);        var pixels = sprite.texture.GetPixels(            (int)sprite.textureRect.x,            (int)sprite.textureRect.y,            (int)sprite.textureRect.width,            (int)sprite.textureRect.height);        targetTex.SetPixels(pixels);        targetTex.Apply();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

参考:https://answers.unity.com/questions/651984/convert-sprite-image-to-texture.html

3: Texture2D转Sprite//t2d为待转换的Texture2D对象Sprite s= Sprite.Create(t2d, new Rect(0, 0, t2d.width, t2d.height), Vector2.zero);
回复 收起回复
2021-2-23 18:29:33   回复
 赞 赞 1

使用道具 登录

3#
楼上的回答太专业了吧
回复 收起回复
2021-8-31 22:20:59   回复
 赞 赞 2

使用道具 登录

4#
元素是个好家园,加油啊
回复 收起回复
2022-11-3 17:55:43   回复
 赞 赞 1

使用道具 登录

CG 游戏行业专业问题

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表