ItemPropertyDrawer.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace HQFPSWeapons
  4. {
  5. [CustomPropertyDrawer(typeof(ItemProperty.Value))]
  6. public class ItemPropertyDrawer : PropertyDrawer
  7. {
  8. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  9. {
  10. position = EditorGUI.IndentedRect(position);
  11. position.x -= 16f
  12. ;
  13. var name = property.FindPropertyRelative("m_Name");
  14. float initialX = position.x;
  15. // Source label
  16. position.width = 64f;
  17. position.height = 16f;
  18. UnityEngine.GUI.Label(position, "Property: ");
  19. // Source popup
  20. var allProperties = ItemDatabase.Default.GetAllPropertyNames();
  21. if(allProperties.Length == 0)
  22. return;
  23. position.x = position.xMax;
  24. position.width = 128f;
  25. int selectedIndex = GetStringIndex(name.stringValue, allProperties);
  26. selectedIndex = EditorGUI.Popup(position, selectedIndex, allProperties);
  27. name.stringValue = allProperties[selectedIndex];
  28. // Value label
  29. position.x = initialX;
  30. position.width = 64f;
  31. position.y = position.yMax + 4f;
  32. UnityEngine.GUI.Label(position, "Value: ");
  33. // Show the correct value based on the selected type
  34. position.x = position.xMax;
  35. DrawFloatProperty(position, property.FindPropertyRelative("m_Float"));
  36. }
  37. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  38. {
  39. return 36f;
  40. }
  41. public static int GetStringIndex(string str, string[] strings)
  42. {
  43. for(int i = 0;i < strings.Length;i ++)
  44. if(strings[i] == str)
  45. return i;
  46. return 0;
  47. }
  48. private static void DrawFloatProperty(Rect position, SerializedProperty property)
  49. {
  50. var current = property.FindPropertyRelative("m_Current");
  51. var defaultVal = property.FindPropertyRelative("m_Default");
  52. current.floatValue = EditorGUI.FloatField(position, current.floatValue);
  53. defaultVal.floatValue = current.floatValue;
  54. }
  55. }
  56. }