NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/UI/ShowAllCharacter/GenericItem.cs

304 lines
7.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using cfg.ActorCfg;
using Framework;
using Gameplay.Net;
using PhxhSDK;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 通用道具
/// </summary>
public class GenericItem : UIGameObjectWrapper
{
/// <summary>
/// 置灰的灰度
/// </summary>
private const float GREY = 0.5f;
/// <summary>
/// 正常颜色
/// </summary>
private const float NORMAL = 1f;
#region UI
/// <summary>
/// 道具品质
/// </summary>
private Image imageQuality;
/// <summary>
/// 道具图标
/// </summary>
private Image imageIcon;
/// <summary>
/// 道具数量背景
/// </summary>
private Image imageCount;
/// <summary>
/// 道具数量文本
/// </summary>
private TMP_Text textCount;
/// <summary>
/// 道具按钮
/// </summary>
private Button button;
#endregion
/// <summary>
/// 道具配置
/// </summary>
private DataItem dataItem;
/// <summary>
/// 回调
/// </summary>
private Action callbackAction;
#region 属性
/// <summary>
/// 是否显示道具数量
/// </summary>
public bool IsShowCount
{
set
{
imageCount.gameObject.IsScaleShow(value);
}
}
/// <summary>
/// 是否置灰
/// </summary>
public bool IsGrey
{
set
{
if (value)
{
imageQuality.color = new Color(GREY, GREY, GREY, imageQuality.color.a);
imageIcon.color = new Color(GREY, GREY, GREY, imageIcon.color.a);
imageCount.color = new Color(GREY, GREY, GREY, imageCount.color.a);
textCount.color = new Color(GREY, GREY, GREY, textCount.color.a);
}
else
{
imageQuality.color = new Color(NORMAL, NORMAL, NORMAL, imageQuality.color.a);
imageIcon.color = new Color(NORMAL, NORMAL, NORMAL, imageIcon.color.a);
imageCount.color = new Color(NORMAL, NORMAL, NORMAL, imageCount.color.a);
textCount.color = new Color(NORMAL, NORMAL, NORMAL, textCount.color.a);
}
}
}
/// <summary>
/// 是否开启按钮
/// </summary>
public bool IsEnableButton
{
set { button.enabled = value; }
}
#endregion
/// <summary>
/// 根据GameObject构造
/// </summary>
/// <param name="root"></param>
public GenericItem(GameObject root, Action callback = null) : base(root, false)
{
Construct(callback);
}
/// <summary>
/// 根据GameObject克隆并构造
/// </summary>
/// <param name="root"></param>
/// <param name="i"></param>
public GenericItem(GameObject root, int i, Action callback = null) : base(root, i, false)
{
Construct(callback);
}
private void Construct(Action callback)
{
callbackAction = callback;
imageQuality = GetComponent<Image>("BaseItem/ItemQuality");
imageIcon = GetComponent<Image>("BaseItem/ItemIcon");
imageCount = GetComponent<Image>("BaseItem/ImageCount");
textCount = GetComponent<TMP_Text>("BaseItem/ImageCount/TextCount");
button = GetComponent<Button>("BaseItem");
IsShowCount = false;
IsGrey = false;
IsEnableButton = callbackAction != null;
BindButton(button, OnClick);
}
/// <summary>
/// 设置道具数据
/// </summary>
/// <param name="data">道具配置</param>
/// <param name="count">道具数量道具数量小于等于0时不显示道具数量</param>
public virtual async void SetInfo(DataItem data, int count = 0)
{
dataItem = data;
if (dataItem == null)
{
DebugUtil.LogError("空数据");
return;
}
SetCount(count);
imageIcon.sprite = await AssetManager.Instance.LoadAssetAsync<Sprite>(dataItem.IconPath);
IsScaleShow = true;
}
/// <summary>
/// 设置道具数据
/// </summary>
/// <param name="iconPath">图标路径</param>
/// <param name="count">道具数量道具数量小于等于0时不显示道具数量</param>
/// <param name="callback">点击回调,回调是空时禁用点击</param>
public virtual async void SetInfo(string iconPath, int count = 0, Action callback = null)
{
SetCount(count);
SetCallback(callback);
imageIcon.sprite = await AssetManager.Instance.LoadAssetAsync<Sprite>(iconPath);
IsScaleShow = true;
}
/// <summary>
/// 根据道具id设置道具数据
/// </summary>
/// <param name="itemID">道具配置id</param>
/// <param name="count">道具数量道具数量小于等于0时不显示道具数量</param>
public virtual void SetInfo(int itemID, int count = 0)
{
DataItem data = TableManager.Instance.Tables.Item.Get(itemID);
if (data == null)
{
DebugUtil.LogError($"道具id错误,错误道具id{itemID}");
return;
}
SetInfo(data, count);
}
/// <summary>
/// 设置回调
/// </summary>
/// <param name="callback"></param>
public void SetCallback(Action callback)
{
callbackAction = callback;
IsEnableButton = callbackAction != null;
}
/// <summary>
/// 设置道具数量
/// </summary>
public void SetCount(int count)
{
if (count <= 0)
{
IsShowCount = false;
return;
}
textCount.text = count.ToString();
IsShowCount = true;
}
private void OnClick()
{
callbackAction?.Invoke();
}
}
/// <summary>
/// 通用道具,通关奖励
/// </summary>
public class GenericItem_PassReward : GenericItem
{
/// <summary>
/// 已获取状态
/// </summary>
private GameObject goGet;
/// <summary>
/// 首次通关状态
/// </summary>
private GameObject goFirstPass;
/// <summary>
/// 重复通关状态
/// </summary>
private GameObject goNormalPass;
/// <summary>
/// 是否已经获取
/// </summary>
public bool IsGet
{
set
{
goGet.IsScaleShow(value);
IsGrey = value;
}
}
/// <summary>
/// 是否首次通关
/// </summary>
public bool IsFirstPass
{
set { goFirstPass.IsScaleShow(value); }
}
/// <summary>
/// 是否重复通关
/// </summary>
public bool IsNormalPass
{
set { goNormalPass.IsScaleShow(value); }
}
public GenericItem_PassReward(GameObject root, Action callback = null) : base(root, callback)
{
Construct();
}
public GenericItem_PassReward(GameObject root, int i, Action callback = null) : base(root, i, callback)
{
Construct();
}
private void Construct()
{
goGet = FindObj("BaseItem_PassReward/TextAlreadyGet");
goNormalPass = FindObj("BaseItem_PassReward/LevelNormalPass");
goFirstPass = FindObj("BaseItem_PassReward/LevelFirstPass");
IsGet = false;
IsFirstPass = false;
IsNormalPass = false;
}
/// <summary>
/// 设置奖励道具数据
/// </summary>
/// <param name="rewardItem"></param>
/// <param name="isGet"></param>
/// <param name="rewardType"></param>
public void SetInfo(RewardItem rewardItem, bool isGet = false, ERewardType rewardType = ERewardType.None)
{
IsGet = isGet;
IsFirstPass = rewardType == ERewardType.LevelFirstPassReward;
IsNormalPass = rewardType == ERewardType.LevelNormalPassReward;
SetInfo(rewardItem.Data, rewardItem.Count);
}
}