using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace WildAttack { /// /// UI管理类 /// public class UIManager : Singleton { #region Members public Transform uiRoot; private Transform hpBarRoot; private Transform flotageRoot; private MainPanel mainPanel; private GameOverPanel gameOverPanel; private HPBarPanel hpBarPanel; private CrossHair crossHair; public CanvasScaler uiRootCanvasScaler; UserGameAnalyse1 userGameAnalyse1; #endregion #region Functions public void Init(UserGameAnalyse1 _userGameAnalyse1,Action callBack) { userGameAnalyse1 = _userGameAnalyse1; uiRoot = GameObject.Find("UIRoot").transform; uiRootCanvasScaler = uiRoot.GetComponent(); //ScaleWithScreen(); hpBarRoot = GameObject.Find("HpBarRoot").transform; flotageRoot = GameObject.Find("FlotageRoot").transform; mainPanel = GameObject.Instantiate(Resources.Load("UI/MainPanel"), uiRoot).GetComponent(); mainPanel.onQuitCallback += onShowResultView; gameOverPanel = GameObject.Instantiate(Resources.Load("UI/GameOverPanel"), uiRoot).GetComponent(); hpBarPanel = GameObject.Instantiate(Resources.Load("UI/HPBarPanel"), hpBarRoot).GetComponent(); crossHair = GameObject.Instantiate(Resources.Load("UI/CrossHairPanel"), uiRoot).GetComponentInChildren(); GameObject.Instantiate(Resources.Load("UI/FlotagePanel"), flotageRoot); gameOverPanel.gameObject.SetActive(false); crossHair.gameObject.SetActive(false); callBack(); } /** * 点击结束按钮再触发 */ void onShowResultView() { //退出按钮时候上传一次分数 GameMananger.GetInstance().OnUploadScore(); //结束游戏页面 userGameAnalyse1.showResultView(() => { UnityEngine.SceneManagement.SceneManager.LoadScene("Home", UnityEngine.SceneManagement.LoadSceneMode.Single); }); } /// /// 计算canvas缩放 uiScaleMode为constantPixelSize时 /// public void ScaleWithScreen() { float w = Screen.width / 1280f; float h = Screen.height / 720f; if (w < h) uiRootCanvasScaler.scaleFactor = w; else uiRootCanvasScaler.scaleFactor = h; } /// /// 开始游戏和重开对应ui /// /// public void SetDefaultProcess(bool active) { // active mainPanel.SetProcessInfoActive(active); hpBarPanel.SetProcessInfoActive(active); } /// /// 游戏结束 /// /// public void GameOver(bool isWin) { if (!gameOverPanel.gameObject.activeSelf) { gameOverPanel.gameObject.SetActive(true); crossHair.gameObject.SetActive(false); gameOverPanel.ShowGameOverPanel(isWin); //GameMananger.GetInstance().ResetAim(); //crossHair.ResetCrossHair(); } } public void HideMainPanel() { mainPanel.gameObject.SetActive(false); hpBarPanel.gameObject.SetActive(false); } public void Restart() { mainPanel.gameObject.SetActive(true); mainPanel.GetComponent().RenderUserInfo(); hpBarPanel.gameObject.SetActive(true); gameOverPanel.gameObject.SetActive(false); crossHair.gameObject.SetActive(false); } #region HpBar /// /// 血条相关 /// public void AddHp() { hpBarPanel.AddHp(); } public void SubHp() { hpBarPanel.SubHp(); } public void ResetHpBar() { hpBarPanel.ResetHpBar(); } #endregion /// /// 计算canvas缩放 uiScaleMode为ScaleWithScreenSize时 /// private float ScaleFactor => Screen.width / uiRootCanvasScaler.referenceResolution.x * (1 - uiRootCanvasScaler.matchWidthOrHeight) + Screen.height / uiRootCanvasScaler.referenceResolution.y * uiRootCanvasScaler.matchWidthOrHeight; /// /// 获取准星位置 /// /// public Vector2 GetCrossHairPos() { return crossHair._rectTransform.anchoredPosition * ScaleFactor; } #endregion } }