// CRUD ----------- void RenameAsset(ItemDataSO item, string newName) { Debug.Log("RenameAsset: " + item.data.itemID + " to: " + newName); var path = AssetDatabase.GetAssetPath(item); AssetDatabase.Refresh(); AssetDatabase.RenameAsset(path, newName); AssetDatabase.SaveAssets(); }
ItemDataSO GetSelectedItem() { var path = AssetDatabase.GUIDToAssetPath(assetGuids[index]); selected = AssetDatabase.LoadAssetAtPath <ItemDataSO>(path); if (selected == null) { path = AssetDatabase.GUIDToAssetPath(assetGuids[index]); selected = AssetDatabase.LoadAssetAtPath <ItemDataSO>(path); } return(selected); }
int GetItemIndex(ItemDataSO item) { var asset = AssetDatabase.GetAssetPath(item); var fileName = Path.GetFileName(asset); fileName = fileName.Replace(".asset", ""); for (int i = 0; i < assetGuids.Length; i++) { var assetPath = AssetDatabase.GUIDToAssetPath(assetGuids[i]); var assetName = Path.GetFileNameWithoutExtension(assetPath); assetName = assetName.Replace(".asset", ""); if (fileName == assetName) { return(i); } } return(0); }
// Main ----------- void OnGUI() { index = Mathf.Clamp(index, 0, assetGuids.Length - 1); // heading ----- if (assetGuids != null) { selected = GetSelectedItem(); var str = "Selected: " + selected.name + " " + (index + 1) + "/" + assetGuids.Length; EditorGUILayout.LabelField(str, EditorStyles.boldLabel); } else { EditorGUILayout.LabelField("Item:"); } // Change selected ----- using (var group = new EditorGUILayout.HorizontalScope(GUI.skin.box)) { if (GUILayout.Button("<<")) { index--; } if (GUILayout.Button(">>")) { index++; } if (index < 0) { index = assetGuids.Length - 1; } if (index >= assetGuids.Length) { index = 0; } selected = GetSelectedItem(); } // Add new ----------- GUILayout.Label("Add new item:", EditorStyles.boldLabel); using (var group = new EditorGUILayout.HorizontalScope(GUI.skin.box)) { var folder = GUILayout.TextField("data_items/"); newName = GUILayout.TextField(newName); if (GUILayout.Button("Add Item", EditorStyles.miniButtonLeft) && !string.IsNullOrEmpty(newName)) { string path = AssetDatabase.GenerateUniqueAssetPath("Assets/" + folder + newName + ".asset"); newName = string.Empty; var dir = Path.GetDirectoryName(path); if (AssetDatabase.IsValidFolder(dir)) { CreateNewItem(path); FindItemAssets(); selected = AssetDatabase.LoadAssetAtPath <ItemDataSO>(path); index = GetItemIndex(selected); } } } // Rename asset ------ GUILayout.Label("Rename item asset:", EditorStyles.boldLabel); using (var group = new EditorGUILayout.HorizontalScope(GUI.skin.box)) { newAssetName = GUILayout.TextField(newAssetName); if (GUILayout.Button("Rename Asset", EditorStyles.miniButtonLeft) && !string.IsNullOrEmpty(newAssetName)) { var item = GetSelectedItem(); RenameAsset(item, newAssetName); } } // Draw item ---- if (selected != null) { // Draw item editor / inspector DrawItemInspector(); // Delete selected asset button if (GUILayout.Button("DELETE SELECTED", GUILayout.Height(40))) { DeleteItemAsset(); FindItemAssets(); // set index index = Mathf.Clamp(--index, 0, assetGuids.Length - 1); } } }