IRawImageForSpriteUtility.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using Blue;
  2. using System.IO;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace GHZLangChao
  6. {
  7. /// <summary>
  8. /// RawImage 转 Sprite
  9. /// </summary>
  10. public interface IRawImageForSpriteUtility : IUtility
  11. {
  12. Sprite SwitchSprite(RawImage rawImage);
  13. Texture2D TextureToTexture2D(Texture texture);
  14. Sprite PngToSprite(string fullPath, int x, int y);
  15. }
  16. public class RawImageForSpriteUtility : IRawImageForSpriteUtility
  17. {
  18. public Sprite SwitchSprite(RawImage rawImage)
  19. {
  20. //SaveLocal(rawImage, "1", "0");
  21. // 获取RawImage的纹理
  22. Texture2D rawTexture = TextureToTexture2D(rawImage.texture);
  23. // 创建一个新的纹理,用于截图
  24. Texture2D screenshot = new Texture2D(rawTexture.width, rawTexture.height);
  25. // 截取RawImage的画面,并应用到新的纹理上
  26. screenshot.SetPixels(rawTexture.GetPixels());
  27. screenshot.Apply();
  28. // 将截图应用到Image组件上
  29. Sprite sprite = Sprite.Create(screenshot, new Rect(0, 0, screenshot.width, screenshot.height), new Vector2(0.5f, 0.5f));
  30. return sprite;
  31. }
  32. /// <summary>
  33. /// 运行模式下Texture转换成Texture2D
  34. /// </summary>
  35. /// <param name="texture"></param>
  36. /// <returns></returns>
  37. public Texture2D TextureToTexture2D(Texture texture)
  38. {
  39. Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
  40. RenderTexture currentRT = RenderTexture.active;
  41. RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
  42. Graphics.Blit(texture, renderTexture);
  43. RenderTexture.active = renderTexture;
  44. texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
  45. texture2D.Apply();
  46. RenderTexture.active = currentRT;
  47. RenderTexture.ReleaseTemporary(renderTexture);
  48. return texture2D;
  49. }
  50. /// <summary>
  51. /// 保存到本地
  52. /// </summary>
  53. /// <param name="rawImage"></param>
  54. /// <param name="Step">某个步骤</param>
  55. /// <param name="id">某个步骤的第几张图片</param>
  56. private void SaveLocal(RawImage rawImage, string Step, string id)
  57. {
  58. Texture2D texture2D = TextureToTexture2D(rawImage.texture);
  59. byte[] bytes = texture2D.EncodeToPNG();
  60. if (!Directory.Exists(Application.persistentDataPath + "/" + Step))
  61. Directory.CreateDirectory(Application.persistentDataPath + "/" + Step);
  62. string filename = Application.persistentDataPath + "/" + Step + "/" + id + ".png";
  63. if (File.Exists(filename))
  64. File.Delete(filename);
  65. Debug.LogError(Application.persistentDataPath);
  66. File.WriteAllBytes(filename, bytes);
  67. }
  68. /// <summary>
  69. /// IO方式 —— 将PNG转为Sprite精灵图片
  70. /// </summary>
  71. /// <param name="fullPath">读取的PNG图片全路径:路径+文件名+后缀</param>
  72. /// <param name="x">图片尺寸:宽</param>
  73. /// <param name="y">图片尺寸:高</param>
  74. /// <returns></returns>
  75. public Sprite PngToSprite(string fullPath, int x, int y)
  76. {
  77. using (FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read)) //自动双清
  78. {
  79. fs.Seek(0, SeekOrigin.Begin); //设定当前流的位置
  80. byte[] bytes = new byte[fs.Length]; //创建文件长度缓冲区
  81. fs.Read(bytes, 0, (int)fs.Length); //读取文件
  82. Texture2D texture = new Texture2D(x, y); //创建Texture
  83. texture.LoadImage(bytes);
  84. return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one / 2);
  85. }
  86. }
  87. }
  88. }