//-------------------------------------------------------- // [Author]: 第二世界 // [ Date ]: Wednesday, April 11, 2018 //-------------------------------------------------------- using System; using System.Collections.Generic; using UnityEngine; using System.Text; using vnxbqy.UI; using UnityEngine.UI; namespace vnxbqy.UI { public class HighSettingFadeInFadeOut : MonoBehaviour { [SerializeField] Button m_Switch;//切换按钮1 [SerializeField] Image m_ArrowOpen; [SerializeField] Image m_ArrowClose; [SerializeField] Vector2 m_GridSize; [SerializeField] Vector2 m_Space; [SerializeField] MainWinTopGrid[] m_GridGroup1; [SerializeField] MainWinTopGrid[] m_GridGroup2; [SerializeField] RectTransform m_GroupContainer1; [SerializeField] RectTransform m_GroupContainer2; static State state = State.Opened; const int ALWAYSHOW_THRESHOLD = 7; const int LineNume = 7; //每行显示的数量 static float closeTimer = 0f; int openedFunctionCount = 0; TreasureFindHostModel hostModel { get { return ModelCenter.Instance.GetModel(); } } LimitedTimePackageItemClassModel limitedTimePackageItemModel { get { return ModelCenter.Instance.GetModel(); } } ImpactRankModel impactRankModel { get { return ModelCenter.Instance.GetModel(); } } OpenServiceAchievementModel OpenServerModel { get { return ModelCenter.Instance.GetModel(); } } public static void SetAutoCloseTimer() { closeTimer = 8f; } void Start() { m_Switch.SetListener(() => { Switch(state == State.Closed); }); openedFunctionCount = 0; openedFunctionCount += CountOpenedFunction(m_GridGroup1); openedFunctionCount += CountOpenedFunction(m_GridGroup2); SwitchImmedidately(true); } public void Init() { this.enabled = true; RefreshGridsActiveState(); TimeUtility.OnServerOpenDayRefresh += OnStepServerDay; FuncOpen.Instance.OnFuncStateChangeEvent += OnFuncStateChange; hostModel.TreasureFindHostCompleteAct += TreasureFindHostCompleteAct; RedpointCenter.Instance.redpointValueChangeEvent += UpdateRedpoint; OpenServerActivityCenter.Instance.openServerActivityStateChange += OpenServerActivityStateChange; OperationTimeHepler.Instance.operationStartEvent += OperationStartEvent; OperationTimeHepler.Instance.operationEndEvent += OperationEndEvent; limitedTimePackageItemModel.UpdateLimitedTimePackageItem += UpdateLimitedTimePackageItem; impactRankModel.inImpactRankUpdate += InImpactRankUpdate; OpenServerModel.IsOpenEvent += IsOpenEvent; OpenServerModel.IsPreviewTimeUpdate += IsOpenEvent; OperationTimeHepler.Instance.operationAdvanceEvent += OperationAdvanceEvent; } public void UnInit() { FuncOpen.Instance.OnFuncStateChangeEvent -= OnFuncStateChange; hostModel.TreasureFindHostCompleteAct -= TreasureFindHostCompleteAct; TimeUtility.OnServerOpenDayRefresh -= OnStepServerDay; OpenServerActivityCenter.Instance.openServerActivityStateChange -= OpenServerActivityStateChange; RedpointCenter.Instance.redpointValueChangeEvent -= UpdateRedpoint; OperationTimeHepler.Instance.operationStartEvent -= OperationStartEvent; OperationTimeHepler.Instance.operationEndEvent -= OperationEndEvent; limitedTimePackageItemModel.UpdateLimitedTimePackageItem -= UpdateLimitedTimePackageItem; impactRankModel.inImpactRankUpdate -= InImpactRankUpdate; OpenServerModel.IsOpenEvent -= IsOpenEvent; OpenServerModel.IsPreviewTimeUpdate -= IsOpenEvent; OperationTimeHepler.Instance.operationAdvanceEvent -= OperationAdvanceEvent; this.enabled = false; } void Switch(bool active) { //var index = 0; //foreach (var grid in m_GridGroup1) //{ // if (!grid.alwayShow && openedFunctionCount > ALWAYSHOW_THRESHOLD) // { // grid.Switch(active, 0, 0f); // } // if (grid.gameObject.activeInHierarchy) // { // index++; // } //} //index = 0; //foreach (var grid in m_GridGroup2) //{ // if (!grid.alwayShow && openedFunctionCount > ALWAYSHOW_THRESHOLD) // { // var offset = (index / LineNume) + 1; // grid.Switch(active, offset * (m_GridSize.y + m_Space.y), offset * 0.1f); // } // if (grid.gameObject.activeInHierarchy) // { // index++; // } //} state = active ? State.Opened : State.Closed; DisplayArrow(state); if (state == State.Opened) { SetAutoCloseTimer(); } RefreshGridsActiveState(); } public void SwitchImmedidately(bool active) { //foreach (var grid in m_GridGroup1) //{ // if (grid.alwayShow || openedFunctionCount <= ALWAYSHOW_THRESHOLD) // { // grid.SwitchImmediately(true, 0); // } // else // { // grid.SwitchImmediately(active, 0); // } //} //var index = 0; //foreach (var grid in m_GridGroup2) //{ // if (grid.alwayShow || openedFunctionCount <= ALWAYSHOW_THRESHOLD) // { // grid.SwitchImmediately(true, ((index / LineNume) + 1) * (m_GridSize.y + m_Space.y)); // } // else // { // grid.SwitchImmediately(active, ((index / LineNume) + 1) * (m_GridSize.y + m_Space.y)); // } // if (grid.gameObject.activeInHierarchy) // { // index++; // } //} state = active ? State.Opened : State.Closed; DisplayArrow(state); if (state == State.Opened) { SetAutoCloseTimer(); } RefreshGridsActiveState(); } private void OperationAdvanceEvent(Operation obj) { RefreshGridsActiveState(); } private void IsOpenEvent() { RefreshGridsActiveState(); } private void InImpactRankUpdate() { RefreshGridsActiveState(); } private void UpdateLimitedTimePackageItem() { RefreshGridsActiveState(); } private void OperationEndEvent(Operation arg1, int arg2) { RefreshGridsActiveState(); } private void OperationStartEvent(Operation arg1, int arg2) { RefreshGridsActiveState(); } private void UpdateRedpoint(int redPointID) { if (redPointID == 201 || redPointID == 210) { RefreshGridsActiveState(); } } private void OpenServerActivityStateChange() { RefreshGridsActiveState(); } private void OnStepServerDay()//开服活动 { var days = ModelCenter.Instance.GetModel().openServerActivityDays; if (TimeUtility.OpenDay >= days) { RefreshGridsActiveState(); } } private void TreasureFindHostCompleteAct()//仙宝寻主 { RefreshGridsActiveState(); } private void OnFuncStateChange(int obj) { RefreshGridsActiveState(); } private void RefreshGridsActiveState() { openedFunctionCount = 0; openedFunctionCount += CountOpenedFunction(m_GridGroup1); openedFunctionCount += CountOpenedFunction(m_GridGroup2); ActiveGroup(m_GridGroup1); ActiveGroup(m_GridGroup2); LayoutGroup(m_GridGroup1, m_GroupContainer1); LayoutGroup(m_GridGroup2, m_GroupContainer2); m_Switch.SetActive(IsShowButton()); } bool IsShowButton() { if (openedFunctionCount <= ALWAYSHOW_THRESHOLD) { return false; } //foreach (var grid in m_GridGroup1) //{ // if (!grid.alwayShow && grid.gameObject.activeSelf) // { // return true; // } //} //foreach (var grid in m_GridGroup2) //{ // if (!grid.alwayShow && grid.gameObject.activeSelf) // { // return true; // } //} return true; } //private void LateUpdate() //{ // var alwayShow = NewBieCenter.Instance.IsAreaNeedUnflod(2) || FunctionalGuideCenter.Instance.IsAreaNeedUnflod(2); // if (!alwayShow) // { // if (state == State.Opened) // { // closeTimer -= Time.deltaTime; // if (closeTimer <= 0f) // { // Switch(false); // } // } // } //} private void DisplayArrow(State state) { m_ArrowOpen.SetActive(state == State.Opened); m_ArrowClose.SetActive(state == State.Closed); } int CountOpenedFunction(MainWinTopGrid[] grids) { var count = 0; foreach (var grid in grids) { var active = grid.IsOpen(); count += active ? 1 : 0; } return count; } void ActiveGroup(MainWinTopGrid[] grids) { foreach (var grid in grids) { var active = grid.IsSwitchOpenShow(); if (active && !grid.gameObject.activeSelf) { grid.SetActive(true); } if (grid.gameObject.activeSelf && (grid.alwayShow || openedFunctionCount <= ALWAYSHOW_THRESHOLD)) { grid.SwitchImmediately(true, 0); } if (!active && grid.gameObject.activeSelf) { grid.SetActive(false); } } } void LayoutGroup(MainWinTopGrid[] grids, RectTransform container) { var index = 0; foreach (var grid in grids) { if (grid.gameObject.activeSelf) { var row = index / LineNume; var column = index % LineNume; var x = container.rect.width * 0.5f - m_GridSize.x * (column + 0.5f) - m_Space.x * column; var y = container.rect.height * 0.5f - m_GridSize.y * (row + 0.5f) - m_Space.y * row; grid.rectTransform.anchoredPosition = new Vector2(x, y); index++; } } foreach (var grid in grids) { if (grid.gameObject.activeSelf) { grid.transform.SetAsFirstSibling(); } } } [ContextMenu("Layout")] public void LayoutGroup() { LayoutGroup(m_GridGroup1, m_GroupContainer1); LayoutGroup(m_GridGroup2, m_GroupContainer2); } public enum State { Opened, Closed, } public static bool IsSwitchOpen() { return HighSettingFadeInFadeOut.state == State.Opened; } } }