Hi,
Need you help guys… I broke my mind already trying to figure out the reason.
What I got is a ScriptableObject “Inventory” with only 1 field for other ScriptableObject files - List
This Inventory creates from asset → create menu.
I wrote a custom inspector for Inventory, where added “Update inventory” method to grab all items textures in specific project folder and create SetData files.
As a result many SetData files created and added to Inventory list field, but after AssetDatabase.Refresh() or Unity asset default update all SetData files were deleted excepting the last created.
here what I got in console:
Observed deletion of: Assets/_Project/Dynamic Assets/Inventory/Sets/set_06_0.asset
UnityEngine.Debug:Log(Object)
Google.PackageManager.LoggingController:Log(String)
Google.PackageManager.PackagePostprocessor:OnPostprocessAllAssets(String[], String[], String[], String[])
UnityEditor.AssetDatabase:Refresh()
ItemsSetData:Create(String) (at Assets/_Project/Scripts/ItemsSetData.cs:48)
InventoryDataInspector:BuildSetData(CustomFileInfo[]) (at Assets/_Project/Scripts/Editor/InventoryDataInspector.cs:104)
InventoryDataInspector:UpdateInventory() (at Assets/_Project/Scripts/Editor/InventoryDataInspector.cs:89)
InventoryDataInspector:OnGUI() (at Assets/_Project/Scripts/Editor/InventoryDataInspector.cs:46)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
I thought may be something wrong with a custom inspector script, and tried to move it to EditorWindow, but had the same result… Can’t understand, what is happening here.
Here are scripts:
InventoryData
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
#if UNITY_EDITOR
using UnityEditor;
#endif
[CreateAssetMenu(fileName = "Inventory", menuName = "Tools/Inventory/Create inventory", order = 1)]
[Serializable]
public class InventoryData : ScriptableObject {
public List<ItemsSetData> sets = new List<ItemsSetData>();
}
InventoryDataInspector
using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
[CustomEditor(typeof(InventoryData))]
[CanEditMultipleObjects]
public class InventoryDataInspector : Editor {
static string pathToInventoryFolder = "Assets/_Project/Static Assets/Textures/Inventory";
static string pathToDataAssets = "Assets/_Project/Dynamic Assets/Inventory";
static string pathToProject;
static InventoryData t;
public override void OnInspectorGUI() {
t = target as InventoryData;
EditorGUILayout.LabelField("This is TestEditor");
base.OnInspectorGUI();
GUILayout.Space(20);
EditorGUILayout.LabelField("Path to inventory image assets");
pathToInventoryFolder = GUILayout.TextField(pathToInventoryFolder);
EditorGUILayout.LabelField("Path to data assets");
pathToDataAssets = GUILayout.TextField(pathToDataAssets);
GUILayout.Label("Update in editor");
if (GUILayout.Button("Update inventory by path")){
UpdateInventory();
}
EditorUtility.SetDirty(t);
}
private struct CustomFileInfo{
public string fullSetName;
public string setNumericName;
public string setName;
public string type;
public string side;
public FileInfo fileInfo;
public string[] splittedName;
}
private static void UpdateInventory(){
pathToProject = Application.dataPath.Substring(0, Application.dataPath.IndexOf("Assets") - 1);
DirectoryInfo info = new DirectoryInfo(Path.Combine(pathToProject, pathToInventoryFolder));
FileInfo[] filesInfo = info.GetFiles().Where(name => name.Extension != (".meta")).ToArray();
CustomFileInfo[] infos = new CustomFileInfo[filesInfo.Length];
for (int i = 0; i < infos.Length; i++) {
infos[i].fileInfo = filesInfo[i];
string[] arr = infos[i].splittedName = filesInfo[i].Name.Split("_"[0]);
infos[i].setName = arr[1];
infos[i].setNumericName = arr[1]+"_"+arr[2];
infos[i].fullSetName = arr[0]+"_"+arr[1]+"_"+arr[2];
infos[i].type = arr[3];
if (infos[i].type == "chest"){
infos[i].type = "armor";
}
infos[i].side = arr[4].Substring(0, arr[4].IndexOf(filesInfo[i].Extension));
}
List<CustomFileInfo[]> sortedBySets = infos.GroupBy(x => x.fullSetName).Select(x => x.ToArray()).ToList();
t.sets.Clear();
for (int i = 0; i < sortedBySets.Count; i++) {
t.sets.Add(BuildSetData(sortedBySets[i]));
}
}
private static ItemsSetData BuildSetData(CustomFileInfo[] infos){
string subPath = string.Format("Sets/{0}.asset", infos[0].fullSetName);
string setsFolderPath = Path.Combine(pathToProject, Path.Combine(pathToDataAssets, "Sets"));
if (Directory.Exists(setsFolderPath)){
Directory.Delete(setsFolderPath, true);
}
Directory.CreateDirectory(setsFolderPath);
// relative path to asset folder
ItemsSetData setData = ItemsSetData.Create(Path.Combine(pathToDataAssets, subPath));
string[] itemTypes = System.Enum.GetNames (typeof(ItemType));
// give a name like "01_0"
setData.setName = infos[0].setName;
for (int k = 0; k < infos.Length; k++) {
CustomFileInfo info = infos[k];
for (int j = 0; j < itemTypes.Length; j++) {
ItemType enumType = (ItemType)System.Enum.Parse( typeof( ItemType ), itemTypes[j] );
// exception for pet
if ((enumType == ItemType.Pet && info.type.ToLower() == ItemType.Pet.ToString().ToLower())
|| (enumType != ItemType.Pet && info.type.Substring(0,1).ToLower() == itemTypes[j].Substring(0, 1).ToLower())){
ItemsSetData.ItemGroup itemGroup = setData.items.Find(x=>x.type == enumType);
if (itemGroup == null){
itemGroup = new ItemsSetData.ItemGroup();
setData.items.Add(itemGroup);
itemGroup.type = enumType;
itemGroup.shortCode = info.type.Substring(0,1)+"_"+info.setName;
}
string path = Path.Combine(pathToInventoryFolder, info.fileInfo.Name);
if (info.side == "back"){
itemGroup.backTexture = (Texture2D)AssetDatabase.LoadAssetAtPath<Texture2D>(path) as Texture2D;
}
else if (info.side == "front"){
itemGroup.frontTexture = (Texture2D)AssetDatabase.LoadAssetAtPath<Texture2D>(path) as Texture2D;
}
break;
}
}
}
return setData;
}
ItemsSetData
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GameSparks.Core;
using System.Linq;
using System;
#if UNITY_EDITOR
using UnityEditor;
#endif
[CreateAssetMenu (fileName = "ItemsSetData", menuName = "Tools/Inventory/Create items set", order = 1)]
[Serializable]
public class ItemsSetData : ScriptableObject
{
[Serializable]
public class ItemGroup
{
public string shortCode;
public ItemType type;
public Texture2D frontTexture;
public Texture2D backTexture;
}
public string setName;
public List<ItemGroup> items = new List<ItemGroup> ();
#if UNITY_EDITOR
[MenuItem ("Assets/Create/Inventory Item List")]
#endif
public static ItemsSetData Create (string path = "Assets/ItemsSetData.asset")
{
ItemsSetData asset = ScriptableObject.CreateInstance<ItemsSetData> ();
AssetDatabase.CreateAsset (asset, path);
AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
return asset;
}
}
Any ideas, what is wrong?