本章节制作了屏幕的淡入淡出效果
源代码
UI_FadeScreen.cs
using UnityEngine;
public class UI_FadeScreen : MonoBehaviour
{
private Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
public void FadeOut() => anim.SetTrigger("fadeOut");
public void FadeIn() => anim.SetTrigger("fadeIn");
}
UI_MainMenu.cs
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class UI_MainMenu : MonoBehaviour
{
[SerializeField] private string sceneName = "MainScene"; //加载场景名称
[SerializeField] private GameObject continueButton;
[SerializeField] UI_FadeScreen fadeScreen;
private void Start()
{
//若开始时未找到存档,则将继续游戏按钮隐藏
if (!SaveManager.instance.HasSaveData())
continueButton.SetActive(false);
}
//继续游戏
public void ContinueGame()
{
StartCoroutine(LoadScreenWithFadeEffect(1.5f));
}
//创建新游戏
public void NewGame()
{
SaveManager.instance.DeleteSavaData();
StartCoroutine(LoadScreenWithFadeEffect(1.5f));
}
//退出
public void ExitGame()
{
Application.Quit();
}
//载入游戏黑屏效果
IEnumerator LoadScreenWithFadeEffect(float _delay)
{
fadeScreen.FadeOut();
yield return new WaitForSeconds(_delay);
SceneManager.LoadScene(sceneName);
}
}
UI.cs
using System.Collections;
using UnityEngine;
public class UI : MonoBehaviour
{
[Header("结束屏幕")]
[SerializeField] private UI_FadeScreen fadeScreen;
[SerializeField] private GameObject endText;
[Space]
[SerializeField] private GameObject charcaterUI;
[SerializeField] private GameObject skillTreeUI;
[SerializeField] private GameObject craftUI;
[SerializeField] private GameObject optionsUI;
[SerializeField] private GameObject inGameUI;
public UI_ItemTooltip itemTooltip;
public UI_StatTooltip statTooltip;
public UI_SkillToolTip skillTooltip;
public UI_CraftWindow craftWindow;
private void Awake()
{
SwitchTo(skillTreeUI);//我们需要在将事件分配到技能脚本之前将其分配到技能树槽
}
void Start()
{
SwitchTo(inGameUI);
itemTooltip.gameObject.SetActive(false);
statTooltip.gameObject.SetActive(false);
skillTooltip.gameObject.SetActive(false);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.C))
SwitchWithKeyTo(charcaterUI); //打开角色界面
if (Input.GetKeyDown(KeyCode.B))
SwitchWithKeyTo(craftUI); //打开工艺界面
if (Input.GetKeyDown(KeyCode.K))
SwitchWithKeyTo(skillTreeUI); //打开技能树界面
if (Input.GetKeyDown(KeyCode.O))
SwitchWithKeyTo(optionsUI); //打开设置界面
if (Input.GetKeyDown(KeyCode.Escape))
SwitchWithKeyTo(inGameUI); //关闭所有界面
}
public void SwitchTo(GameObject _menu)
{
for (int i = 0; i < transform.childCount; i++)
{
//确保黑屏被激活
bool fadeScreen = transform.GetChild(i).GetComponent<UI_FadeScreen>() != null;
if (!fadeScreen)
transform.GetChild(i).gameObject.SetActive(false);
}
if (_menu != null)
_menu.SetActive(true);
}
public void SwitchWithKeyTo(GameObject _menu)
{
if (_menu != null && _menu.activeSelf)
{
//如果界面已打开,则关闭当前界面
_menu.SetActive(false);
CheckForInGameUI();
return;
}
SwitchTo(_menu);
}
private void CheckForInGameUI()
{
for (int i = 0; i < transform.childCount; i++)
{
if (transform.GetChild(i).gameObject.activeSelf)
return;
}
SwitchTo(inGameUI);
}
public void SwichOnEndScreen()
{
fadeScreen.FadeOut();
StartCoroutine(EndScreenCorutine());
}
IEnumerator EndScreenCorutine()
{
yield return new WaitForSeconds(1);
endText.SetActive(true);
}
}
PlayerDeadState.cs
using UnityEngine;
public class PlayerDeadState : PlayerState
{
public PlayerDeadState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
{
}
public override void AnimationFinishTrigger()
{
base.AnimationFinishTrigger();
}
public override void Enter()
{
base.Enter();
GameObject.Find("Canvas").GetComponent<UI>().SwichOnEndScreen();
}
public override void Exit()
{
base.Exit();
}
public override void Update()
{
base.Update();
player.SetZeroVelocity();
}
}