("Ass_unity 合并贴图">
当前位置:   article > 正文

unity合并贴图,并读取合并贴图中任意位置贴图_unity 合并贴图

unity 合并贴图

1.合并图片

  1. using System;
  2. using System.IO;
  3. using UnityEditor;
  4. using UnityEngine;
  5. public class TextureCombine
  6. {
  7. [MenuItem("Tools/TextureCombine")]
  8. static void Combine()
  9. {
  10. Texture2D pic1 = AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/Images/pic1.jpg");
  11. Texture2D pic2 = AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/Images/pic2.jpg");
  12. Texture2D pic3 = AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/Images/pic3.jpg");
  13. Texture2D pic4 = AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/Images/pic4.jpg");
  14. Texture2D exportImg = Combine(new[] { pic1, pic2,pic3,pic4 }, new[] { (0, 0), (0, 484),(512,0),(512,512) } , 1024);
  15. File.WriteAllBytes("Assets/CombineImages/out.jpg", exportImg.EncodeToJPG());
  16. AssetDatabase.Refresh();
  17. }
  18. /// <summary>
  19. /// 合并贴图
  20. /// </summary>
  21. /// <param name="texs">贴图</param>
  22. /// <param name="offsets">贴图位置,左下角(0,0)</param>
  23. /// <param name="size">自己贴图的大小</param>
  24. /// <returns></returns>
  25. static Texture2D Combine(Texture2D[] texs, ValueTuple<int, int>[] offsets, int size)
  26. {
  27. Texture2D @out = new Texture2D(size, size, TextureFormat.RGBA32, true);
  28. for (int i = 0; i < texs.Length; i++)
  29. {
  30. var tex = texs[i];
  31. var offest = offsets[i];
  32. var width = tex.width;
  33. var height = tex.height;
  34. RenderTexture tmp = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear);
  35. Graphics.Blit(tex, tmp);
  36. RenderTexture previous = RenderTexture.active;
  37. RenderTexture.active = tmp;
  38. Texture2D @new = new Texture2D(width, height);
  39. @new.ReadPixels(new Rect(0, 0, width, height), 0, 0);
  40. @new.Apply();
  41. @out.SetPixels(offest.Item1, offest.Item2, width, height, @new.GetPixels());
  42. RenderTexture.active = previous;
  43. RenderTexture.ReleaseTemporary(tmp);
  44. }
  45. return @out;
  46. }
  47. }

我自己4个贴图合成的一个贴图

2.读取合并图片中任意位置的图片(示例读取右上角)

  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.IO;
  4. public class Test1 : MonoBehaviour
  5. {
  6. private string path;
  7. private Texture2D combineTex;
  8. public RawImage completImg;
  9. public Image singleImg;
  10. // Start is called before the first frame update
  11. void Start()
  12. {
  13. path = Application.dataPath + "/CombineImages/out.jpg";
  14. if (!File.Exists(path))
  15. {
  16. print("路径不存在");
  17. return;
  18. }
  19. byte[] combineImgData= File.ReadAllBytes(path);
  20. combineTex= ByteToTex2d(combineImgData, 1024, 1024);
  21. CombineImg();
  22. SingleImg();
  23. }
  24. //合并之后的贴图
  25. private void CombineImg()
  26. {
  27. if(combineTex!=null)
  28. {
  29. completImg.texture = combineTex;
  30. }
  31. else
  32. {
  33. print("combineTex=null");
  34. }
  35. }
  36. //合并之后贴图中的单个贴图
  37. private void SingleImg()
  38. {
  39. //读取右上角的位置
  40. Sprite sp = Sprite.Create(combineTex, new Rect(512, 512, 512, 512), new Vector2(0.5f, 0.5f));
  41. singleImg.sprite = sp;
  42. //设置图片宽、高
  43. //singleImg.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(256, 256);
  44. }
  45. public static Texture2D ByteToTex2d(byte[] bytes, int w , int h )
  46. {
  47. Texture2D tex = new Texture2D(w, h);
  48. tex.LoadImage(bytes);
  49. return tex;
  50. }
  51. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/98084
推荐阅读
相关标签
  

闽ICP备14008679号