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

cs 复制代码
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.cs
        DebugUtility.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 died
            if (m_Player.isDead)
                EndGame(false);
        }
    }

    void EndGame(bool win)
    {
        // unlocks the cursor before leaving the scene, to be able to click buttons
        Cursor.lockState = CursorLockMode.None;
        Cursor.visible = true;

        // Remember that we need to load the appropriate end scene after a delay
        gameIsEnding = true;
        endGameFadeCanvasGroup.gameObject.SetActive(true);
        if (win)
        {
            m_SceneToLoad = winSceneName;
            m_TimeLoadEndGameScene = Time.time + endSceneLoadDelay + delayBeforeFadeToBlack;

            // play a sound on win
            var 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 message
            var message = Instantiate(WinGameMessagePrefab).GetComponent<DisplayMessage>();
            if (message)
            {
                message.delayBeforeShowing = delayBeforeWinMessage;
                message.GetComponent<Transform>().SetAsLastSibling();
            }
        }
        else
        {
            m_SceneToLoad = loseSceneName;
            m_TimeLoadEndGameScene = Time.time + endSceneLoadDelay;
        }
    }
}
相关推荐
野奔在山外的猫1 小时前
【案例】Unity 平台访问文件浏览器(汇总)
unity
_李小白2 小时前
【OPENGL ES 3.0 学习笔记】第九天:缓存、顶点和顶点数组
笔记·学习·elasticsearch
洛白白3 小时前
Word文档中打勾和打叉的三种方法
经验分享·学习·word·生活·学习方法
楼田莉子4 小时前
C++学习:C++11关于类型的处理
开发语言·c++·后端·学习
酷讯网络_2408701604 小时前
PHP双轨直销企业会员管理系统/购物直推系统/支持人脉网络分销系统源码
学习·开源
xwz小王子5 小时前
面向机器人学习的低成本、高效且拟人化手部的设计与制作
人工智能·学习·机器人
半夏知半秋5 小时前
游戏登录方案中常见的设计模式整理
服务器·开发语言·笔记·学习·游戏·设计模式·lua
知识分享小能手6 小时前
uni-app 入门学习教程,从入门到精通, uni-app常用API的详细语法知识点(上)(5)
前端·javascript·vue.js·学习·微信小程序·小程序·uni-app
电子云与长程纠缠6 小时前
UE5 C++ CVar控制台命令字段使用
c++·学习·ue5
Bathwind-w6 小时前
直流电机编码器测速
学习