177 lines
5.5 KiB
C#
177 lines
5.5 KiB
C#
using UnityEngine;
|
||
using Logic;
|
||
using RuntimeData;
|
||
using Animancer;
|
||
using TMPro;
|
||
using UnityEngine.UI;
|
||
using Logic.Audio;
|
||
using TH1Resource;
|
||
|
||
public class MainUI
|
||
{
|
||
private Main _main;
|
||
private MapData _mapData;
|
||
public GameObject ROMainUI;
|
||
|
||
public Button NewGameButton;
|
||
public Button ResumeGameButton;
|
||
public Button StoryModeButton;
|
||
public Button LibraryButton;
|
||
public Button SettingButton;
|
||
public Button HistoryButton;
|
||
public Button AboutButton;
|
||
|
||
public bool NeedShow = false;
|
||
|
||
private bool _isShowing = false;
|
||
private bool _isAnimating = false;
|
||
private float _fadeDuration = 0.2f;
|
||
|
||
public MainUI(Main main, MapData mapData)
|
||
{
|
||
_main = main;
|
||
_mapData = mapData;
|
||
ROMainUI = _main.UIManager.ROUIManager.transform.Find("GameUI/MainUI").gameObject;
|
||
ROMainUI.gameObject.SetActive(false);
|
||
|
||
// 绑定所有按钮
|
||
NewGameButton = ROMainUI.transform.Find("ButtonRow/NewGameButton/MainUIButton").GetComponent<Button>();
|
||
ResumeGameButton = ROMainUI.transform.Find("ButtonRow/ResumeGameButton/MainUIButton").GetComponent<Button>();
|
||
StoryModeButton = ROMainUI.transform.Find("ButtonRow/StoryModeButton/MainUIButton").GetComponent<Button>();
|
||
LibraryButton = ROMainUI.transform.Find("ButtonRow/LibraryButton/MainUIButton").GetComponent<Button>();
|
||
HistoryButton = ROMainUI.transform.Find("ButtonRow/HistoryButton/MainUIButton").GetComponent<Button>();
|
||
|
||
SettingButton = ROMainUI.transform.Find("ButtonRow2/SettingButton").GetComponent<Button>();
|
||
AboutButton = ROMainUI.transform.Find("ButtonRow2/AboutButton").GetComponent<Button>();
|
||
|
||
NewGameButton.onClick.AddListener(OnNewGameClicked);
|
||
ResumeGameButton.onClick.AddListener(OnResumeGameClicked);
|
||
StoryModeButton.onClick.AddListener(OnStoryModeClicked);
|
||
LibraryButton.onClick.AddListener(OnLibraryClicked);
|
||
SettingButton.onClick.AddListener(OnSettingClicked);
|
||
HistoryButton.onClick.AddListener(OnHistoryClicked);
|
||
AboutButton.onClick.AddListener(OnAboutClicked);
|
||
|
||
//如果有存当,显示"继续"这个按钮
|
||
ROMainUI.transform.Find("ButtonRow").Find("ResumeGameButton").gameObject.SetActive(_main.HasArchive());
|
||
|
||
}
|
||
|
||
public void Update()
|
||
{
|
||
if (_isAnimating) return;
|
||
|
||
if (NeedShow && !ROMainUI.activeSelf)
|
||
{
|
||
Show();
|
||
}
|
||
else if (!NeedShow && ROMainUI.activeSelf)
|
||
{
|
||
Hide();
|
||
}
|
||
}
|
||
|
||
public void Show()
|
||
{
|
||
if (_isShowing || _isAnimating) return;
|
||
|
||
AudioManager.Instance.PlayMusic("Main",0.3f,0.3f,true);
|
||
_isShowing = true;
|
||
_isAnimating = true;
|
||
ROMainUI.SetActive(true);
|
||
|
||
AnimancerComponent animancer = ROMainUI.GetComponent<AnimancerComponent>();
|
||
AnimationClip fadeInClip = Resources.Load<AnimationClip>("Animations/UI/SettingPanelFadeIn");
|
||
if (fadeInClip != null)
|
||
animancer.Play(fadeInClip);
|
||
|
||
Timer.Instance.TimerRegister(ROMainUI, () => { _isAnimating = false; }, _fadeDuration);
|
||
}
|
||
|
||
public void Hide()
|
||
{
|
||
if (!_isShowing || _isAnimating) return;
|
||
|
||
_isShowing = false;
|
||
_isAnimating = true;
|
||
|
||
AnimancerComponent animancer = ROMainUI.GetComponent<AnimancerComponent>();
|
||
AnimationClip fadeOutClip = Resources.Load<AnimationClip>("Animations/UI/SettingPanelFadeOut");
|
||
if (fadeOutClip != null)
|
||
animancer.Play(fadeOutClip);
|
||
|
||
Timer.Instance.TimerRegister(ROMainUI, () =>
|
||
{
|
||
ROMainUI.SetActive(false);
|
||
_isAnimating = false;
|
||
}, _fadeDuration);
|
||
}
|
||
|
||
private void OnNewGameClicked()
|
||
{
|
||
NeedShow = false;
|
||
_main.UIManager.GameUI.ChooseUI.NeedShow = true;
|
||
}
|
||
|
||
private void OnResumeGameClicked()
|
||
{
|
||
//Debug.Log("Resume Game Clicked");
|
||
if (_main.HasArchive())
|
||
{
|
||
ShowLoadingAndResumeGame();
|
||
}
|
||
//NeedShow = false;
|
||
}
|
||
|
||
public void ShowLoadingAndResumeGame()
|
||
{
|
||
|
||
float loadingTime = 2f;
|
||
var loading = _main.UIManager.ROUIManager.transform.Find("LoadingUI");
|
||
var loadingAnim = loading.GetComponent<AnimancerComponent>();
|
||
if (loadingAnim == null)
|
||
{
|
||
_main.ContinueGame();
|
||
return;
|
||
}
|
||
//先播放loading界面,然后加载游戏,再关掉loading界面
|
||
loading.gameObject.SetActive(true);
|
||
loadingAnim.Play(ResourceCache.Instance.AnimCache.UICommonPanelFadeIn);
|
||
Timer.Instance.TimerRegister(this,() => {
|
||
_main.ContinueGame();
|
||
NeedShow = false;
|
||
},0.5f);
|
||
|
||
//播放loading消失的动画
|
||
Timer.Instance.TimerRegister(loading,()=> { loadingAnim.Play(ResourceCache.Instance.AnimCache.UICommonPanelFadeOut); },loadingTime);
|
||
//将loading设置为false
|
||
Timer.Instance.TimerRegister(loading,()=> { loading.gameObject.SetActive(false); },loadingTime + 0.2f);
|
||
}
|
||
|
||
private void OnStoryModeClicked()
|
||
{
|
||
Debug.Log("Story Mode Clicked");
|
||
}
|
||
|
||
private void OnLibraryClicked()
|
||
{
|
||
NeedShow = false;
|
||
_main.UIManager.GameUI.LibraryUI.NeedShow = true;
|
||
}
|
||
|
||
private void OnSettingClicked()
|
||
{
|
||
_main.UIManager.SettingUI.NeedShow = true;
|
||
}
|
||
|
||
private void OnHistoryClicked()
|
||
{
|
||
_main.UIManager.GameUI.HistoryUI.NeedShow = true;
|
||
}
|
||
|
||
private void OnAboutClicked()
|
||
{
|
||
_main.UIManager.GameUI.AboutUI.NeedShow = true;
|
||
}
|
||
}
|