1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using Blue;
- using UnityEngine;
- using UnityEngine.UI;
- namespace GHZLangChao
- {
-
-
-
- public interface IRawImageForSpriteUtility : IUtility
- {
- Sprite SwitchSprite(RawImage rawImage);
- }
- public class RawImageForSpriteUtility : IRawImageForSpriteUtility
- {
- public Sprite SwitchSprite(RawImage rawImage)
- {
-
- Texture2D rawTexture = TextureToTexture2D(rawImage.texture);
-
- Texture2D screenshot = new Texture2D(rawTexture.width, rawTexture.height);
-
- screenshot.SetPixels(rawTexture.GetPixels());
- screenshot.Apply();
-
- Sprite sprite = Sprite.Create(screenshot, new Rect(0, 0, screenshot.width, screenshot.height), new Vector2(0.5f, 0.5f));
- return sprite;
- }
-
-
-
-
-
- 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;
- }
- }
- }
|