ItemDatabaseORIG.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using UnityEditor;
  7. using UnityEditor.UIElements;
  8. using UnityEngine;
  9. using UnityEngine.UIElements;
  10. public class ItemDatabaseORIG : EditorWindow {
  11. private static List<ItemORIG> m_ItemDatabase = new List<ItemORIG>();
  12. private VisualElement m_ItemsTab;
  13. private static VisualTreeAsset m_ItemRowTemplate;
  14. private ScrollView m_DetailSection;
  15. private ListView m_ItemListView;
  16. private Sprite m_DefaultItemIcon;
  17. private ItemORIG m_activeItem;
  18. private VisualElement m_LargeDisplayIcon;
  19. private int m_ItemHeight = 60;
  20. /* [MenuItem("WUG/Item Database")]
  21. public static void Init() {
  22. ItemDatabaseORIG wnd = GetWindow<ItemDatabaseORIG>();
  23. wnd.titleContent = new GUIContent("Item Database");
  24. Vector2 size = new Vector2(1000, 475);
  25. wnd.minSize = size;
  26. wnd.maxSize = size;
  27. }
  28. */
  29. public void CreateGUI() {
  30. // Import the UXML Window
  31. var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/WUG/Editor/ItemDatabase.uxml");
  32. VisualElement rootFromUXML = visualTree.Instantiate();
  33. rootVisualElement.Add(rootFromUXML);
  34. // Import the stylesheet
  35. var styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/WUG/Editor/ItemDatabase.uss");
  36. rootVisualElement.styleSheets.Add(styleSheet);
  37. //Import the ListView Item Template
  38. m_ItemRowTemplate = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/WUG/Editor/ItemRowTemplate.uxml");
  39. m_DefaultItemIcon = (Sprite)AssetDatabase.LoadAssetAtPath("Assets/WUG/Sprites/UnknownIcon.png", typeof(Sprite));
  40. //Get references for later
  41. m_DetailSection = rootVisualElement.Q<ScrollView>("ScrollView_Details");
  42. m_LargeDisplayIcon = m_DetailSection.Q<VisualElement>("Icon");
  43. //Load all existing item assets
  44. LoadAllItems();
  45. //Populate the listview
  46. m_ItemsTab = rootVisualElement.Q<VisualElement>("ItemsTab");
  47. GenerateListView();
  48. //Hook up button click events
  49. rootVisualElement.Q<Button>("Btn_AddItem").clicked += AddItem_OnClick;
  50. rootVisualElement.Q<Button>("Btn_DeleteItem").clicked += DeleteItem_OnClick;
  51. //Register Value Changed Callbacks for new items added to the ListView
  52. m_DetailSection.Q<TextField>("ItemName").RegisterValueChangedCallback(evt => {
  53. m_activeItem.FriendlyName = evt.newValue;
  54. m_ItemListView.Rebuild();
  55. });
  56. m_DetailSection.Q<ObjectField>("IconPicker").RegisterValueChangedCallback(evt => {
  57. Sprite newSprite = evt.newValue as Sprite;
  58. m_activeItem.Icon = newSprite == null ? m_DefaultItemIcon : newSprite;
  59. m_LargeDisplayIcon.style.backgroundImage = newSprite == null ? m_DefaultItemIcon.texture : newSprite.texture;
  60. m_ItemListView.Rebuild();
  61. });
  62. }
  63. /// <summary>
  64. /// Delete the active Item asset from the Asset/Data folder
  65. /// </summary>
  66. private void DeleteItem_OnClick() {
  67. //Get the path of the fie and delete it through AssetDatabase
  68. string path = AssetDatabase.GetAssetPath(m_activeItem);
  69. AssetDatabase.DeleteAsset(path);
  70. //Purge the reference from the list and refresh the ListView
  71. m_ItemDatabase.Remove(m_activeItem);
  72. m_ItemListView.Rebuild();
  73. //Nothing is selected, so hide the details section
  74. m_DetailSection.style.visibility = Visibility.Hidden;
  75. }
  76. /// <summary>
  77. /// Add a new Item asset to the Asset/Data folder
  78. /// </summary>
  79. private void AddItem_OnClick() {
  80. //Create an instance of the scriptable object
  81. ItemORIG newItem = CreateInstance<ItemORIG>();
  82. newItem.FriendlyName = $"New Item";
  83. newItem.Icon = m_DefaultItemIcon;
  84. //Create the asset
  85. AssetDatabase.CreateAsset(newItem, $"Assets/Data/{newItem.ID}.asset");
  86. //Add it to the item list
  87. m_ItemDatabase.Add(newItem);
  88. //Refresh the ListView so everything is redrawn again
  89. m_ItemListView.Rebuild();
  90. m_ItemListView.style.height = m_ItemDatabase.Count * m_ItemHeight + 5;
  91. m_ItemListView.SetSelection(m_ItemDatabase.Count - 1);
  92. }
  93. /// <summary>
  94. /// Look through all items located in Assets/Data and load them into memory
  95. /// </summary>
  96. private void LoadAllItems() {
  97. m_ItemDatabase.Clear();
  98. string[] allPaths = Directory.GetFiles("Assets/Data", "*.asset", SearchOption.AllDirectories);
  99. foreach (string path in allPaths) {
  100. string cleanedPath = path.Replace("\\", "/");
  101. m_ItemDatabase.Add((ItemORIG)AssetDatabase.LoadAssetAtPath(cleanedPath, typeof(ItemORIG)));
  102. }
  103. }
  104. /// <summary>
  105. /// Create the list view based on the asset data
  106. /// </summary>
  107. private void GenerateListView() {
  108. //Defining what each item will visually look like. In this case, the makeItem function is creating a clone of the ItemRowTemplate.
  109. Func<VisualElement> makeItem = () => m_ItemRowTemplate.CloneTree();
  110. //Define the binding of each individual Item that is created. Specifically,
  111. //it binds the Icon visual element to the scriptable object�s Icon property and the
  112. //Name label to the FriendlyName property.
  113. Action<VisualElement, int> bindItem = (e, i) => {
  114. e.Q<VisualElement>("Icon").style.backgroundImage = m_ItemDatabase[i] == null ? m_DefaultItemIcon.texture : m_ItemDatabase[i].Icon.texture;
  115. e.Q<Label>("Name").text = m_ItemDatabase[i].FriendlyName;
  116. };
  117. //Create the listview and set various properties
  118. m_ItemListView = new ListView(m_ItemDatabase, m_ItemHeight, makeItem, bindItem);
  119. m_ItemListView.selectionType = SelectionType.Single;
  120. m_ItemListView.style.height = m_ItemDatabase.Count * m_ItemHeight + 5;
  121. m_ItemsTab.Add(m_ItemListView);
  122. m_ItemListView.onSelectionChange += ListView_onSelectionChange;
  123. m_ItemListView.SetSelection(0);
  124. }
  125. /// <summary>
  126. ///
  127. /// </summary>
  128. /// <param name="selectedItems"></param>
  129. private void ListView_onSelectionChange(IEnumerable<object> selectedItems) {
  130. //Get the first item in the selectedItems list.
  131. //There will only ever be one because SelectionType is set to Single
  132. m_activeItem = (ItemORIG)selectedItems.First();
  133. //Create a new SerializedObject and bind the Details VE to it.
  134. //This cascades the binding to the children
  135. SerializedObject so = new SerializedObject(m_activeItem);
  136. m_DetailSection.Bind(so);
  137. //Set the icon if it exists
  138. if (m_activeItem.Icon != null) {
  139. m_LargeDisplayIcon.style.backgroundImage = m_activeItem.Icon.texture;
  140. }
  141. //Make sure the detail section is visible. This can turn off when you delete an item
  142. m_DetailSection.style.visibility = Visibility.Visible;
  143. }
  144. }