//-------------------------------------------------------- // [Author]: 第二世界 // [ Date ]: Monday, November 27, 2017 //-------------------------------------------------------- using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; namespace Snxxz.UI { public class FunctionalGuideTrigger : MonoBehaviour { [SerializeField] int[] guides; RectTransform container; Dictionary behaviours = new Dictionary(); TreasureModel treasureModel { get { return ModelCenter.Instance.GetModel(); } } AchievementModel achievementModel { get { return ModelCenter.Instance.GetModel(); } } Window m_Parent; Window parent { get { return m_Parent ?? (m_Parent = this.transform.GetComponentInParent()); } } private void Awake() { WindowCenter.Instance.windowAfterOpenEvent += OnWindowOpen; WindowCenter.Instance.windowBeforeCloseEvent += OnWindowPreClose; var instance = new GameObject("FunctionalGuideContainer"); container = instance.AddMissingComponent(); container.MatchWhith(parent.transform as RectTransform); } private void OnDestroy() { WindowCenter.Instance.windowAfterOpenEvent -= OnWindowOpen; WindowCenter.Instance.windowBeforeCloseEvent -= OnWindowPreClose; } private void OnWindowOpen(Window _window) { if (parent != _window) { return; } container.gameObject.SetActive(true); CheckGuide(); var underwayGuides = FunctionalGuideCenter.Instance.GetUnderwayGuides(); for (int i = 0; i < underwayGuides.Count; i++) { var guideId = underwayGuides[i]; if (GuideBelongtoThisTrigger(guideId)) { BeginFunctionalGuide(guideId); } } FuncOpen.Instance.OnFuncStateChangeEvent += OnFunctionOpen; PlayerDatas.Instance.PlayerDataRefreshInfoEvent += OnLevelChange; PlayerTaskDatas.Event_MainlineTask += OnMainLineTaskUpdate; PlayerTaskDatas.CardLevelChange += OnMainLineTaskLimitStateChange; FunctionUnlockFlyObject.functionUnLockShowEndEvent += OnFunctionUnLockShowEnd; FunctionalGuideCenter.Instance.beginGuideEvent += BeginFunctionalGuide; FunctionalGuideCenter.Instance.removeGuideEvent += OnGuideRemove; treasureModel.treasureCollectProgressUpdateEvent += OnTreasureCollectingStateChange; treasureModel.treasureStageUpEvent += OnTreasureStageChange; achievementModel.achievementAwardableEvent += OnAchievementAwardAble; } private void OnWindowPreClose(Window _window) { if (parent != _window) { return; } foreach (var behaviour in behaviours.Values) { if (behaviour != null) { FunctionalGuideBehaviourPool.Recycle(behaviour.gameObject); } } behaviours.Clear(); container.gameObject.SetActive(false); FuncOpen.Instance.OnFuncStateChangeEvent -= OnFunctionOpen; PlayerDatas.Instance.PlayerDataRefreshInfoEvent -= OnLevelChange; PlayerTaskDatas.Event_MainlineTask -= OnMainLineTaskUpdate; PlayerTaskDatas.CardLevelChange -= OnMainLineTaskLimitStateChange; FunctionUnlockFlyObject.functionUnLockShowEndEvent -= OnFunctionUnLockShowEnd; FunctionalGuideCenter.Instance.beginGuideEvent -= BeginFunctionalGuide; FunctionalGuideCenter.Instance.removeGuideEvent -= OnGuideRemove; treasureModel.treasureCollectProgressUpdateEvent -= OnTreasureCollectingStateChange; treasureModel.treasureStageUpEvent -= OnTreasureStageChange; achievementModel.achievementAwardableEvent -= OnAchievementAwardAble; } private void OnFunctionOpen(int _functionId) { CheckGuide(); } private void OnLevelChange(PlayerDataRefresh refreshType) { switch (refreshType) { case PlayerDataRefresh.LV: CheckGuide(); break; } } private void OnMainLineTaskUpdate(int _taskId, int _state) { CheckGuide(); } private void OnMainLineTaskLimitStateChange(int _taskId) { CheckGuide(); } private void OnFunctionUnLockShowEnd(FunctionUnlockType _type) { CheckGuide(); } private void OnTreasureCollectingStateChange(int _treasureId) { CheckGuide(); } private void OnTreasureStageChange(int _treasureId) { CheckGuide(); } private void OnAchievementAwardAble(int _achievementId) { CheckGuide(); } private void CheckGuide() { FunctionalGuideCenter.Instance.CheckGuides(guides); } private void OnGuideRemove(int _guide) { if (behaviours.ContainsKey(_guide)) { FunctionalGuideBehaviourPool.Recycle(behaviours[_guide].gameObject); behaviours.Remove(_guide); } } private void BeginFunctionalGuide(int _guide) { if (!GuideBelongtoThisTrigger(_guide)) { return; } var behaviour = FunctionalGuideBehaviourPool.Require(); var behaviourRectTransform = (RectTransform)behaviour.transform; behaviourRectTransform.MatchWhith(container); behaviour.gameObject.SetActive(true); behaviour.BeginGuide(_guide); behaviours[_guide] = behaviour; } private bool GuideBelongtoThisTrigger(int _guide) { for (int i = 0; i < guides.Length; i++) { if (guides[i] == _guide) { return true; } } return false; } } }