IRawImageForSpriteUtility.cs 4.0 KB

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