using System.IO; using Sirenix.OdinInspector; using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif namespace DanieleMarotta.RiversongCodeShowcase { public class IconMaker : MonoBehaviour { #if UNITY_EDITOR [SerializeField] [PreviewField] private RenderTexture _iconTexture; [Button(ButtonSizes.Large)] [GUIColor("cyan")] public void MakeIcon() { if (!_iconTexture) return; var folderPath = "Assets/_Project/GameAssets/UI/Icons"; if (!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath); var fileName = $"Icon_{GUID.Generate()}.png"; RenderTexture.active = _iconTexture; var temp = new Texture2D(_iconTexture.width, _iconTexture.height, TextureFormat.RGBA32, false); temp.ReadPixels(new Rect(0, 0, _iconTexture.width, _iconTexture.height), 0, 0); temp.Apply(); var fullPath = Path.Combine(folderPath, fileName); File.WriteAllBytes(fullPath, temp.EncodeToPNG()); AssetDatabase.Refresh(); var importer = (TextureImporter)AssetImporter.GetAtPath(fullPath); importer.textureType = TextureImporterType.Sprite; importer.spriteImportMode = SpriteImportMode.Single; importer.maxTextureSize = 256; importer.mipmapEnabled = true; importer.SaveAndReimport(); } #endif } }