2025-05-19 11:49:19 +08:00

187 lines
6.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using TMPro;
using XMLTool;
using UnityEngine.EventSystems;
using System;
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;
Dictionary<Device, GameObject> dict = new Dictionary<Device, GameObject>();
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
}
private void OnPickDeviceHandler(OnPickDevice device)
{
ItemFactory(device.device);
mResLoader.LoadAsync();
}
public void ItemFactory(Device item)
{
if (dict.ContainsKey(item) == false)
{
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>();
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;
}
}
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);
}
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;
}
}
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();
}
}
protected override void OnShow()
{
}
protected override void OnHide()
{
TypeEventSystem.Global.UnRegister<OnPickDevice>(OnPickDeviceHandler);
}
protected override void OnClose()
{
TypeEventSystem.Global.UnRegister<OnPickDevice>(OnPickDeviceHandler);
}
public void OnModuleQuit(OnModuleQuit arg)
{
Hide();
}
}
}