367 lines
13 KiB
C#
367 lines
13 KiB
C#
using Assets.Framework.Tool;
|
|
using Assets.MyGame.Scripts.Net;
|
|
using Assets.MyGame.Scripts.Net.API.UserData_SubClass;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class UIHome_BY_BackPackPanel : SubUIBase
|
|
{
|
|
public List<BaseProductCount> props;
|
|
public Dictionary<string, BaseProductCount> propsID_Dic = new Dictionary<string, BaseProductCount>();
|
|
public Dictionary<int, BaseProductCount> propsObjHashCode_Dic = new Dictionary<int, BaseProductCount>();
|
|
public Transform pageParent;
|
|
public BackPackPropItem prefabItem;
|
|
|
|
public TextMeshProUGUI tMP_Gold;
|
|
public TextMeshProUGUI tMP_Diamond;
|
|
|
|
[Header("道具详情")]
|
|
public TextMeshProUGUI tMP_Name;
|
|
public TextMeshProUGUI tMP_Description;
|
|
public Image img_Prop;
|
|
public string propID;
|
|
public string propName; // 当前选中道具名称(数量选择面板用)
|
|
public PropEnum propType;
|
|
|
|
public ScrollRect scrollRect;
|
|
public Button btn_Use;
|
|
|
|
[Header("改名")]//使用改名卡后,弹出输入框,输入新名字,点击确定按钮,发送请求修改名字
|
|
public GameObject changNamePanel;
|
|
public TMP_InputField tMPText_NewName;
|
|
public Button btn_ChangeName;
|
|
|
|
[Header("大喇叭")]//使用大喇叭后,弹出输入框,输入弹幕,点击确定按钮,发送请求
|
|
public GameObject DaLaBaPanel;
|
|
public TMP_InputField tMPText_DaLaBaLabel;
|
|
public Button btn_FaSong;
|
|
[Header("使用提示(带数量)")]
|
|
public GameObject useObj;
|
|
public Slider slider_UseNum;
|
|
public TextMeshProUGUI tMPText_ShiyongLabel;//确认使用道具 propname*<color=yellow>数量</color>
|
|
public Button btn_Add;
|
|
public Button btn_Subtract;
|
|
public Button btn_QueRen;
|
|
|
|
/// <summary>当前选中道具的拥有数量(用于数量选择面板上限)</summary>
|
|
private int _curPropOwnedCount;
|
|
/// <summary>当前选中数量选择面板对应道具的回调(确认使用时调用)</summary>
|
|
private System.Action<int> _curUseCountAction;
|
|
|
|
// 刷新时保存的状态
|
|
//private Vector2 savedScrollPos;
|
|
private string savedPropID;
|
|
|
|
public override void Awake()
|
|
{
|
|
base.Awake();
|
|
GameManager.Instance.OnUserDataChanged += OnUserDataChanged;
|
|
|
|
// 改名确认按钮
|
|
if (btn_ChangeName != null)
|
|
btn_ChangeName.onClick.AddListener(OnClickConfirmChangeName);
|
|
|
|
// 改名面板默认关闭
|
|
if (changNamePanel != null)
|
|
changNamePanel.SetActive(false);
|
|
|
|
// 大喇叭
|
|
if (DaLaBaPanel != null)
|
|
DaLaBaPanel.SetActive(false);
|
|
if (btn_FaSong != null)
|
|
{
|
|
// ⭐ 先清再绑:防止面板重复 Show 叠加监听器(点一次发 N 次 = 多扣 N 个大喇叭)
|
|
btn_FaSong.onClick.RemoveAllListeners();
|
|
btn_FaSong.onClick.AddListener(OnClickFaSong);
|
|
}
|
|
|
|
// 数量选择面板(默认隐藏)
|
|
if (useObj != null) useObj.SetActive(false);
|
|
|
|
if (slider_UseNum != null)
|
|
slider_UseNum.onValueChanged.AddListener(v => UpdateUseNumLabel());
|
|
|
|
if (btn_Add != null)
|
|
btn_Add.onClick.AddListener(() =>
|
|
{
|
|
if (slider_UseNum != null)
|
|
slider_UseNum.value = Mathf.Min((int)slider_UseNum.value + 1, (int)slider_UseNum.maxValue);
|
|
});
|
|
|
|
if (btn_Subtract != null)
|
|
btn_Subtract.onClick.AddListener(() =>
|
|
{
|
|
if (slider_UseNum != null)
|
|
slider_UseNum.value = Mathf.Max((int)slider_UseNum.value - 1, (int)slider_UseNum.minValue);
|
|
});
|
|
|
|
if (btn_QueRen != null)
|
|
btn_QueRen.onClick.AddListener(OnClickConfirmUseNum);
|
|
}
|
|
|
|
/// <summary>刷新数量选择标签</summary>
|
|
private void UpdateUseNumLabel()
|
|
{
|
|
if (tMPText_ShiyongLabel == null) return;
|
|
int n = slider_UseNum != null ? Mathf.RoundToInt(slider_UseNum.value) : 1;
|
|
tMPText_ShiyongLabel.text = $"确认使用道具 <color=yellow>{propName}</color> * <color=yellow>{n}</color>";
|
|
}
|
|
|
|
/// <summary>确认使用按钮:执行当前数量回调</summary>
|
|
private void OnClickConfirmUseNum()
|
|
{
|
|
int n = slider_UseNum != null ? Mathf.RoundToInt(slider_UseNum.value) : 1;
|
|
if (n <= 0)
|
|
{
|
|
UIManager.Instance.ShowBoardError("数量必须大于0");
|
|
return;
|
|
}
|
|
var cb = _curUseCountAction;
|
|
HideUseNumPanel();
|
|
cb?.Invoke(n);
|
|
}
|
|
|
|
/// <summary>显示数量选择面板</summary>
|
|
/// <param name="ownedCount">当前拥有数量(滑块上限)</param>
|
|
/// <param name="onConfirm">确认使用回调(参数=使用数量)</param>
|
|
private void ShowUseNumPanel(int ownedCount, System.Action<int> onConfirm)
|
|
{
|
|
_curPropOwnedCount = ownedCount;
|
|
_curUseCountAction = onConfirm;
|
|
|
|
if (useObj != null) useObj.SetActive(true);
|
|
|
|
if (slider_UseNum != null)
|
|
{
|
|
slider_UseNum.minValue = 1;
|
|
slider_UseNum.maxValue = Mathf.Max(1, ownedCount);
|
|
slider_UseNum.value = 1;
|
|
slider_UseNum.wholeNumbers = true;
|
|
}
|
|
UpdateUseNumLabel();
|
|
}
|
|
|
|
/// <summary>隐藏数量选择面板</summary>
|
|
private void HideUseNumPanel()
|
|
{
|
|
if (useObj != null) useObj.SetActive(false);
|
|
_curUseCountAction = null;
|
|
}
|
|
|
|
/// <summary>大喇叭发送中标志:防止回调返回前连点发送按钮导致多发多扣道具</summary>
|
|
private bool _isSendingFaSong;
|
|
|
|
private void OnClickFaSong()
|
|
{
|
|
// ⭐ 防重入:上次请求回调还没回来时连点直接忽略
|
|
if (_isSendingFaSong) return;
|
|
// ⭐ 先取文本判空再拼前缀(原"广播:"+text 判空永远不成立,空内容也会发送扣道具)
|
|
string content = tMPText_DaLaBaLabel?.text?.Trim();
|
|
if (string.IsNullOrEmpty(content))
|
|
{
|
|
UIManager.Instance.ShowBoardError("请输入广播内容");
|
|
return;
|
|
}
|
|
|
|
_isSendingFaSong = true;
|
|
// ⭐ 显示格式:"<sprite index=0>【小*鱼】:内容"(前面加喇叭图标 TMP 标签;名字打码与游戏内大喇叭同格式)
|
|
var userName = GameManager.Instance.user?.BaseInfo?.userName;
|
|
if (string.IsNullOrEmpty(userName)) userName = "玩家";
|
|
APIRequestList.SendBroadcast($"<sprite index=0>【{MaskBroadcastName(userName)}】:{content}", (result) =>
|
|
{
|
|
_isSendingFaSong = false;
|
|
tMPText_DaLaBaLabel.text = "";
|
|
DaLaBaPanel.SetActive(false);
|
|
// ⭐ 发送成功=服务端已扣大喇叭:拉最新用户数据刷新背包数量
|
|
APIRequestList.GetUser((user) =>
|
|
{
|
|
if (user != null)
|
|
GameManager.Instance.UpdataUserData(user);
|
|
});
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 玩家名打码(广播格式):保留首尾字符、中间一个 *("小金鱼"→"小*鱼")。
|
|
/// 与 GameMainPanel_FishBase.MaskBroadcastName 同格式(背包面板独立副本)。
|
|
/// </summary>
|
|
private static string MaskBroadcastName(string name)
|
|
{
|
|
if (string.IsNullOrEmpty(name)) return name;
|
|
name = name.Trim();
|
|
if (name.Length <= 1) return name;
|
|
return name[0] + "*" + name[name.Length - 1];
|
|
}
|
|
|
|
private void OnClickConfirmChangeName()
|
|
{
|
|
string newName = tMPText_NewName?.text?.Trim();
|
|
if (string.IsNullOrEmpty(newName))
|
|
{
|
|
UIManager.Instance.ShowBoardError("请输入新名字");
|
|
return;
|
|
}
|
|
|
|
UIManager.Instance.ShowTiShi($"确定要将名字改为\"{newName}\"吗?", () =>
|
|
{
|
|
APIRequestList.UpdateUserName(newName, (result) =>
|
|
{
|
|
UIManager.Instance.ShowBoardError("改名成功");
|
|
tMPText_NewName.text = "";
|
|
changNamePanel.SetActive(false);
|
|
// 拉取服务器最新用户数据,同步本地并触发全局 UI 更新
|
|
APIRequestList.GetUser((user) =>
|
|
{
|
|
if (user != null)
|
|
GameManager.Instance.UpdataUserData(user);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
// 兜底:防止面板被销毁后仍留在事件里(否则之后任何 UpdataUserData 都会调到已销毁的面板,
|
|
// 抛 MissingReferenceException 并中断整个事件链,导致其他面板的用户数据无法刷新)
|
|
if (GameManager.Instance != null)
|
|
GameManager.Instance.OnUserDataChanged -= OnUserDataChanged;
|
|
}
|
|
|
|
private void OnUserDataChanged()
|
|
{
|
|
// 刷新前保存状态
|
|
//if (scrollRect != null) savedScrollPos = scrollRect.normalizedPosition;
|
|
savedPropID = propID;
|
|
|
|
RefreshUI();
|
|
RebuildPropsList();
|
|
|
|
// 刷新后恢复状态
|
|
//if (scrollRect != null) scrollRect.normalizedPosition = savedScrollPos;
|
|
}
|
|
|
|
protected override void OnShow(object args = null)
|
|
{
|
|
base.OnShow(args);
|
|
RefreshUI();
|
|
RebuildPropsList();
|
|
}
|
|
|
|
private void RefreshUI()
|
|
{
|
|
if (GameManager.Instance?.user?.assets == null) return;
|
|
tMP_Gold.text = GameManager.Instance.user.assets.gold.ToString();
|
|
tMP_Diamond.text = GameManager.Instance.user.assets.diamond.ToString();
|
|
}
|
|
|
|
private void RebuildPropsList()
|
|
{
|
|
props = GameManager.Instance.user.props;
|
|
|
|
// 构建字典,过滤数量 ≤0 的
|
|
propsID_Dic.Clear();
|
|
if (props != null)
|
|
{
|
|
foreach (BaseProductCount product in props)
|
|
{
|
|
if (product.count > 0)
|
|
propsID_Dic[product.number] = product;
|
|
}
|
|
}
|
|
|
|
// 清空旧列表
|
|
for (int j = pageParent.childCount - 1; j >= 0; j--)
|
|
Destroy(pageParent.GetChild(j).gameObject);
|
|
propsObjHashCode_Dic.Clear();
|
|
|
|
Toggle firstToggle = null;
|
|
Toggle savedToggle = null;
|
|
|
|
foreach (BaseProductCount product in propsID_Dic.Values)
|
|
{
|
|
GameObject obj = Instantiate(prefabItem.gameObject, pageParent);
|
|
var tempItem = obj.GetComponent<BackPackPropItem>();
|
|
tempItem.Init(new BackPackPropItemData
|
|
{
|
|
url = APIManager.baseURL + product.imageUrl,
|
|
count = product.count,
|
|
propEnum = (PropEnum)product.type
|
|
});
|
|
|
|
var toggle = obj.GetComponent<Toggle>();
|
|
propsObjHashCode_Dic[obj.GetHashCode()] = product;
|
|
if (firstToggle == null) firstToggle = toggle;
|
|
if (!string.IsNullOrEmpty(savedPropID) && product.number == savedPropID)
|
|
savedToggle = toggle;
|
|
|
|
obj.GetComponent<Toggle>().onValueChanged.AddListener((value) =>
|
|
{
|
|
if (value) ShowPropDetail(product);
|
|
});
|
|
}
|
|
|
|
// 优先选中刷新前的道具;若已用完则选中第一个
|
|
var target = savedToggle ?? firstToggle;
|
|
if (target != null) target.isOn = true;
|
|
}
|
|
|
|
private void ShowPropDetail(BaseProductCount product)
|
|
{
|
|
if (product == null) return;
|
|
tMP_Name.text = product.name;
|
|
tMP_Description.text = product.desc;
|
|
propID = product.number;
|
|
propName = product.name;
|
|
propType = (PropEnum)product.type;
|
|
|
|
WebTool.DownImage(APIManager.baseURL + product.imageUrl, (texture) =>
|
|
{
|
|
if (texture != null && img_Prop != null)
|
|
img_Prop.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f);
|
|
});
|
|
|
|
btn_Use.onClick.RemoveAllListeners();
|
|
btn_Use.onClick.AddListener(() =>
|
|
{
|
|
switch (propType)
|
|
{
|
|
case PropEnum.none: break;
|
|
case PropEnum.炮塔碎片: break;
|
|
case PropEnum.翅膀碎片: break;
|
|
case PropEnum.战神碎片: break;
|
|
case PropEnum.刮刮卡:
|
|
uIManager.ShowAsync(UIPanel.GuaGuaKa);
|
|
break;
|
|
case PropEnum.改名卡:
|
|
if (changNamePanel != null)
|
|
changNamePanel.SetActive(true);
|
|
break;
|
|
case PropEnum.大喇叭:
|
|
if (DaLaBaPanel != null)
|
|
DaLaBaPanel.SetActive(true);
|
|
break;
|
|
case PropEnum.技能: break;
|
|
case PropEnum.召唤卡: break;
|
|
case PropEnum.VIP经验:
|
|
// 数量选择面板:滑块上限=当前拥有数量
|
|
ShowUseNumPanel(product.count, n =>
|
|
{
|
|
APIRequestList.UseVipExp(n, (result) =>
|
|
{
|
|
UIManager.Instance.ShowBoardError("使用成功");
|
|
APIRequestList.GetUser(GameManager.Instance.user.id, (user) =>
|
|
{
|
|
GameManager.Instance.UpdataUserData(user);
|
|
});
|
|
});
|
|
});
|
|
break;
|
|
default: break;
|
|
}
|
|
});
|
|
}
|
|
}
|