12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using Blue;
- using System.IO;
- using UnityEngine;
- using UnityEngine.UI;
- namespace GHZLangChao
- {
- /// <summary>
- /// RawImage 转 Sprite
- /// </summary>
- public interface IRawImageForSpriteUtility : IUtility
- {
- Sprite SwitchSprite(RawImage rawImage);
- Sprite PngToSprite(string fullPath, int x, int y);
- }
- public class RawImageForSpriteUtility : IRawImageForSpriteUtility
- {
- public Sprite SwitchSprite(RawImage rawImage)
- {
- SaveLocal(rawImage, "1", "0");
- // 获取RawImage的纹理
- Texture2D rawTexture = TextureToTexture2D(rawImage.texture);
- // 创建一个新的纹理,用于截图
- Texture2D screenshot = new Texture2D(rawTexture.width, rawTexture.height);
- // 截取RawImage的画面,并应用到新的纹理上
- screenshot.SetPixels(rawTexture.GetPixels());
- screenshot.Apply();
- // 将截图应用到Image组件上
- Sprite sprite = Sprite.Create(screenshot, new Rect(0, 0, screenshot.width, screenshot.height), new Vector2(0.5f, 0.5f));
- return sprite;
- }
- /// <summary>
- /// 运行模式下Texture转换成Texture2D
- /// </summary>
- /// <param name="texture"></param>
- /// <returns></returns>
- private Texture2D TextureToTexture2D(Texture texture)
- {
- Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
- RenderTexture currentRT = RenderTexture.active;
- RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
- Graphics.Blit(texture, renderTexture);
- RenderTexture.active = renderTexture;
- texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
- texture2D.Apply();
- RenderTexture.active = currentRT;
- RenderTexture.ReleaseTemporary(renderTexture);
- return texture2D;
- }
- /// <summary>
- /// 保存到本地
- /// </summary>
- /// <param name="rawImage"></param>
- /// <param name="Step">某个步骤</param>
- /// <param name="id">某个步骤的第几张图片</param>
- private void SaveLocal(RawImage rawImage, string Step, string id)
- {
- Texture2D texture2D = TextureToTexture2D(rawImage.texture);
- byte[] bytes = texture2D.EncodeToPNG();
- if (!Directory.Exists(Application.persistentDataPath + "/" + Step))
- Directory.CreateDirectory(Application.persistentDataPath + "/" + Step);
- string filename = Application.persistentDataPath + "/" + Step + "/" + id + ".png";
- if (File.Exists(filename))
- File.Delete(filename);
- Debug.LogError(Application.persistentDataPath);
- File.WriteAllBytes(filename, bytes);
- }
- /// <summary>
- /// IO方式 —— 将PNG转为Sprite精灵图片
- /// </summary>
- /// <param name="fullPath">读取的PNG图片全路径:路径+文件名+后缀</param>
- /// <param name="x">图片尺寸:宽</param>
- /// <param name="y">图片尺寸:高</param>
- /// <returns></returns>
- public Sprite PngToSprite(string fullPath, int x, int y)
- {
- using (FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read)) //自动双清
- {
- fs.Seek(0, SeekOrigin.Begin); //设定当前流的位置
- byte[] bytes = new byte[fs.Length]; //创建文件长度缓冲区
- fs.Read(bytes, 0, (int)fs.Length); //读取文件
- Texture2D texture = new Texture2D(x, y); //创建Texture
- texture.LoadImage(bytes);
- return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one / 2);
- }
- }
- }
- }
|