67 lines
2.2 KiB
C#
Raw Normal View History

2025-09-08 14:51:28 +08:00
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System;
using LitJson;
using UnityEngine.SceneManagement;
namespace FSM
{
#if UNITY_EDITOR
// <20>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Գ<EFBFBD><D4B3><EFBFBD><EBA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
[CustomPropertyDrawer(typeof(DropdownStringAttribute))]
public class DropdownStringDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// <20><>ȡ<EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
DropdownStringAttribute dropdownAttribute = (DropdownStringAttribute)attribute;
// <20><>ȡ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ȡѡ<C8A1><D1A1>
string[] options = ReadOptionsFromFile(dropdownAttribute.filePath);
// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int selectedIndex = Array.IndexOf(options, property.stringValue);
if (selectedIndex == -1)
{
selectedIndex = 0; // <20><><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>ҵ<EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEA3AC>Ĭ<EFBFBD><C4AC>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ѡ<EFBFBD><D1A1>
}
selectedIndex = EditorGUI.Popup(position, label.text, selectedIndex, options);
// <20><><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD>ֵ
if (selectedIndex >= 0)
{
property.stringValue = options[selectedIndex];
}
}
// <20><>ȡ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD>б<EFBFBD>
private string[] ReadOptionsFromFile(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
return null;
}
List<string> str = new();
string folder = "";
List<JsonData> data = JsonMapper.ToObject<List<JsonData>>(Resources.Load<TextAsset>(filePath).text);
for (int i = 0; i < data.Count; i++)
{
if (SceneManager.GetActiveScene().name == data[i]["scene"].ToString())
{
folder = data[i]["folder"].ToString();
}
}
List<JsonData> data2 = JsonMapper.ToObject<List<JsonData>>(Resources.Load<TextAsset>(folder + "/ExcelData/ExcelToJson/StateData").text);
for (int i = 0; i < data2.Count; i++)
{
if (!string.IsNullOrEmpty(data2[i]["state"].ToString()))
{
str.Add(data2[i]["state"].ToString());
}
}
return str.ToArray();
}
}
#endif
}