最近在学习UI框架,无奈没有完整的项目学习,四处搜索找了这款游戏源码,在Unity2018上完美运行。于是乎开始学习开发这款游戏。今天主要完成的任务时拼UI。搭建了3个场景, StartScene, LoadingScene, MainScene。PlayScene比较复杂,包含了复杂的逻辑,放在最后学习。
1.StartScene
这个场景比较简单,主要包括3个部分:背景,Logo,开始按钮。
逻辑:点击开始按钮,进入LoadingScene。
在UICamera或者Canvas(StartScene)物体上挂载脚本StartMgr.cs
1 using UnityEngine; 2 using UnityEngine.SceneManagement; 3 using UnityEngine.UI; 4 public class StartMgr : MonoBehaviour 5 { 6 public Button btnStart; 7 private void Start() 8 { 9 if(btnStart != null) 10 { 11 //给按钮添加监听事件 12 btnStart.onClick.AddListener(OnLoadLevel); 13 } 14 } 15 16 private void OnLoadLevel() 17 { 18 SceneManager.LoadScene(1); 19 } 20 21 }
2.LoadingScene
这个场景包含的主要元素是:背景、Logo、齿轮(含旋转的动画),进度条。
加载游戏用到了异步加载技术,在LoadScene物体上挂载脚本LoadScene.cs。
1 using System.Collections; 2 using UnityEngine; 3 using UnityEngine.SceneManagement; 4 using UnityEngine.UI; 5 using System.Collections.Generic; 6 public class LoadScene : MonoBehaviour 7 { 8 public Slider progressBar; 9 //当前加载进度 10 private float currentProgress = 0; 11 //目标加载进度 12 private float targetProgress = 0; 13 14 private void Start() 15 { 16 //启动协程 17 StartCoroutine("LoadingScene"); 18 } 19 20 private IEnumerator LoadingScene() 21 { 22 //异步加载 23 AsyncOperation asyncOp = SceneManager.LoadSceneAsync(2); 24 //没有加载完毕时 25 while (asyncOp.progress < 0.9f) 26 { 27 Debug.Log("正在疯狂加载中..."); 28 currentProgress = asyncOp.progress; 29 yield return UpdateProgressBarValue(); 30 } 31 currentProgress = 1f; 32 yield break; 33 } 34 35 private IEnumerator<WaitForEndOfFrame> UpdateProgressBarValue() 36 { 37 while (targetProgress < currentProgress) 38 { 39 Debug.Log("正在更新进度..."); 40 targetProgress += 0.01f; 41 progressBar.value = targetProgress; 42 yield return new WaitForEndOfFrame(); 43 } 44 yield break; 45 } 46 47 }
3.MainScene
这个场景元素比较多,分为七大块。Main, PersonalInfo,Action,TAsk,Install,Explain
今天完成的逻辑比较简单,就是点击头像打开个人中心面板及其关闭逻辑。代码如下:Person
PersonalController.cs负责个人中心面板逻辑,MainSceneController负责整个场景游戏逻辑。
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.UI; 5 public class MainSceneController : MonoBehaviour 6 { 7 public Button headBtn; 8 public GameObject personalInfo; 9 10 private void Start() 11 { 12 Init(); 13 } 14 15 private void Init() 16 { 17 headBtn.onClick.AddListener(OnHeadBtn); 18 } 19 private void OnHeadBtn() 20 { 21 personalInfo.SetActive(true); 22 } 23 24 }
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.UI; 5 public class PersonalController : MonoBehaviour 6 { 7 public InputField nameInput; 8 public InputField mottoInput; 9 public Button sureBtn; 10 public Button closeBtn; 11 12 13 private void Start() 14 { 15 Init(); 16 } 17 18 private void Init() 19 { 20 sureBtn.onClick.AddListener(OnSureBtn); 21 closeBtn.onClick.AddListener(OnCloseBtn); 22 } 23 private void OnSureBtn() 24 { 25 PlayerPrefs.SetString("name", nameInput.text); 26 PlayerPrefs.SetString("motto", mottoInput.text); 27 OnCloseBtn(); 28 } 29 30 private void OnCloseBtn() 31 { 32 gameObject.SetActive(false); 33 } 34 }
原文地址:https://www.cnblogs.com/blackteeth/p/10203287.html
时间: 2024-10-16 21:14:07