1091020 自由的上一頁按鈕
by 黃國哲
2020-10-20 21:22:25, 回應(0), 人氣(498)

UI 流程管理腳本
這個 Script 主要工作將其列出如下:
1. 建立一個列表來紀錄 UI 畫面歷程,用來將曾經進入的 UI 畫面依序記錄下來,以方便依序返回。
2. 先將第一個開啟的 UI 畫面記錄到歷程中,當歷程只剩下一筆時,就不能再返回。
3. 不可以進入與目前 UI 畫面相同的畫面。
4. 即將要進入或返回的目標畫面必須移到最上層。
5. 往前進入的目標畫面必須記錄到歷程中。
6. 返回時,必須要將目前畫面從歷程紀錄中移除。
在Animation中的Close裡套用下面Script
using UnityEngine;
public class UIStateClosed : StateMachineBehaviour {
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
animator.gameObject.SetActive(false);
}
}
在各個按鈕套用下面Script
using UnityEngine;
using System.Collections.Generic;
public class UIManager : MonoBehaviour {
public GameObject startScreen;
public string outTrigger;
private List<GameObject> screenHistory;
void Awake(){
this.screenHistory = new List<GameObject>{this.startScreen};
}
public void ToScreen(GameObject target){
GameObject current = this.screenHistory[this.screenHistory.Count - 1];
if(target == null || target == current) return;
this.PlayScreen(current , target , false , this.screenHistory.Count);
this.screenHistory.Add(target);
}
public void GoBack(){
if(this.screenHistory.Count > 1){
int currentIndex = this.screenHistory.Count - 1;
this.PlayScreen(this.screenHistory[currentIndex] , this.screenHistory[currentIndex - 1] , true , currentIndex - 2);
this.screenHistory.RemoveAt(currentIndex);
}
}
private void PlayScreen(GameObject current , GameObject target , bool isBack , int order){
current.GetComponent<Animator>().SetTrigger(this.outTrigger);
if(isBack){
current.GetComponent<Canvas>().sortingOrder = order;
}else{
current.GetComponent<Canvas>().sortingOrder = order - 1;
target.GetComponent<Canvas>().sortingOrder = order;
}
target.SetActive(true);
}
}










回應