修改框架
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8a4ee71d02fa8cd40aceb7f62a6efd89
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,11 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 装备 - 物品
|
||||
/// </summary>
|
||||
public class DataEquipment : DataItem {
|
||||
/// <summary> 装备类型 (类型1/类型2) </summary>
|
||||
public string type;
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 材料 - 物品
|
||||
/// </summary>
|
||||
public class DataMaterial : DataItem {
|
||||
/// <summary> 最大堆叠数 </summary>
|
||||
public int maxStack;
|
||||
|
||||
public override int MaxStack => maxStack;
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 装备栏
|
||||
/// </summary>
|
||||
public class Equipment {
|
||||
/// <summary> 插槽字典 </summary>
|
||||
public Dictionary<string, EquipmentSlot> dictionary = new Dictionary<string, EquipmentSlot>();
|
||||
|
||||
/// <summary> 索引器 </summary>
|
||||
public EquipmentSlot this[string key] => dictionary[key];
|
||||
|
||||
public bool ContainsKey(string key) => dictionary.ContainsKey(key);
|
||||
/// <summary> 添加插槽 </summary>
|
||||
public void AddSlot(EquipmentSlot slot) => dictionary.Add(slot.name, slot);
|
||||
}
|
||||
/// <summary>
|
||||
/// 插槽类型
|
||||
/// </summary>
|
||||
public enum SlotType {
|
||||
库存, 主手, 副手, 上衣, 头盔, 手套, 腰带, 鞋子, 项链, 戒指1, 戒指2, 手镯1, 手镯2
|
||||
}
|
||||
/// <summary>
|
||||
/// 装备插槽
|
||||
/// </summary>
|
||||
public abstract class EquipmentSlot {
|
||||
/// <summary> 名字 </summary>
|
||||
public string name;
|
||||
/// <summary> 物品 </summary>
|
||||
public DataEquipment item;
|
||||
|
||||
public EquipmentSlot(SlotType slot) => name = slot.ToString();
|
||||
|
||||
/// <summary> 设置 </summary>
|
||||
public void Settings(DataItem item, int count) {
|
||||
if (item is DataEquipment equipment) { this.item = equipment; }
|
||||
else { this.item = null; }
|
||||
}
|
||||
public abstract bool Verify(DataEquipment equipment);
|
||||
}
|
||||
/// <summary>
|
||||
/// 武器 - 装备插槽
|
||||
/// </summary>
|
||||
public class WeaponSlot : EquipmentSlot {
|
||||
/// <summary> 副手插槽 </summary>
|
||||
public DeputySlot deputy;
|
||||
|
||||
public WeaponSlot(SlotType slot) : base(slot) { }
|
||||
|
||||
public override bool Verify(DataEquipment equipment) {
|
||||
string[] type = equipment.type.Split("/");
|
||||
if (type[0] == WeaponType.单手.ToString()) { return true; }
|
||||
if (type[0] == WeaponType.双手.ToString()) { return true; }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 副手 - 装备插槽
|
||||
/// </summary>
|
||||
public class DeputySlot : EquipmentSlot {
|
||||
/// <summary> 主手插槽 </summary>
|
||||
public WeaponSlot weapon;
|
||||
|
||||
public DeputySlot(SlotType slot) : base(slot) { }
|
||||
|
||||
public override bool Verify(DataEquipment equipment) {
|
||||
string[] type = equipment.type.Split("/");
|
||||
if (type[0] == WeaponType.副手.ToString()) { return true; }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 护甲 - 装备插槽
|
||||
/// </summary>
|
||||
public class ArmorSlot : EquipmentSlot {
|
||||
/// <summary> 护甲类型 </summary>
|
||||
public string armorType;
|
||||
|
||||
public ArmorSlot(SlotType slot, ArmorType armor) : base(slot) => armorType = armor.ToString();
|
||||
|
||||
public override bool Verify(DataEquipment equipment) {
|
||||
string[] type = equipment.type.Split("/");
|
||||
if (type[0] == armorType) { return true; }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 饰品 - 装备插槽
|
||||
/// </summary>
|
||||
public class AccessorySlot : EquipmentSlot {
|
||||
/// <summary> 饰品类型 </summary>
|
||||
public string accessoryType;
|
||||
|
||||
public AccessorySlot(SlotType slot, AccessoryType accessory) : base(slot) => accessoryType = accessory.ToString();
|
||||
|
||||
public override bool Verify(DataEquipment equipment) {
|
||||
string[] type = equipment.type.Split("/");
|
||||
if (type[0] == accessoryType) { return true; }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 13afb37aea13dc94b97e8e85af77b5b8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -28,7 +28,7 @@ public class Inventory {
|
||||
public void ForEach(Action<InventorySlot> action) => slots.ForEach(action);
|
||||
|
||||
/// <summary> 添加物品 </summary>
|
||||
public void AddItem(DataItem item, int count) {
|
||||
public void AddItem(InventoryItem item, int count) {
|
||||
int remain = MergeToSlots(item, count);
|
||||
// 如果还有剩余,按照MaxStack分批放入空位
|
||||
if (remain > 0) { AddToSlots(item, remain, true); }
|
||||
@@ -36,13 +36,13 @@ public class Inventory {
|
||||
}
|
||||
|
||||
/// <summary> 添加物品 </summary>
|
||||
public void AddToSlots(DataItem item, int count, bool isWhile) {
|
||||
public void AddToSlots(InventoryItem item, int count, bool isWhile) {
|
||||
(bool isComplete, int remain) = AddToSlots(item, count);
|
||||
if (!isComplete || remain == 0 || !isWhile) { return; }
|
||||
AddToSlots(item, remain, isWhile);
|
||||
}
|
||||
/// <summary> 添加到插槽 bool = 是否添加成功,int = 余量 </summary>
|
||||
public (bool, int) AddToSlots(DataItem item, int count) {
|
||||
public (bool, int) AddToSlots(InventoryItem item, int count) {
|
||||
InventorySlot emptySlot = slots.FirstOrDefault(s => s.item == null);
|
||||
if (emptySlot == null) { return (false, count); }
|
||||
int addCount = Mathf.Min(count, item.MaxStack);
|
||||
@@ -51,7 +51,7 @@ public class Inventory {
|
||||
}
|
||||
|
||||
/// <summary> 合并到插槽 </summary>
|
||||
public int MergeToSlots(DataItem item, int count) {
|
||||
public int MergeToSlots(InventoryItem item, int count) {
|
||||
// 查询所有可合并的插槽
|
||||
List<InventorySlot> sameSlots = slots.Where(slot => slot.Same(item)).ToList();
|
||||
// 合并到已有插槽
|
||||
@@ -66,33 +66,3 @@ public class Inventory {
|
||||
count -= addCount;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 库存插槽
|
||||
/// </summary>
|
||||
public class InventorySlot {
|
||||
/// <summary> 数量 </summary>
|
||||
public int count;
|
||||
/// <summary> 物品 </summary>
|
||||
public DataItem item;
|
||||
|
||||
// /// <summary> 图片 </summary>
|
||||
public Sprite Sprite => item != null ? item.sprite : null;
|
||||
// /// <summary> 堆叠数量 </summary>
|
||||
public int MaxStack => item != null ? item.MaxStack : 0;
|
||||
|
||||
public InventorySlot() { }
|
||||
|
||||
public InventorySlot(DataItem item, int count) {
|
||||
this.item = item;
|
||||
this.count = count;
|
||||
}
|
||||
/// <summary> 设置 </summary>
|
||||
public void Settings(DataItem item, int count) {
|
||||
this.item = item;
|
||||
this.count = count;
|
||||
}
|
||||
/// <summary> 是否相同 </summary>
|
||||
public bool Same(DataItem obj) {
|
||||
return item != null && item.name == obj.name && count < MaxStack;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
/// <summary>
|
||||
/// 项目拖拽
|
||||
/// </summary>
|
||||
public interface InventoryDrag {
|
||||
/// <summary> 数量 </summary>
|
||||
public int Count { get; }
|
||||
/// <summary> 物品 </summary>
|
||||
public InventoryItem Item { get; }
|
||||
/// <summary> 锚点 </summary>
|
||||
public VisualElement Anchor { get; }
|
||||
|
||||
/// <summary> 取消拖拽 </summary>
|
||||
public void Cancel();
|
||||
/// <summary> 设置物品 </summary>
|
||||
public void Settings(InventoryItem item, int count);
|
||||
}
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 287f1e957d7061a42977427a98b178b5
|
||||
guid: cc8dab09130da0a43bcd57ddecde2332
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
+1
-1
@@ -5,7 +5,7 @@ using UnityEngine;
|
||||
/// <summary>
|
||||
/// 物品
|
||||
/// </summary>
|
||||
public class DataItem {
|
||||
public class InventoryItem {
|
||||
/// <summary> 名字 </summary>
|
||||
public string name;
|
||||
/// <summary> 图片 </summary>
|
||||
@@ -0,0 +1,34 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 库存插槽
|
||||
/// </summary>
|
||||
public class InventorySlot {
|
||||
/// <summary> 数量 </summary>
|
||||
public int count;
|
||||
/// <summary> 物品 </summary>
|
||||
public InventoryItem item;
|
||||
|
||||
// /// <summary> 图片 </summary>
|
||||
public Sprite Sprite => item != null ? item.sprite : null;
|
||||
// /// <summary> 堆叠数量 </summary>
|
||||
public int MaxStack => item != null ? item.MaxStack : 0;
|
||||
|
||||
public InventorySlot() { }
|
||||
|
||||
public InventorySlot(InventoryItem item, int count) {
|
||||
this.item = item;
|
||||
this.count = count;
|
||||
}
|
||||
/// <summary> 设置 </summary>
|
||||
public void Settings(InventoryItem item, int count) {
|
||||
this.item = item;
|
||||
this.count = count;
|
||||
}
|
||||
/// <summary> 是否相同 </summary>
|
||||
public bool Same(InventoryItem obj) {
|
||||
return item != null && item.name == obj.name && count < MaxStack;
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fc5511946a6586b42a9a026b078f6736
|
||||
guid: ddb28431ab55e034386cfe8f66c0d9f5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
@@ -1,35 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using MuHua;
|
||||
|
||||
/// <summary>
|
||||
/// 物品 - 管理器
|
||||
/// </summary>
|
||||
public class ManagerItem : ModuleSingle<ManagerItem> {
|
||||
/// <summary> 材料列表 </summary>
|
||||
public List<ConstMaterial> materials;
|
||||
/// <summary> 装备列表 </summary>
|
||||
public List<ConstEquipment> equipments;
|
||||
|
||||
protected override void Awake() => NoReplace(false);
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 物品奖励
|
||||
/// </summary>
|
||||
public class ItemReward {
|
||||
/// <summary> 物品列表 </summary>
|
||||
public List<DataItem> items;
|
||||
|
||||
public void Settings<T>(List<T> list) where T : ConstItem {
|
||||
items = new List<DataItem>();
|
||||
list.ForEach(obj => items.Add(obj.To()));
|
||||
}
|
||||
|
||||
public (DataItem, int) Get(int max = 1) {
|
||||
int count = Random.Range(1, max + 1);
|
||||
int index = Random.Range(0, items.Count);
|
||||
return (items[index], count);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9871787cca5a9e54fab450c7d3674d69
|
||||
guid: 48193b9c6d7c6af4088a00876ccaa1c3
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 681e43f413713dd46914cb6348821756
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,26 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 饰品 - 物品常量
|
||||
/// </summary>
|
||||
[CreateAssetMenu(menuName = "MuHua/物品系统/饰品")]
|
||||
public class ConstAccessory : ConstEquipment {
|
||||
/// <summary> 饰品类型 </summary>
|
||||
public AccessoryType type;
|
||||
|
||||
public override DataItem To() {
|
||||
DataEquipment item = new DataEquipment();
|
||||
item.name = name;
|
||||
item.sprite = sprite;
|
||||
item.type = type.ToString();
|
||||
return item;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 饰品类型
|
||||
/// </summary>
|
||||
public enum AccessoryType {
|
||||
戒指, 手镯, 项链
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fe5fcaac3685ff2408432feaa1d0999a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,26 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 护甲 - 物品常量
|
||||
/// </summary>
|
||||
[CreateAssetMenu(menuName = "MuHua/物品系统/护甲")]
|
||||
public class ConstArmor : ConstEquipment {
|
||||
/// <summary> 护甲类型 </summary>
|
||||
public ArmorType type;
|
||||
|
||||
public override DataItem To() {
|
||||
DataEquipment item = new DataEquipment();
|
||||
item.name = name;
|
||||
item.sprite = sprite;
|
||||
item.type = type.ToString();
|
||||
return item;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 护甲类型
|
||||
/// </summary>
|
||||
public enum ArmorType {
|
||||
上衣, 头盔, 手套, 腰带, 鞋子
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 33876f23eac24b14e8ba9cca9d545a4a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,10 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 装备 - 物品常量
|
||||
/// </summary>
|
||||
public class ConstEquipment : ConstItem {
|
||||
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5f4628602f47a4241837820971e174a3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,26 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 武器 - 物品常量
|
||||
/// </summary>
|
||||
[CreateAssetMenu(menuName = "MuHua/物品系统/武器")]
|
||||
public class ConstWeapon : ConstEquipment {
|
||||
/// <summary> 武器类型 </summary>
|
||||
public WeaponType type;
|
||||
|
||||
public override DataItem To() {
|
||||
DataEquipment item = new DataEquipment();
|
||||
item.name = name;
|
||||
item.sprite = sprite;
|
||||
item.type = type.ToString();
|
||||
return item;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 武器类型
|
||||
/// </summary>
|
||||
public enum WeaponType {
|
||||
单手, 副手, 双手
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 45818eb64c9b04441b20b29f0230edb7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+3
-3
@@ -6,13 +6,13 @@ using UnityEngine;
|
||||
/// 物品 - 常量
|
||||
/// </summary>
|
||||
[CreateAssetMenu(menuName = "MuHua/物品系统/物品")]
|
||||
public class ConstItem : ScriptableObject {
|
||||
public class ConstInventoryItem : ScriptableObject {
|
||||
/// <summary> 图片 </summary>
|
||||
public Sprite sprite;
|
||||
|
||||
/// <summary> 数据转换 </summary>
|
||||
public virtual DataItem To() {
|
||||
DataItem item = new DataItem();
|
||||
public virtual InventoryItem To() {
|
||||
InventoryItem item = new InventoryItem();
|
||||
item.name = name;
|
||||
item.sprite = sprite;
|
||||
return item;
|
||||
@@ -1,20 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 材料 - 物品常量
|
||||
/// </summary>
|
||||
[CreateAssetMenu(menuName = "MuHua/物品系统/材料")]
|
||||
public class ConstMaterial : ConstItem {
|
||||
/// <summary> 最大堆叠数 </summary>
|
||||
public int maxStack = 99;
|
||||
|
||||
public override DataItem To() {
|
||||
DataMaterial item = new DataMaterial();
|
||||
item.name = name;
|
||||
item.sprite = sprite;
|
||||
item.maxStack = maxStack;
|
||||
return item;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5592cd11563474d42a1f6805b66f7a1f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,110 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using MuHua;
|
||||
|
||||
/// <summary>
|
||||
/// UI装备栏
|
||||
/// </summary>
|
||||
public class UIEquipment : ModuleUIPanel {
|
||||
|
||||
public Equipment equipment;
|
||||
/// <summary> 插槽字典 </summary>
|
||||
public Dictionary<string, UIEquipmentSlot> dictionary = new Dictionary<string, UIEquipmentSlot>();
|
||||
|
||||
public UIEquipment(VisualElement element) : base(element) {
|
||||
InitialSlot(1, SlotType.主手);
|
||||
InitialSlot(2, SlotType.副手);
|
||||
InitialSlot(3, SlotType.上衣);
|
||||
InitialSlot(4, SlotType.项链);
|
||||
|
||||
InitialSlot(4, SlotType.戒指1);
|
||||
InitialSlot(4, SlotType.戒指2);
|
||||
InitialSlot(4, SlotType.手镯1);
|
||||
InitialSlot(4, SlotType.手镯2);
|
||||
|
||||
InitialSlot(4, SlotType.头盔);
|
||||
InitialSlot(4, SlotType.手套);
|
||||
InitialSlot(4, SlotType.腰带);
|
||||
InitialSlot(4, SlotType.鞋子);
|
||||
}
|
||||
public void Dispose() {
|
||||
|
||||
}
|
||||
|
||||
public void Settings(Equipment equipment) {
|
||||
if (this.equipment != null) { Dispose(); }
|
||||
this.equipment = equipment;
|
||||
if (equipment == null) { return; }
|
||||
foreach (var item in dictionary) {
|
||||
if (!equipment.ContainsKey(item.Key)) { continue; }
|
||||
EquipmentSlot slot = equipment[item.Key];
|
||||
item.Value.Settings(slot);
|
||||
}
|
||||
}
|
||||
|
||||
private void InitialSlot(int index, SlotType type) {
|
||||
VisualElement element = Q<VisualElement>($"EquipmentSlot{index}");
|
||||
UIEquipmentSlot uiSlot = new UIEquipmentSlot(element);
|
||||
dictionary.Add(type.ToString(), uiSlot);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// UI装备插槽
|
||||
/// </summary>
|
||||
public class UIEquipmentSlot : ModuleUIPanel, DragContainer {
|
||||
|
||||
public EquipmentSlot value;
|
||||
|
||||
public Label CountLabel => Q<Label>("Count");
|
||||
public VisualElement Image => Q<VisualElement>("Image");
|
||||
|
||||
public int Count => 1;
|
||||
public DataItem Item => value.item;
|
||||
public VisualElement Anchor => element;
|
||||
|
||||
public UIEquipmentSlot(VisualElement element) : base(element) {
|
||||
element.RegisterCallback<MouseDownEvent>(MouseDownEvent);
|
||||
element.RegisterCallback<MouseOverEvent>(MouseOverEvent);
|
||||
element.RegisterCallback<MouseLeaveEvent>(MouseLeaveEvent);
|
||||
element.RegisterCallback<ClickEvent>(ClickEvent);
|
||||
}
|
||||
private void MouseDownEvent(MouseDownEvent evt) {
|
||||
UIPopupManager.I.dragItem.Settings(this);
|
||||
Image.EnableInClassList("equipment-image-drag", true);
|
||||
}
|
||||
private void MouseOverEvent(MouseOverEvent evt) {
|
||||
if (!Verify()) { return; }
|
||||
UIPopupManager.I.dragItem.EnterContainer(this);
|
||||
}
|
||||
private void MouseLeaveEvent(MouseLeaveEvent evt) {
|
||||
UIPopupManager.I.dragItem.ExitContainer(this);
|
||||
}
|
||||
private void ClickEvent(ClickEvent evt) {
|
||||
// Debug.Log("sss");
|
||||
}
|
||||
|
||||
public void Settings(EquipmentSlot value) {
|
||||
this.value = value;
|
||||
UpdatePreview();
|
||||
}
|
||||
public void Settings(DataItem item, int count) {
|
||||
value.Settings(item, count);
|
||||
UpdatePreview();
|
||||
}
|
||||
public void Cancel() {
|
||||
Image.EnableInClassList("equipment-image-drag", false);
|
||||
}
|
||||
|
||||
private void UpdatePreview() {
|
||||
Sprite sprite = value == null || value.item == null ? null : value.item.sprite;
|
||||
Image.style.backgroundImage = new StyleBackground(sprite);
|
||||
}
|
||||
private bool Verify() {
|
||||
DragContainer container = UIPopupManager.I.dragItem.container;
|
||||
if (container == null) { return false; }
|
||||
if (container.Item is DataEquipment equipment) { return value.Verify(equipment); }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bad4c66685a426345b174ec593f67f56
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -36,50 +36,3 @@ public class UIInventory : ModuleUIPanel {
|
||||
items.Create(inventory.slots);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// UI库存插槽
|
||||
/// </summary>
|
||||
public class UIInventorySlot : ModuleUIItem<InventorySlot>, DragContainer {
|
||||
|
||||
public Label CountLabel => Q<Label>("Count");
|
||||
public VisualElement Image => Q<VisualElement>("Image");
|
||||
|
||||
public int Count => value.count;
|
||||
public DataItem Item => value.item;
|
||||
public VisualElement Anchor => element;
|
||||
|
||||
public UIInventorySlot(InventorySlot value, VisualElement element) : base(value, element) {
|
||||
UpdatePreview();
|
||||
element.RegisterCallback<MouseDownEvent>(MouseDownEvent);
|
||||
element.RegisterCallback<MouseOverEvent>(MouseOverEvent);
|
||||
element.RegisterCallback<MouseLeaveEvent>(MouseLeaveEvent);
|
||||
element.RegisterCallback<ClickEvent>(ClickEvent);
|
||||
}
|
||||
private void MouseDownEvent(MouseDownEvent evt) {
|
||||
UIPopupManager.I.dragItem.Settings(this);
|
||||
Image.EnableInClassList("inventory-image-drag", true);
|
||||
}
|
||||
private void MouseOverEvent(MouseOverEvent evt) {
|
||||
UIPopupManager.I.dragItem.EnterContainer(this);
|
||||
}
|
||||
private void MouseLeaveEvent(MouseLeaveEvent evt) {
|
||||
UIPopupManager.I.dragItem.ExitContainer(this);
|
||||
}
|
||||
private void ClickEvent(ClickEvent evt) {
|
||||
// Debug.Log("sss");
|
||||
}
|
||||
|
||||
public void Settings(DataItem item, int count) {
|
||||
value.Settings(item, count);
|
||||
UpdatePreview();
|
||||
}
|
||||
public void Cancel() {
|
||||
Image.EnableInClassList("inventory-image-drag", false);
|
||||
}
|
||||
|
||||
private void UpdatePreview() {
|
||||
CountLabel.text = value.count.ToString();
|
||||
CountLabel.EnableInClassList("inventory-count-hide", value.count <= 1);
|
||||
Image.style.backgroundImage = new StyleBackground(value.Sprite);
|
||||
}
|
||||
}
|
||||
+8
-24
@@ -7,7 +7,7 @@ using MuHua;
|
||||
/// <summary>
|
||||
/// UI拖拽物品
|
||||
/// </summary>
|
||||
public class UIDragItem : ModuleUIPanel, UIControl {
|
||||
public class UIInventoryDrag : ModuleUIPanel, UIControl {
|
||||
/// <summary> 画布 </summary>
|
||||
public VisualElement canvas;
|
||||
/// <summary> 是否启用 </summary>
|
||||
@@ -17,15 +17,15 @@ public class UIDragItem : ModuleUIPanel, UIControl {
|
||||
/// <summary> 偏移位置 </summary>
|
||||
public Vector2 offsetPosition;
|
||||
/// <summary> 物品容器 </summary>
|
||||
public DragContainer container;
|
||||
public InventoryDrag container;
|
||||
/// <summary> 目标容器 </summary>
|
||||
public DragContainer targetContainer;
|
||||
public InventoryDrag targetContainer;
|
||||
|
||||
public Label Count => Q<Label>("Count");
|
||||
public VisualElement Image => Q<VisualElement>("Image");
|
||||
public VisualElement Preview => Q<VisualElement>("Preview");
|
||||
|
||||
public UIDragItem(VisualElement element, VisualElement canvas) : base(element) {
|
||||
public UIInventoryDrag(VisualElement element, VisualElement canvas) : base(element) {
|
||||
this.canvas = canvas;
|
||||
ModuleUI.AddControl(this);
|
||||
canvas.RegisterCallback<MouseUpEvent>(MouseUpEvent);
|
||||
@@ -39,7 +39,7 @@ public class UIDragItem : ModuleUIPanel, UIControl {
|
||||
// 交换物品
|
||||
if (container == null || targetContainer == null) { return; }
|
||||
int count = targetContainer.Count;
|
||||
DataItem item = targetContainer.Item;
|
||||
InventoryItem item = targetContainer.Item;
|
||||
targetContainer.Settings(container.Item, container.Count);
|
||||
container.Settings(item, count);
|
||||
}
|
||||
@@ -57,7 +57,7 @@ public class UIDragItem : ModuleUIPanel, UIControl {
|
||||
}
|
||||
|
||||
/// <summary> 设置容器 </summary>
|
||||
public void Settings(DragContainer container) {
|
||||
public void Settings(InventoryDrag container) {
|
||||
this.container = container;
|
||||
|
||||
isEnable = container.Item != null;
|
||||
@@ -65,11 +65,11 @@ public class UIDragItem : ModuleUIPanel, UIControl {
|
||||
if (isEnable) { UpdatePreview(); }
|
||||
}
|
||||
/// <summary> 进入容器 </summary>
|
||||
public void EnterContainer(DragContainer container) {
|
||||
public void EnterContainer(InventoryDrag container) {
|
||||
targetContainer = container;
|
||||
}
|
||||
/// <summary> 退出容器 </summary>
|
||||
public void ExitContainer(DragContainer container) {
|
||||
public void ExitContainer(InventoryDrag container) {
|
||||
if (targetContainer == container) { targetContainer = null; }
|
||||
}
|
||||
|
||||
@@ -81,19 +81,3 @@ public class UIDragItem : ModuleUIPanel, UIControl {
|
||||
Image.style.backgroundImage = new StyleBackground(container.Item.sprite);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 项目拖拽
|
||||
/// </summary>
|
||||
public interface DragContainer {
|
||||
/// <summary> 数量 </summary>
|
||||
public int Count { get; }
|
||||
/// <summary> 物品 </summary>
|
||||
public DataItem Item { get; }
|
||||
/// <summary> 锚点 </summary>
|
||||
public VisualElement Anchor { get; }
|
||||
|
||||
/// <summary> 取消拖拽 </summary>
|
||||
public void Cancel();
|
||||
/// <summary> 设置物品 </summary>
|
||||
public void Settings(DataItem item, int count);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using MuHua;
|
||||
|
||||
/// <summary>
|
||||
/// UI库存插槽
|
||||
/// </summary>
|
||||
public class UIInventorySlot : ModuleUIItem<InventorySlot>, InventoryDrag {
|
||||
|
||||
public Label CountLabel => Q<Label>("Count");
|
||||
public VisualElement Image => Q<VisualElement>("Image");
|
||||
|
||||
public int Count => value.count;
|
||||
public InventoryItem Item => value.item;
|
||||
public VisualElement Anchor => element;
|
||||
|
||||
public UIInventorySlot(InventorySlot value, VisualElement element) : base(value, element) {
|
||||
UpdatePreview();
|
||||
element.RegisterCallback<MouseDownEvent>(MouseDownEvent);
|
||||
element.RegisterCallback<MouseOverEvent>(MouseOverEvent);
|
||||
element.RegisterCallback<MouseLeaveEvent>(MouseLeaveEvent);
|
||||
element.RegisterCallback<ClickEvent>(ClickEvent);
|
||||
}
|
||||
private void MouseDownEvent(MouseDownEvent evt) {
|
||||
UIPopupManager.I.inventoryDrag.Settings(this);
|
||||
Image.EnableInClassList("inventory-image-drag", true);
|
||||
}
|
||||
private void MouseOverEvent(MouseOverEvent evt) {
|
||||
UIPopupManager.I.inventoryDrag.EnterContainer(this);
|
||||
}
|
||||
private void MouseLeaveEvent(MouseLeaveEvent evt) {
|
||||
UIPopupManager.I.inventoryDrag.ExitContainer(this);
|
||||
}
|
||||
private void ClickEvent(ClickEvent evt) {
|
||||
// Debug.Log("sss");
|
||||
}
|
||||
|
||||
public void Settings(InventoryItem item, int count) {
|
||||
value.Settings(item, count);
|
||||
UpdatePreview();
|
||||
}
|
||||
public void Cancel() {
|
||||
Image.EnableInClassList("inventory-image-drag", false);
|
||||
}
|
||||
|
||||
private void UpdatePreview() {
|
||||
CountLabel.text = value.count.ToString();
|
||||
CountLabel.EnableInClassList("inventory-count-hide", value.count <= 1);
|
||||
Image.style.backgroundImage = new StyleBackground(value.Sprite);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3d078f2759ef3ef4bb65217dd9b2fb94
|
||||
guid: ef816982615753c4c9d3ebcf38d51598
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
Reference in New Issue
Block a user