This should be the last article.
The previous content is through the call MySceneManager, state (scene) switch, that is, the core is in him, all of the content is around him. Control the center MySceneManager, control the switch of the whole scene.
It’s the same with the UI. The only special thing is the LoadingPanel handling, which is a little bit different. More on that later.
Other interfaces are handled in exactly the same way as states. But consider that there are many kinds of UIs, including full-screen ones (also divided into transparent and opaque ones) and half-screen ones. The two differences are that it shows the FULL screen UI, you don’t have to show the rest of the UI, but the transparent UI, or the Tips UI, you can still see the rest of the UI, which is a hierarchy issue. ! [For example, the Login interface behind the registration interface can still be seen, so Login cannot be closed or hidden](
It’s up to you to decide what to do, but I use enumerations as tokens;
Management first:
First, UI management
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; public class UIManager : MonoBehaviour { private static Transform canvas; public static Transform Canvas { get { if (canvas == null) { canvas= GameObject.FindWithTag("Canvas").transform; } return canvas; }} /// <summary> /// already loaded Panel; /// </summary> private static Dictionary<PanelNameEnum, BasePanel> m_panelDic = new Dictionary<PanelNameEnum, BasePanel>(); Public static BasePanel LoadPanel(PanelNameEnum panelEnum, PanelNameEnum prePanel) { BasePanel target = null; string pName = Enum.GetName(typeof(PanelNameEnum), panelEnum); if (! M_paneldid.trygetvalue (panelEnum,out target)) {// Load AB package with the name uiprefabs (self-defined), and AB package resources (want to load Panel); GameObject t= ABManager.Instance.LoadAssets<GameObject>("uiprefabs", pName); GameObject temp = Instantiate(t, Canvas, false); temp.name = pName; target= temp.GetComponent<BasePanel>(); m_panelDic.Add(panelEnum, target); }; // According to its enumeration type, determine whether the current previous panel needs to be hidden; if(prePanel! =PanelNameEnum.None) SwitchEnumPanel(target.ScreenEnu, m_panelDic[prePanel]); target.gameObject.SetActive(true); target.transform.SetParent(Canvas,false); target.transform.SetAsLastSibling(); return target; } //Loading is dedicated; Transfer data or transfer method to be supplemented later; public static BasePanel LoadPanel(string title=null,string content=null,string imageName=null) { BasePanel target=null; string pName = Enum.GetName(typeof(PanelNameEnum), PanelNameEnum.LoadingPanel); if (! m_panelDic.TryGetValue(PanelNameEnum.LoadingPanel, out target)) { GameObject t = ABManager.Instance.LoadAssets<GameObject>("uiprefabs", pName); GameObject temp = Instantiate(t, Canvas, false); temp.name = pName; target = temp.GetComponent<BasePanel>(); m_panelDic.Add(PanelNameEnum.LoadingPanel, target); }; target.gameObject.SetActive(true); target.transform.SetParent(Canvas, false); target.transform.SetAsLastSibling(); if(target is LoadingPanel) { (target as LoadingPanel).ShowContent(title, content, imageName); } return target; } // The parameter panel is about to open. private static void SwitchEnumPanel(PanelScreenEnum panel,BasePanel panelName) { switch (panel) { case PanelScreenEnum.FullScreen: panelName.ISShow(false); break; case PanelScreenEnum.HalfScreen: panelName.ISShow(true); break; case PanelScreenEnum.Tips: panelName.ISShow(true); break; default: break; } } public static void DestoryPanel(PanelNameEnum panelEnum) { if (m_panelDic.ContainsKey(panelEnum)) { BasePanel temp = m_panelDic[panelEnum]; m_panelDic.Remove(panelEnum); Destroy(temp.gameObject); } else { MDebug.LogError($"Please Check {panelEnum},Don't Destory!!!" ); } } public static void HideTargetPanel(PanelNameEnum panelEnum) { BasePanel target = null; if (m_panelDic.TryGetValue(panelEnum, out target)) { target.ISShow(false); } } public static void HideAllPanel() { if (m_panelDic == null) { MDebug.LogError("panelDic is null, please Check!!!" ); } foreach (var item in m_panelDic.Values) { item.ISShow(false); }} /// <summary> /// /// </summary> public static void ClearPanelDic() { foreach (var item in m_panelDic.Values) { Destroy(item.obj); } m_panelDic.Clear(); GC.Collect(); }}Copy the code
The base class of UI
Public abstract class BasePanel: MonoBehaviour {// Indicates whether the UIPanel is full-screen or otherwise; protected PanelScreenEnum screenEnu = PanelScreenEnum.HalfScreen; // Enumerations of names are actually either loaded directly through the name string or loaded through enumerations, depending on how UIManager defines them. It's just my enumeration. protected PanelNameEnum nameEnum; Public void Awake() {public void Awake(); InitData(); } public void OnUpdate() {// Update UI logic; UpdateUI(); } public GameObject obj { get { return gameObject; } } public PanelScreenEnum ScreenEnu { get => screenEnu; } public PanelNameEnum NameEnum { get => nameEnum; } public void ISShow(bool isShow) { obj.SetActive(isShow); } protected virtual void UpdateUI() { } protected abstract void InitData(); }Copy the code
3. Separate out the Loading interface
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System; public class LoadingPanel : BasePanel { [SerializeField] private Image m_Bg; public Slider m_Slider; public Text m_title_text; public Text m_Content; public Text showSliderValue_text; protected override void InitData() { this.screenEnu = PanelScreenEnum.FullScreen; this.nameEnum = PanelNameEnum.LoadingPanel; MessageManager.AddListener("loading", DoValueSlider); } public void OnDestroy() { MessageManager.RemoveFunc("loaidng"); } public void DoValueSlider(float num) { m_Slider.value = num; Mdebug. Log("Loading interface progress value m_slider. value:" + m_slider.value); If (num >= 0.8999) {m_slider. value = 1; } showSliderValue_text.text = (m_Slider.value * 100).ToString() + "%"; } public void ShowContent(string title = null, string content = null, string imageName = null) { if (title ! = null && content ! = null) m_title_text.text = title; m_Content.text = content; if (imageName ! = null && imageName! =string.Empty) { MDebug.Log(imageName ! = string.Empty); MDebug.Log(imageName! =null); m_Bg = ABManager.Instance.LoadAssets<Image>("iamgebg", imageName); }}}Copy the code
Note: MessageManager AddListener (” loading “, DoValueSlider); I registered this message in the message box as soon as the screen opened, where is it executed? Remember in the scenario management class? Enter each scene, the loading interface is loaded through UIManager first, so this delegate is registered to MessageManager this thunder Mid-Autumn U. Then, through MySceneManager executes the LoadSceneMsg management class in the asynchronous loading scenario method, in this method, executes messagemanager.dofunc (” loading “, async.progress); The previously registered delegate DoValueSlider is executed by passing in two parameters. When the load is complete, I remove the method again. If you destroy the interface, the method is automatically removed.
While (async. Progress < 0.9 f)
{
MDebug. Log (” async. Progress “+ async. Progress);
MessageManager. DoFunc (” loading “, async. Progress);
yield return null;
}
At this point, the framework is complete. Because some things still don’t make sense, but I’m really tired of changing them… Just work it out.
The following is about music related processing:
Iv. Management of other played music. The same goes for special effects. There’s nothing to say.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MusicManager :AutoSingleton<MusicManager> { private AudioClip buttonClip; private AudioClip bgClip; private AudioClip otherClip; private GameObject audioSource; private AudioSource buttonAudioSource; private AudioSource bgAudioSource; private AudioSource otherAudioSource; private void Awake() { CreatAudioSource(); PlayBGAudio(true); } private void CreatAudioSource() { if (audioSource == null) { audioSource = new GameObject("AudioSourceComponent"); DontDestroyOnLoad(audioSource); Mdebug. Log(" Create object AudioSource"); } if (buttonAudioSource == null) { GameObject go = new GameObject("buttonAudioSource"); go.transform.SetParent(audioSource.transform); buttonAudioSource = go.AddComponent<AudioSource>(); } if (bgAudioSource == null) { GameObject go = new GameObject("bgAudioSource"); bgAudioSource = go.AddComponent<AudioSource>(); go.transform.SetParent(audioSource.transform); } if (otherAudioSource == null) { GameObject go = new GameObject("otherAudioSource"); otherAudioSource = go.AddComponent<AudioSource>(); go.transform.SetParent(audioSource.transform); } } public void OnMyClickClip() { if (buttonClip == null) buttonClip = ABManager.Instance.LoadAssets<AudioClip>("audio", "buttonClip"); buttonAudioSource.clip = buttonClip; buttonAudioSource.mute = false; buttonAudioSource.loop = false; buttonAudioSource.Play(); } public void PlayBGAudio(bool isPlay) { if (bgClip == null) { bgClip = ABManager.Instance.LoadAssets<AudioClip>("audio", "zanyong_bg"); } bgAudioSource.clip = bgClip; buttonAudioSource.mute = false; buttonAudioSource.loop = true; bgAudioSource.Play(); }}Copy the code
Done!! In the future, I will probably write something about Shader. I will also write something about Ai related FSM, GOAP, automatic pathfinding, etc. I’m going to say but, I don’t have much time (another word for laziness), and I’m mostly studying (which is true) and sleeping. I’m a nerd…
I’ve only been working in game development for two years now, and I’m so new to it that I can’t say anything. At the beginning, I took a lot of detdettions. In the last half year, I was interested in Shader, so I played Shader for a long time. I was interested in writing code, so I didn’t like to play Lianliankan, lianliankan also understand less. This project, the following I posted on the line, originally wanted to write a demo to play, but art resources are not easy to find, and recently there are new learning goals, it ran aground. After a period of time and the appointment of three big men (one to edit, a special effect, a front-end main process), is fooling a small brother, planning to take time to fooling a model over, people together may do a hand play.