1
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using MuHua;
|
||||
|
||||
namespace MuHuaEditor {
|
||||
/// <summary>
|
||||
/// 装备 - 编辑窗口
|
||||
/// </summary>
|
||||
public class EquipmentWindow : ModuleUIEditorWindow {
|
||||
|
||||
public VisualTreeAsset ButtonTemplate = default;
|
||||
|
||||
private EquipmentType currentEquipmentType;
|
||||
private UIEquipmentEnum<EquipmentType> equipmentType;
|
||||
private UIEquipmentEnum<WeaponType> weaponType;
|
||||
private UIEquipmentEnum<ArmorType> armorType;
|
||||
private UIEquipmentEnum<AccessoryType> accessoryType;
|
||||
private UIEquipmentList equipmentList;
|
||||
|
||||
public VisualElement VEEquipmentType => Q<VisualElement>("EquipmentType");
|
||||
public VisualElement VEEquipmentExtendType => Q<VisualElement>("EquipmentExtendType");
|
||||
public VisualElement EquipmentList => Q<VisualElement>("EquipmentList");
|
||||
|
||||
[MenuItem("Window/UI Toolkit/EquipmentWindow")]
|
||||
public static void ShowWindow() {
|
||||
EquipmentWindow wnd = GetWindow<EquipmentWindow>();
|
||||
wnd.titleContent = new GUIContent("EquipmentWindow");
|
||||
}
|
||||
|
||||
public override void Initial() {
|
||||
equipmentType = new UIEquipmentEnum<EquipmentType>(VEEquipmentType, ButtonTemplate, SettingsEquipmentType);
|
||||
weaponType = new UIEquipmentEnum<WeaponType>(VEEquipmentExtendType, ButtonTemplate, (value) => SettingsEquipmentType(value.ToString()));
|
||||
armorType = new UIEquipmentEnum<ArmorType>(VEEquipmentExtendType, ButtonTemplate, (value) => SettingsEquipmentType(value.ToString()));
|
||||
accessoryType = new UIEquipmentEnum<AccessoryType>(VEEquipmentExtendType, ButtonTemplate, (value) => SettingsEquipmentType(value.ToString()));
|
||||
equipmentList = new UIEquipmentList(EquipmentList, rootVisualElement, ButtonTemplate, (value) => Debug.Log(value));
|
||||
AddControl(equipmentList);
|
||||
|
||||
equipmentType.Initial();
|
||||
}
|
||||
public override void OnDestroy() {
|
||||
base.OnDestroy();
|
||||
equipmentType.Release();
|
||||
weaponType.Release();
|
||||
armorType.Release();
|
||||
accessoryType.Release();
|
||||
}
|
||||
|
||||
/// <summary> 设置装备类型 </summary>
|
||||
private void SettingsEquipmentType(EquipmentType type) {
|
||||
currentEquipmentType = type;
|
||||
if (type == EquipmentType.武器) { weaponType.Initial(); }
|
||||
if (type == EquipmentType.护甲) { armorType.Initial(); }
|
||||
if (type == EquipmentType.饰品) { accessoryType.Initial(); }
|
||||
}
|
||||
/// <summary> 设置装备类型 </summary>
|
||||
private void SettingsEquipmentType(string type) {
|
||||
string path = $"Assets/AssetsEquipment/ScriptableObject/{currentEquipmentType}/{type}";
|
||||
EnsureDirectoryExists(path);
|
||||
List<EquipmentConst> equipments = FindConstAssets<EquipmentConst>(path);
|
||||
equipmentList.Initial(equipments);
|
||||
}
|
||||
|
||||
#region 编辑器工具
|
||||
/// <summary> 查找指定文件夹下的所有 T 类型的 ScriptableObject 资源 </summary>
|
||||
public static List<T> FindConstAssets<T>(string folderPath) where T : ScriptableObject {
|
||||
List<T> result = new List<T>();
|
||||
string filter = $"t:{typeof(T).Name}";
|
||||
string[] guids = AssetDatabase.FindAssets(filter, new[] { folderPath });
|
||||
foreach (string guid in guids) {
|
||||
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
|
||||
T asset = AssetDatabase.LoadAssetAtPath<T>(assetPath);
|
||||
if (asset != null) { result.Add(asset); }
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/// <summary> 确保目录存在 </summary>
|
||||
public static void EnsureDirectoryExists(string path) {
|
||||
if (!Directory.Exists(path)) { Directory.CreateDirectory(path); }
|
||||
}
|
||||
#endregion
|
||||
|
||||
private void CreateConstWeapon() {
|
||||
// 创建一个新的 ConstWeapon 资源
|
||||
var asset = ScriptableObject.CreateInstance<WeaponConst>();
|
||||
string path = EditorUtility.SaveFilePanelInProject(
|
||||
"保存 ConstWeapon",
|
||||
"NewConstWeapon.asset",
|
||||
"asset",
|
||||
"请选择保存 ConstWeapon 的路径"
|
||||
);
|
||||
if (!string.IsNullOrEmpty(path)) {
|
||||
AssetDatabase.CreateAsset(asset, path);
|
||||
AssetDatabase.SaveAssets();
|
||||
EditorUtility.FocusProjectWindow();
|
||||
Selection.activeObject = asset;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ec99af1e55d08254f8089eba7fa65bcd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences:
|
||||
- m_ViewDataDictionary: {instanceID: 0}
|
||||
- VisualTreeAsset: {fileID: 9197481963319205126, guid: 866ca35095f42154d97c957787b91a83, type: 3}
|
||||
- ButtonTemplate: {fileID: 9197481963319205126, guid: 72d39f3bc55f0a94196e83e8bfea1857, type: 3}
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fe6d086cc2b6beb4e8d9dfe9339270f5
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using MuHua;
|
||||
|
||||
namespace MuHuaEditor {
|
||||
/// <summary>
|
||||
/// 装备枚举
|
||||
/// </summary>
|
||||
public class UIEquipmentEnum<T> : ModuleUIPanel where T : Enum {
|
||||
|
||||
public Action<T> callback;
|
||||
public ModuleUIItems<UIItem, T> items;
|
||||
|
||||
public UIEquipmentEnum(VisualElement element, VisualTreeAsset templateAsset, Action<T> callback) : base(element) {
|
||||
this.callback = callback;
|
||||
items = new ModuleUIItems<UIItem, T>(element, templateAsset,
|
||||
(data, element) => new UIItem(data, element, this));
|
||||
}
|
||||
/// <summary> 释放资源 </summary>
|
||||
public void Release() => items.Release();
|
||||
|
||||
/// <summary> 初始化 </summary>
|
||||
public void Initial() {
|
||||
items.Create(EnumToList());
|
||||
items.SelectFirst();
|
||||
}
|
||||
/// <summary> 设置值 </summary>
|
||||
public void Settings(T value) => callback?.Invoke(value);
|
||||
|
||||
private List<T> EnumToList() {
|
||||
var list = new List<T>();
|
||||
foreach (var name in Enum.GetValues(typeof(T))) { list.Add((T)name); }
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary> UI项 </summary>
|
||||
public class UIItem : ModuleUIItem<T> {
|
||||
public readonly UIEquipmentEnum<T> parent;
|
||||
|
||||
public Button Button => Q<Button>("Button");
|
||||
|
||||
public UIItem(T value, VisualElement element, UIEquipmentEnum<T> parent) : base(value, element) {
|
||||
this.parent = parent;
|
||||
Button.text = value.ToString();
|
||||
Button.clicked += Select;
|
||||
}
|
||||
public override void DefaultState() {
|
||||
Button.EnableInClassList("button-s", false);
|
||||
}
|
||||
public override void SelectState() {
|
||||
parent.Settings(value);
|
||||
Button.EnableInClassList("button-s", true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1a27a9357cd2dfb46b03783e745821ff
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using MuHua;
|
||||
|
||||
namespace MuHuaEditor {
|
||||
/// <summary>
|
||||
/// 装备 - UI列表
|
||||
/// </summary>
|
||||
public class UIEquipmentList : ModuleUIPanel, UIControl {
|
||||
|
||||
public Action<EquipmentConst> callback;
|
||||
public UIScrollViewListV<UIItem, EquipmentConst> items;
|
||||
|
||||
public VisualElement ScrollView => Q<VisualElement>("ScrollView");
|
||||
|
||||
public UIEquipmentList(VisualElement element, VisualElement canvas, VisualTreeAsset templateAsset, Action<EquipmentConst> callback) : base(element) {
|
||||
this.callback = callback;
|
||||
items = new UIScrollViewListV<UIItem, EquipmentConst>(ScrollView, canvas, templateAsset,
|
||||
(data, element) => new UIItem(data, element, this));
|
||||
}
|
||||
|
||||
public void Update() => items.Update();
|
||||
|
||||
public void Dispose() => items.Dispose();
|
||||
|
||||
/// <summary> 初始化 </summary>
|
||||
public void Initial(List<EquipmentConst> equipments) {
|
||||
items.Create(equipments);
|
||||
items.SelectFirst();
|
||||
}
|
||||
|
||||
/// <summary> 设置值 </summary>
|
||||
public void Settings(EquipmentConst value) => callback?.Invoke(value);
|
||||
|
||||
/// <summary> UI项 </summary>
|
||||
public class UIItem : ModuleUIItem<EquipmentConst> {
|
||||
public readonly UIEquipmentList parent;
|
||||
|
||||
public Button Button => Q<Button>("Button");
|
||||
|
||||
public UIItem(EquipmentConst value, VisualElement element, UIEquipmentList parent) : base(value, element) {
|
||||
this.parent = parent;
|
||||
Button.text = value.name;
|
||||
Button.clicked += Select;
|
||||
}
|
||||
public override void DefaultState() {
|
||||
Button.EnableInClassList("button-s", false);
|
||||
}
|
||||
public override void SelectState() {
|
||||
parent.Settings(value);
|
||||
Button.EnableInClassList("button-s", true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ea8d2198def9909448f54d619cfc65aa
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using UnityEditor;
|
||||
|
||||
namespace MuHuaEditor {
|
||||
/// <summary>
|
||||
/// 武器 - 自定义编辑器
|
||||
/// </summary>
|
||||
[CustomEditor(typeof(WeaponConst))]
|
||||
public class WeaponConstEditor : Editor {
|
||||
public VisualTreeAsset ButtonTemplate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b2c69e4ee377a7f48934adc2c8d6359f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user