Skip to content

Instantly share code, notes, and snippets.

@usbharu
Created March 26, 2021 00:52
Show Gist options
  • Save usbharu/43cacff7776447d42e8ada7747c5841e to your computer and use it in GitHub Desktop.
Save usbharu/43cacff7776447d42e8ada7747c5841e to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class Item : MonoBehaviour
{
public string itemName;
[ContextMenuItem("set image", "SetImage")]
public Texture2D itemImage;
#if UNITY_EDITOR
protected void SetImage()
{
Texture2D texture2D = AssetPreview.GetAssetPreview(gameObject);
var bytes = texture2D.EncodeToPNG();
File.WriteAllBytes(Path.Combine(Directory.GetCurrentDirectory(), "Assets", "ItemImage", gameObject.name) + ".png",bytes);
AssetDatabase.Refresh();
Debug.Log(Path.Combine(Directory.GetCurrentDirectory(),"Assets","ItemImage"));
itemImage = AssetDatabase.LoadAssetAtPath(Path.Combine( "Assets","ItemImage", gameObject.name) +".png", typeof(Texture2D)) as Texture2D;
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment