#if(UNITY_EDITOR) using System.Collections; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; public class MakeShotScript : MonoBehaviour { [SerializeField] public NPC npc; [SerializeField] public RenderTexture render_texture; public void button_function() { Texture2D tex2d = new Texture2D(256, 256); RenderTexture.active = render_texture; tex2d.ReadPixels(new Rect(0, 0, render_texture.width, render_texture.height), 0, 0); tex2d.Apply(); SaveSprite(tex2d); } void SaveSprite(Texture2D tex) { string assetPath = "Assets/Resources/Avatar/NPC/" + npc.Name+"_2.png"; Sprite sp = Sprite.Create( tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0, 0), 30); sp.name = "SpriteName"; //Debug.Log("Saved PPU: " + sp.pixelsPerUnit); // Saved PPU: 30 // Would prefer a Unity specific way to do this but AssetDatabase.CreateAsset wasn't working. // Due to it being a .png instead of .asset? File.WriteAllBytes(Application.dataPath + "/../" + assetPath, tex.EncodeToPNG()); AssetDatabase.Refresh(); AssetDatabase.AddObjectToAsset(sp, assetPath); AssetDatabase.SaveAssets(); sp = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Sprite)) as Sprite; //Debug.Log("Loaded PPU: " + sp.pixelsPerUnit); // Loaded PPU: 100 } } #endif