Documentation for the Unity C# Library
Loading...
Searching...
No Matches
ItemIdPropertyDrawer.cs
Go to the documentation of this file.
1#if UNITY_EDITOR
2using System.Linq;
3using UnityEditor;
4using UnityEngine;
5using UnityEngine.UIElements;
6
8{
9 [CustomPropertyDrawer(typeof(ItemIDAttribute))]
10 public class ItemIdPropertyDrawer : PropertyDrawer
11 {
12 private static string[] namesCache;
13
14 public override VisualElement CreatePropertyGUI(SerializedProperty property)
15 {
16 namesCache = ItemIDsSettings.GetOrCreateSettings().itemIds.Select(i => i.name).ToArray();
17
18 return base.CreatePropertyGUI(property);
19 }
20
21 public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
22 {
23 if (namesCache == null || namesCache.Length != ItemIDsSettings.GetOrCreateSettings().itemIds.Count)
24 {
25 namesCache = ItemIDsSettings.GetOrCreateSettings().itemIds.Select(i => i.name).ToArray();
26 }
27
28 property.intValue = ItemIDsSettings.GetOrCreateSettings()
29 .itemIds[EditorGUI.Popup(
30 rect,
31 label.text,
32 ItemIDsSettings.GetOrCreateSettings()
33 .itemIds.FindIndex(
34 i => i.id == property.intValue
35 ),
36 namesCache
37 )]
38 .id;
39 }
40 }
41}
42#endif