hey I have an itemSystem editor extension where i convert a texture2D retrieved with AssetPreview.GetAssetPreview to a png with EncodeToPNG() and then File.WriteAllBytes(…). strange thing is that the png is in my windows folder, but it doesn’t seem to updated my asset folder automatically to appear in unty? it is only there after I opened the windows folder or refresh?
so i added an AssetDataBase.Refresh(); to the code but still can’t get it to load as item.Icon in my instance that also gets created as an asset…
if this makes any sense
here’s my code:
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
public class ItemSystem : EditorWindow
{
[MenuItem ("MyGame/Add ItemSystem")]
static void Init ()
{
EditorWindow.GetWindow<ItemSystem>();
}
public enum ItemType
{
item
}
public ItemType typeToCreate = ItemType.item;
public string itemName;
public int itemValue;
public string description;
public GameObject itemObj;
public Texture2D icon;
public int amount;
int index;
ItemManager itemManager;
void OnEnable()
{
GameObject go=GameObject.Find("ItemManager");
if(go==null)
{
Debug.Log("ceating IM");
go=new GameObject();
go.AddComponent<ItemManager>();
go.name="ItemManager";
}
itemManager=go.GetComponent<ItemManager>()as ItemManager;
}
void OnGUI()
{
itemName = EditorGUILayout.TextField("Name : ", itemName);
itemValue = EditorGUILayout.IntField("Value : ", itemValue);
description = EditorGUILayout.TextField("Description : ", description);
amount = EditorGUILayout.IntField("Amount : ", amount);
itemObj = EditorGUILayout.ObjectField(itemObj,typeof(GameObject),true)as GameObject;
if(itemObj!=null)icon = EditorGUILayout.ObjectField(AssetPreview.GetAssetPreview(itemObj), typeof(Texture2D),true, GUILayout.MaxWidth(50), GUILayout.MaxHeight(50))as Texture2D;
typeToCreate= (ItemType)EditorGUILayout.EnumPopup(typeToCreate);
if(itemName != null itemObj != null)
{
switch(typeToCreate)
{
case ItemType.item:
if(GUILayout.Button("Create Item", GUILayout.Width(150)))
{
Item item = ScriptableObject.CreateInstance<Item>();
item.ItemName = itemName;
item.ItemValue = itemValue;
item.Description = description;
item.ItemObj = itemObj;
item.Amount = amount;
itemManager.itemList.Add(item);
index = itemManager.itemList.IndexOf(item );
item.ID = index;
byte[] bytes=icon.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/Icons/"+index+"_"+item.ItemName+"_icon.png", bytes);
AssetDatabase.Refresh();
item.Icon= AssetDatabase.LoadAssetAtPath(Application.dataPath +"/Icons/"+index+"_"+item.ItemName+"_icon.png", typeof(Texture2D))as Texture2D;
//icon = AssetDatabase.LoadAssetAtPath(Application.dataPath +"/Icons/"+index+"_"+item.ItemName+"_icon.png", typeof(Texture2D))as Texture2D;
//item.Icon=icon;
AssetDatabase.CreateAsset(item, "Assets/Items/"+item.ID+"_"+item.ItemName+"_item.prefab");
Create(index);
}
break;
}
}
}
void Create(int index)
{
GameObject clone = Instantiate(itemObj, new Vector3(Camera.main.transform.position.x, 0, Camera.main.transform.localPosition.z+2), itemObj.transform.rotation)as GameObject;
clone.name = itemName;
EditorUtility.SetDirty(itemManager);
}
}