• 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;不是很理解,待续

    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. public class GameFlowManager : MonoBehaviour
    4. {
    5. [Header("Parameters")]
    6. [Tooltip("Duration of the fade-to-black at the end of the game")]
    7. public float endSceneLoadDelay = 3f;
    8. [Tooltip("The canvas group of the fade-to-black screen")]
    9. public CanvasGroup endGameFadeCanvasGroup;
    10. [Header("Win")]
    11. [Tooltip("This string has to be the name of the scene you want to load when winning")]
    12. public string winSceneName = "WinScene";
    13. [Tooltip("Duration of delay before the fade-to-black, if winning")]
    14. public float delayBeforeFadeToBlack = 4f;
    15. [Tooltip("Duration of delay before the win message")]
    16. public float delayBeforeWinMessage = 2f;
    17. [Tooltip("Sound played on win")]
    18. public AudioClip victorySound;
    19. [Tooltip("Prefab for the win game message")]
    20. public GameObject WinGameMessagePrefab;
    21. [Header("Lose")]
    22. [Tooltip("This string has to be the name of the scene you want to load when losing")]
    23. public string loseSceneName = "LoseScene";
    24. public bool gameIsEnding { get; private set; }
    25. PlayerCharacterController m_Player;
    26. NotificationHUDManager m_NotificationHUDManager;
    27. ObjectiveManager m_ObjectiveManager;
    28. float m_TimeLoadEndGameScene;
    29. string m_SceneToLoad;
    30. void Start()
    31. {
    32. Debug.Log("Start Game: " + m_Player);
    33. //返回场景中的所填类型的随机个体
    34. m_Player = FindObjectOfType();
    35. //DebugUtility.cs不是Unity中的内置脚本,存在Assets/FPS/Scripts/DebugUtility.cs
    36. DebugUtility.HandleErrorIfNullFindObject(m_Player, this);
    37. m_ObjectiveManager = FindObjectOfType();
    38. DebugUtility.HandleErrorIfNullFindObject(m_ObjectiveManager, this);
    39. AudioUtility.SetMasterVolume(1);
    40. }
    41. void Update()
    42. {
    43. if (gameIsEnding)
    44. {
    45. Debug.Log("m_TimeLoadEndGameScene: " + m_TimeLoadEndGameScene);
    46. Debug.Log("Time.time: " + Time.time);
    47. Debug.Log("endSceneLoadDelay: " + endSceneLoadDelay);
    48. float timeRatio = 1 - (m_TimeLoadEndGameScene - Time.time) / endSceneLoadDelay;
    49. Debug.Log("timeRatio: " + timeRatio);
    50. endGameFadeCanvasGroup.alpha = timeRatio;
    51. AudioUtility.SetMasterVolume(1 - timeRatio);
    52. // See if it's time to load the (after the delay)
    53. if (Time.time >= m_TimeLoadEndGameScene)
    54. {
    55. Debug.Log("Scene loading: " + m_SceneToLoad);
    56. SceneManager.LoadScene(m_SceneToLoad);
    57. gameIsEnding = false;
    58. }
    59. }
    60. else
    61. {
    62. if (m_ObjectiveManager.AreAllObjectivesCompleted())
    63. EndGame(true);
    64. // Test if player died
    65. if (m_Player.isDead)
    66. EndGame(false);
    67. }
    68. }
    69. void EndGame(bool win)
    70. {
    71. // unlocks the cursor before leaving the scene, to be able to click buttons
    72. Cursor.lockState = CursorLockMode.None;
    73. Cursor.visible = true;
    74. // Remember that we need to load the appropriate end scene after a delay
    75. gameIsEnding = true;
    76. endGameFadeCanvasGroup.gameObject.SetActive(true);
    77. if (win)
    78. {
    79. m_SceneToLoad = winSceneName;
    80. m_TimeLoadEndGameScene = Time.time + endSceneLoadDelay + delayBeforeFadeToBlack;
    81. // play a sound on win
    82. var audioSource = gameObject.AddComponent();
    83. audioSource.clip = victorySound;
    84. audioSource.playOnAwake = false;
    85. audioSource.outputAudioMixerGroup = AudioUtility.GetAudioGroup(AudioUtility.AudioGroups.HUDVictory);
    86. audioSource.PlayScheduled(AudioSettings.dspTime + delayBeforeWinMessage);
    87. // create a game message
    88. var message = Instantiate(WinGameMessagePrefab).GetComponent();
    89. if (message)
    90. {
    91. message.delayBeforeShowing = delayBeforeWinMessage;
    92. message.GetComponent().SetAsLastSibling();
    93. }
    94. }
    95. else
    96. {
    97. m_SceneToLoad = loseSceneName;
    98. m_TimeLoadEndGameScene = Time.time + endSceneLoadDelay;
    99. }
    100. }
    101. }

  • 相关阅读:
    Web集群_02
    个人博客小程序
    ZZULIOJ 2066: 带分数
    python枚举详解
    好消息!想入深户的伙伴们赶紧看过来!
    动态内存管理(malloc calloc realloc free)--- C语言
    【 构建maven工程时,配置了阿里云的前提下,依旧使用中央仓库下载依赖导致失败的问题!】
    Linux学习——标准IO的读写
    Java继承中的属性名相同但是类型不同的情况
    操作系统·进程管理
  • 原文地址:https://blog.csdn.net/qq_33673213/article/details/132940576