using cfg.FunctionLockCfg; using Cysharp.Threading.Tasks; using Framework; using PhxhSDK; using TMPro; using UnityEngine; namespace Gameplay.FunctionLock { public sealed class FunctionLock : UIGameObjectWrapper { /// /// 解锁动画 /// private Animator animator; /// /// 解锁描述 /// private TMP_Text textContent; /// /// 功能锁数据 /// private FunctionLockNode functionLockData; /// /// 解锁动画触发参数名 /// private const string ANIM_TRIGGER_NAME = "Unlock"; /// /// 动画时长 /// private const float ANIM_TIME = 3f; private bool isPlayingAnim = false; public int FunctionLockId { get { return functionLockData.FunctionLockCfgId; } } public DataFunctionLock cfg; /// /// UI名 /// 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(); animator = GetComponent("Lock"); textContent = GetComponent("Lock/TextContent"); cfg = TableManager.Instance.Tables.FunctionLockConfig.Get(functionLockData.FunctionLockCfgId); SetState(); // 设置状态 isPlayingAnim = false; functionLockData.ButtonFunction.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); } } /// /// 点击锁 /// private void OnLock() { UITipsManager.ShowTips(CommonUtils.GetLocalizeText("ui_functionLockTip")); // 功能未解锁 } /// /// 播放解锁动画 /// 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; } } }