DatabasePropertyDrawer.cs 1.1 KB

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