140 lines
3.8 KiB
C#
140 lines
3.8 KiB
C#
using cfg.FunctionLockCfg;
|
|
using Cysharp.Threading.Tasks;
|
|
using Framework;
|
|
using PhxhSDK;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Gameplay.FunctionLock
|
|
{
|
|
public sealed class FunctionLock : UIGameObjectWrapper
|
|
{
|
|
/// <summary>
|
|
/// 解锁动画
|
|
/// </summary>
|
|
private Animator animator;
|
|
/// <summary>
|
|
/// 解锁描述
|
|
/// </summary>
|
|
private TMP_Text textContent;
|
|
private Button button;
|
|
/// <summary>
|
|
/// 功能锁数据
|
|
/// </summary>
|
|
private FunctionLockNode functionLockData;
|
|
|
|
/// <summary>
|
|
/// 解锁动画触发参数名
|
|
/// </summary>
|
|
private const string ANIM_TRIGGER_NAME = "Unlock";
|
|
/// <summary>
|
|
/// 动画时长
|
|
/// </summary>
|
|
private const float ANIM_TIME = 3f;
|
|
|
|
private bool isPlayingAnim = false;
|
|
|
|
public int FunctionLockId { get { return functionLockData.FunctionLockCfgId; } }
|
|
|
|
public DataFunctionLock cfg;
|
|
|
|
/// <summary>
|
|
/// UI名
|
|
/// </summary>
|
|
public string UIName
|
|
{
|
|
get
|
|
{
|
|
if (null == cfg)
|
|
return string.Empty;
|
|
|
|
return cfg.UIName;
|
|
}
|
|
}
|
|
|
|
public bool IsUnlock
|
|
{
|
|
get
|
|
{
|
|
#if !FUNCTION_LOCK
|
|
return true; // 未启用功能锁,永远处于解锁状态
|
|
#endif
|
|
if (null == cfg)
|
|
return true;
|
|
|
|
return FunctionLockManager.Instance.IsUnlock(cfg.FunctionType);
|
|
}
|
|
}
|
|
|
|
public FunctionLock(GameObject root) : base(root, true)
|
|
{
|
|
functionLockData = GoRoot.GetComponent<FunctionLockNode>();
|
|
animator = GetComponent<Animator>("Lock");
|
|
textContent = GetComponent<TMP_Text>("Lock/TextContent");
|
|
button = GetComponent<Button>("Lock");
|
|
|
|
cfg = TableManager.Instance.Tables.FunctionLockConfig.Get(functionLockData.FunctionLockCfgId);
|
|
|
|
SetState(); // 设置状态
|
|
|
|
isPlayingAnim = false;
|
|
|
|
button.onClick.AddListener(OnLock);
|
|
}
|
|
|
|
private void SetState()
|
|
{
|
|
if (IsUnlock) // 已解锁
|
|
{
|
|
IsScaleShow = false;
|
|
|
|
if (null != functionLockData.ButtonFunction)
|
|
functionLockData.ButtonFunction.interactable = true;
|
|
}
|
|
else // 未解锁
|
|
{
|
|
if (null != textContent)
|
|
textContent.text = CommonUtils.GetLocalizeText(cfg.DescKey);
|
|
|
|
IsScaleShow = true;
|
|
|
|
if (null != functionLockData.ButtonFunction)
|
|
functionLockData.ButtonFunction.interactable = false;
|
|
|
|
FunctionLockManager.Instance.AddFunctionLock(this);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 点击锁
|
|
/// </summary>
|
|
private void OnLock()
|
|
{
|
|
UITipsManager.ShowTips(CommonUtils.GetLocalizeText("ui_functionLockTip")); // 功能未解锁
|
|
}
|
|
|
|
/// <summary>
|
|
/// 播放解锁动画
|
|
/// </summary>
|
|
public async void PlayUnlockAnim()
|
|
{
|
|
if (isPlayingAnim)
|
|
return;
|
|
|
|
if (IsUnlock) // 已经解锁直接返回
|
|
return;
|
|
|
|
isPlayingAnim = true;
|
|
FunctionLockManager.Instance.AddAnimState();
|
|
|
|
animator.SetTrigger(ANIM_TRIGGER_NAME);
|
|
FunctionLockManager.Instance.SaveServer((int)cfg.FunctionType); // 服务器保存解锁状态
|
|
await UniTask.WaitForSeconds(ANIM_TIME);
|
|
SetState();
|
|
|
|
FunctionLockManager.Instance.RemoveAnimState();
|
|
isPlayingAnim = false;
|
|
}
|
|
}
|
|
} |