DatabaseItemDrawer.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace HQFPSWeapons.Inventory
  4. {
  5. [CustomPropertyDrawer(typeof(DatabaseItem))]
  6. public class DatabaseItemDrawer : PropertyDrawer
  7. {
  8. private string[] m_AllItems;
  9. private string[] m_AllItemsFull;
  10. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  11. {
  12. if(property.propertyType != SerializedPropertyType.String)
  13. EditorGUI.HelpBox(position, "The Item attribute runs just on strings.", MessageType.Error);
  14. if(m_AllItems == null && ItemDatabase.Default != null)
  15. {
  16. m_AllItems = ItemDatabase.Default.GetAllItemNames().ToArray();
  17. m_AllItemsFull = ItemDatabase.Default.GetAllItemNamesFull().ToArray();
  18. }
  19. if(m_AllItems != null)
  20. property.stringValue = IndexToString(EditorGUI.Popup(position, label.text, StringToIndex(property.stringValue), m_AllItemsFull));
  21. }
  22. private int StringToIndex(string s)
  23. {
  24. for(int i = 0;i < m_AllItems.Length;i ++)
  25. {
  26. if(m_AllItems[i] == s)
  27. return i;
  28. }
  29. return 0;
  30. }
  31. private string IndexToString(int i)
  32. {
  33. return m_AllItems.Length > i ? m_AllItems[i] : "";
  34. }
  35. }
  36. }