当前位置: 首页 > news >正文

unity学习第1天

本身也具有一些unity知识,包括Eidtor界面使用、Shader效果实现、性能分析,但对C#、游戏逻辑不太清楚,这次想从开发者角度理解游戏,提高C#编程,从简单的unity游戏理解游戏逻辑,更好的为工作服务。

unity2019.4.40f1c1,使用Unity官网FPS Microgame(如下图)。原本想从游戏源码启动流程开始学习,但Unity核心代码没有开源,就还是先理解游戏逻辑。

游戏分成 5个Scene,我们关注MainScene,其中有个GameManager,从Inspector看挂有许多Scripts,我们先一个个分析。

 GameFlowManager,引用using UnityEngine.SceneManagement,定义类GameFlowManager前半部分是在自定义Eidtor界面,这里float timeRatio = 1 - (m_TimeLoadEndGameScene - Time.time) / endSceneLoadDelay;不是很理解,待续

using UnityEngine;
using UnityEngine.SceneManagement;public class GameFlowManager : MonoBehaviour
{[Header("Parameters")][Tooltip("Duration of the fade-to-black at the end of the game")]public float endSceneLoadDelay = 3f;[Tooltip("The canvas group of the fade-to-black screen")]public CanvasGroup endGameFadeCanvasGroup;[Header("Win")][Tooltip("This string has to be the name of the scene you want to load when winning")]public string winSceneName = "WinScene";[Tooltip("Duration of delay before the fade-to-black, if winning")]public float delayBeforeFadeToBlack = 4f;[Tooltip("Duration of delay before the win message")]public float delayBeforeWinMessage = 2f;[Tooltip("Sound played on win")]public AudioClip victorySound;[Tooltip("Prefab for the win game message")]public GameObject WinGameMessagePrefab;[Header("Lose")][Tooltip("This string has to be the name of the scene you want to load when losing")]public string loseSceneName = "LoseScene";public bool gameIsEnding { get; private set; }PlayerCharacterController m_Player;NotificationHUDManager m_NotificationHUDManager;ObjectiveManager m_ObjectiveManager;float m_TimeLoadEndGameScene;string m_SceneToLoad;void Start(){   Debug.Log("Start Game: " + m_Player);//返回场景中的所填类型的随机个体m_Player = FindObjectOfType<PlayerCharacterController>();//DebugUtility.cs不是Unity中的内置脚本,存在Assets/FPS/Scripts/DebugUtility.csDebugUtility.HandleErrorIfNullFindObject<PlayerCharacterController, GameFlowManager>(m_Player, this);m_ObjectiveManager = FindObjectOfType<ObjectiveManager>();DebugUtility.HandleErrorIfNullFindObject<ObjectiveManager, GameFlowManager>(m_ObjectiveManager, this);AudioUtility.SetMasterVolume(1);}void Update(){if (gameIsEnding){   Debug.Log("m_TimeLoadEndGameScene: " + m_TimeLoadEndGameScene);Debug.Log("Time.time: " + Time.time);Debug.Log("endSceneLoadDelay: " + endSceneLoadDelay);float timeRatio = 1 - (m_TimeLoadEndGameScene - Time.time) / endSceneLoadDelay;Debug.Log("timeRatio: " + timeRatio);endGameFadeCanvasGroup.alpha = timeRatio;AudioUtility.SetMasterVolume(1 - timeRatio);// See if it's time to load the  (after the delay)if (Time.time >= m_TimeLoadEndGameScene){Debug.Log("Scene loading: " + m_SceneToLoad);SceneManager.LoadScene(m_SceneToLoad);gameIsEnding = false;}}else{if (m_ObjectiveManager.AreAllObjectivesCompleted())EndGame(true);// Test if player diedif (m_Player.isDead)EndGame(false);}}void EndGame(bool win){// unlocks the cursor before leaving the scene, to be able to click buttonsCursor.lockState = CursorLockMode.None;Cursor.visible = true;// Remember that we need to load the appropriate end scene after a delaygameIsEnding = true;endGameFadeCanvasGroup.gameObject.SetActive(true);if (win){m_SceneToLoad = winSceneName;m_TimeLoadEndGameScene = Time.time + endSceneLoadDelay + delayBeforeFadeToBlack;// play a sound on winvar audioSource = gameObject.AddComponent<AudioSource>();audioSource.clip = victorySound;audioSource.playOnAwake = false;audioSource.outputAudioMixerGroup = AudioUtility.GetAudioGroup(AudioUtility.AudioGroups.HUDVictory);audioSource.PlayScheduled(AudioSettings.dspTime + delayBeforeWinMessage);// create a game messagevar message = Instantiate(WinGameMessagePrefab).GetComponent<DisplayMessage>();if (message){message.delayBeforeShowing = delayBeforeWinMessage;message.GetComponent<Transform>().SetAsLastSibling();}}else{m_SceneToLoad = loseSceneName;m_TimeLoadEndGameScene = Time.time + endSceneLoadDelay;}}
}

http://www.lryc.cn/news/169044.html

相关文章:

  • Spring Boot实现对文件进行压缩下载
  • Mac专用投屏工具AirServer 7 .27 for Mac中文免费激活版
  • LabVIEW使用巴特沃兹低通滤波器过滤噪声
  • 【Realtek sdk-3.4.14b】RTL8197FH-VG和RTL8812F自适应认证失败问题分析及修改
  • SpringBoot 的版本、打包、Maven
  • 不同类型程序的句柄研究
  • 【Godot】解决游戏中的孤立/孤儿节点及分析器性能问题的分析处理
  • 国家网络安全宣传周知识竞赛活动小程序界面分享
  • mysql的判断语句
  • ArcGIS Maps SDK for JavaScript系列之四:添加自定义底图
  • Learn Prompt-角色扮演
  • 《动手学深度学习 Pytorch版》 6.1 从全连接层到卷积
  • 六、数学建模之插值与拟合
  • 【项目经验】:elementui表格中数字汉字排序问题及字符串方法localeCompare()
  • Spring Boot的运行原理
  • xen-gic初始化流程
  • Docker从认识到实践再到底层原理(六-1)|Docker容器基本介绍+命令详解
  • 【Flink】FlinkCDC自定义反序列化器
  • linux基础(2)
  • docker安装zookeeper(单机版)
  • 国际版阿里云/腾讯云免开户:云存储服务:云存储服务能够让你随时随地拜访和同享文件
  • 【Java】应用层协议HTTP和HTTPS
  • SpringBoot整合Flowable
  • 华为云香港S3云服务器性能测评_99元一年租用价格
  • prompt 视频收集
  • Rust :与C交互
  • 模拟实现C语言--memcpy函数和memmove函数
  • Linux目录
  • 全国职业技能大赛云计算--高职组赛题卷①(私有云)
  • STM32--PWR电源控制