2025-03-08 13:15:13 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using TMPro;
|
2025-05-19 11:49:19 +08:00
|
|
|
|
using XMLTool;
|
|
|
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
|
using System;
|
2025-03-08 13:15:13 +08:00
|
|
|
|
|
|
|
|
|
|
namespace QFramework.Example
|
|
|
|
|
|
{
|
|
|
|
|
|
public class UIBackPackData : UIPanelData
|
|
|
|
|
|
{
|
|
|
|
|
|
public List<string> devices;
|
|
|
|
|
|
public bool random = false;
|
|
|
|
|
|
public float scrollSpeed = 25;
|
|
|
|
|
|
public string position;
|
|
|
|
|
|
}
|
|
|
|
|
|
public partial class UIBackPack : UIPanel
|
|
|
|
|
|
{
|
|
|
|
|
|
ResLoader mResLoader;
|
2025-05-19 11:49:19 +08:00
|
|
|
|
Dictionary<Device, GameObject> dict = new Dictionary<Device, GameObject>();
|
2025-03-08 13:15:13 +08:00
|
|
|
|
protected override void OnInit(IUIData uiData = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
mData = uiData as UIBackPackData ?? new UIBackPackData();
|
|
|
|
|
|
mResLoader = ResLoader.Allocate();
|
|
|
|
|
|
TypeEventSystem.Global.Register<OnModuleQuit>(OnModuleQuit).UnRegisterWhenGameObjectDestroyed(gameObject);
|
|
|
|
|
|
// please add init code here
|
2025-05-19 11:49:19 +08:00
|
|
|
|
|
2025-03-08 13:15:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-19 11:49:19 +08:00
|
|
|
|
private void OnPickDeviceHandler(OnPickDevice device)
|
2025-03-08 13:15:13 +08:00
|
|
|
|
{
|
2025-05-19 11:49:19 +08:00
|
|
|
|
ItemFactory(device.device);
|
|
|
|
|
|
mResLoader.LoadAsync();
|
|
|
|
|
|
}
|
2025-03-08 13:15:13 +08:00
|
|
|
|
|
2025-05-19 11:49:19 +08:00
|
|
|
|
public void ItemFactory(Device item)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (dict.ContainsKey(item) == false)
|
2025-03-08 13:15:13 +08:00
|
|
|
|
{
|
|
|
|
|
|
GameObject obj = GameObject.Instantiate(ItemPrefab.gameObject, Content);
|
|
|
|
|
|
obj.name = item.Name;
|
|
|
|
|
|
obj.transform.Find("Name").GetComponent<TextMeshProUGUI>().text = item.Name;
|
|
|
|
|
|
Image icon = obj.transform.Find("IconBg/Icon").GetComponent<Image>();
|
|
|
|
|
|
var localImageUrl = Global.deviceIconsPath + item.Icon;
|
|
|
|
|
|
GameObject right = icon.transform.Find("Right").gameObject;
|
|
|
|
|
|
GameObject wrong = icon.transform.Find("Wrong").gameObject;
|
|
|
|
|
|
GameObject Selected = icon.transform.Find("Selected").gameObject;
|
|
|
|
|
|
mResLoader.Add2Load(localImageUrl.ToNetImageResName(),
|
|
|
|
|
|
(bool success, IRes res) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (success)
|
|
|
|
|
|
{
|
|
|
|
|
|
icon.sprite = Utility.GetSprite(res.Asset as Texture2D);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
Button btn = obj.GetComponent<Button>();
|
2025-05-19 11:49:19 +08:00
|
|
|
|
btn.onClick.AddListener(() => Drop(item.Name, obj));
|
|
|
|
|
|
obj.OnPointerUpEvent(OnClickUp);
|
|
|
|
|
|
dict.Add(item, obj);
|
|
|
|
|
|
}
|
|
|
|
|
|
int count = PlayerController.Instance.HasDevice(item.Name).count;
|
|
|
|
|
|
if (count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
dict[item].transform.Find("IconBg/Icon/Count").GetComponent<TextMeshProUGUI>().text = count.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
dict[item].transform.Find("IconBg/Icon/Count").GetComponent<TextMeshProUGUI>().text = string.Empty;
|
|
|
|
|
|
}
|
2025-03-08 13:15:13 +08:00
|
|
|
|
|
2025-05-19 11:49:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnClickUp(PointerEventData data)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (data.button)
|
|
|
|
|
|
{
|
|
|
|
|
|
case PointerEventData.InputButton.Left:
|
|
|
|
|
|
break;
|
|
|
|
|
|
case PointerEventData.InputButton.Right:
|
|
|
|
|
|
GameObject obj = data.pointerPress;
|
|
|
|
|
|
var device = DeviceController.Instance.GetDevice(obj.name);
|
|
|
|
|
|
UIRightMenuData uiData = new UIRightMenuData();
|
|
|
|
|
|
uiData.btns = device.rightMenu.items;
|
|
|
|
|
|
uiData.OnBtnClick = str =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (str.Equals("<22><><EFBFBD><EFBFBD>", System.StringComparison.Ordinal))
|
|
|
|
|
|
{
|
|
|
|
|
|
Drop(device.Name, obj);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
UIKit.OpenPanelAsync<UIRightMenu>(canvasLevel: UILevel.PopUI, uiData: uiData).ToAction().StartGlobal();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case PointerEventData.InputButton.Middle:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnOpen(IUIData uiData = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
mData = uiData as UIBackPackData ?? new UIBackPackData();
|
|
|
|
|
|
TypeEventSystem.Global.Register<OnPickDevice>(OnPickDeviceHandler);
|
|
|
|
|
|
Content.RemoveAllChildren();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var dictItem in PlayerController.Instance.hasDevicesDict)
|
|
|
|
|
|
{
|
|
|
|
|
|
var device = dictItem.Value.device;
|
|
|
|
|
|
var item = DeviceController.Instance.GetDevice(device.Name);
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError(device + ":û<><C3BB><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD>Ӧ<EFBFBD><D3A6>Device<63><65><EFBFBD><EFBFBD>");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
ItemFactory(item);
|
2025-03-08 13:15:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
mResLoader.LoadAsync();
|
|
|
|
|
|
Scroll.verticalNormalizedPosition = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (mData.position)
|
|
|
|
|
|
{
|
|
|
|
|
|
case "left":
|
|
|
|
|
|
// <20><>ê<EFBFBD><C3AA><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2>м䣨Middle-Right<68><74>
|
|
|
|
|
|
bg.rectTransform.anchorMin = new Vector2(0, 0.5f); // <20><><EFBFBD><EFBFBD>ê<EFBFBD>㣨<EFBFBD>Ҳࣩ
|
|
|
|
|
|
bg.rectTransform.anchorMax = new Vector2(0, 0.5f); // <20><><EFBFBD><EFBFBD>ê<EFBFBD>㣨<EFBFBD>Ҳࣩ
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>λ<EFBFBD><CEBB>ƫ<EFBFBD>ƹ<EFBFBD><C6B9>㣨<EFBFBD><E3A3A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ê<EFBFBD>㣩
|
|
|
|
|
|
bg.rectTransform.anchoredPosition = Vector2.zero;
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>Ϊ<EFBFBD>Ҳ<EFBFBD><D2B2>е㣨Ӱ<E3A3A8><D3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>/<2F><>ת<EFBFBD><D7AA><EFBFBD>ģ<EFBFBD>
|
|
|
|
|
|
bg.rectTransform.pivot = new Vector2(0, 0.5f);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "right":
|
|
|
|
|
|
default:
|
|
|
|
|
|
// <20><>ê<EFBFBD><C3AA><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2>м䣨Middle-Right<68><74>
|
|
|
|
|
|
bg.rectTransform.anchorMin = new Vector2(1, 0.5f); // <20><><EFBFBD><EFBFBD>ê<EFBFBD>㣨<EFBFBD>Ҳࣩ
|
|
|
|
|
|
bg.rectTransform.anchorMax = new Vector2(1, 0.5f); // <20><><EFBFBD><EFBFBD>ê<EFBFBD>㣨<EFBFBD>Ҳࣩ
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>λ<EFBFBD><CEBB>ƫ<EFBFBD>ƹ<EFBFBD><C6B9>㣨<EFBFBD><E3A3A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ê<EFBFBD>㣩
|
|
|
|
|
|
bg.rectTransform.anchoredPosition = Vector2.zero;
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>Ϊ<EFBFBD>Ҳ<EFBFBD><D2B2>е㣨Ӱ<E3A3A8><D3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>/<2F><>ת<EFBFBD><D7AA><EFBFBD>ģ<EFBFBD>
|
|
|
|
|
|
bg.rectTransform.pivot = new Vector2(1, 0.5f);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-19 11:49:19 +08:00
|
|
|
|
public void Drop(string name, GameObject obj)
|
|
|
|
|
|
{
|
|
|
|
|
|
int count = PlayerController.Instance.DropDevice(name);
|
|
|
|
|
|
if (count <= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
var device = DeviceController.Instance.GetDevice(obj.name);
|
|
|
|
|
|
if (dict.ContainsKey(device))
|
|
|
|
|
|
{
|
|
|
|
|
|
dict.Remove(device);
|
|
|
|
|
|
}
|
|
|
|
|
|
GameObject.Destroy(obj);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
obj.transform.Find("IconBg/Icon/Count").GetComponent<TextMeshProUGUI>().text = count.ToString();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-03-08 13:15:13 +08:00
|
|
|
|
protected override void OnShow()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnHide()
|
|
|
|
|
|
{
|
2025-05-19 11:49:19 +08:00
|
|
|
|
TypeEventSystem.Global.UnRegister<OnPickDevice>(OnPickDeviceHandler);
|
2025-03-08 13:15:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnClose()
|
|
|
|
|
|
{
|
2025-05-19 11:49:19 +08:00
|
|
|
|
TypeEventSystem.Global.UnRegister<OnPickDevice>(OnPickDeviceHandler);
|
2025-03-08 13:15:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
public void OnModuleQuit(OnModuleQuit arg)
|
|
|
|
|
|
{
|
|
|
|
|
|
Hide();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|