日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Unity3D游戏开发——编程实现游戏管理器

發布時間:2023/12/13 综合教程 29 生活家
生活随笔 收集整理的這篇文章主要介紹了 Unity3D游戏开发——编程实现游戏管理器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本篇簡介

本篇介紹了如何將上一篇的設計模式思想運用到實際的開發過程中。

腳本文件

(1)IGameManager:這個接口存在聲明了一個屬性(一個擁有getter函數的變量,屬性的類型是ManagerStatus,一個枚舉)和一個方法,它們都要被任何實現這個接口的類實現。定義了status屬性,可以從任何地方獲取狀態(getter方法是公有方法),但是只能在腳本中設置狀態(setter方法是私有方法).(ps:一個接口本身不會做任何事情,它存在的意義僅僅是將代碼結構約束到其他類身上)
(2)ManagerStatus:這是一個幾乎沒有代碼的文件,列出了管理器所有可能的狀態(限定了status屬性只能是里面列出的狀態之一)。
(3)PlayerManager與InventoryManager:前者為人物管理器,后者為庫存管理器。它們都繼承自類MonoBehaviour且實現了IGameManager定義的結構。
(4)Managers:管理器的管理器。

狀態轉化

在所有的管理器中,初始化(InventoryManager沒有做任何事情,而PlayerManager設置了一些值)一旦完成,status將被設置為Started。
但是數據模塊的初始化中可能有一些長線任務(例如加載所保存的數據),在初始化時,Startup()將運行這些任務,設置管理器的狀態為Initializing(初始化中)。當完成后,再將status轉化為Started。

IGameManger的腳本代碼

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public interface IGameManager 
{
    ManagerStatus status { get; } //這是一個我們需要定義的枚舉

    void Startup();  //這個方法的目的是處理管理器的初始化

}

ManagerStatus的腳本代碼

public enum ManagerStatus
{
    Shutdown,  //關機
    Initializing,  //正在初始化
    Started  //開始啟動
}

PlayerManager的腳本代碼

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

///<summary>
///人物管理腳本
///</summary>

public class PlayerManager : MonoBehaviour, IGameManager    //繼承一個類并實現一個接口
{
    public ManagerStatus status
    {
        get;
        private set;  //status只讀,不能在外部設置,內部可讀寫
    }

    public int health  //人物健康值
    {
        get;
        private set;
    }

    public int maxHealth  //人物最高健康值
    {
        get;
        private set;
    }

    public void Startup()
    {
        Debug.Log("PlayerManager starting ...");
        health = 50;   //初始化值
        maxHealth = 100;

        status = ManagerStatus.Started;
    }

    public void ChangeHealth(int value)
    {
        health += value;
        if (health > maxHealth)
        {
            health = maxHealth;
        }
        else if (health < 0)  //健康值最小為0
        {
            health = 0;
        }

        Debug.Log("Health:" + health + "/" + maxHealth);
    }
}

InventoryManager的腳本代碼

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

///<summary>
///庫存管理腳本
///</summary>

public class InventoryManager : MonoBehaviour, IGameManager
{
    public ManagerStatus status  //這個屬性可以從任何地方獲取,但只能在這個腳本中設置
    {
        get;
        private set;
    }

    public void Startup()  
    {
        Debug.Log("InventoryManager starting ...");//任何長任務在這里啟動
        status = ManagerStatus.Started;   //如果是長任務,狀態改變為"Initializing"
    }

}

Managers的腳本代碼

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

///<summary>
///管理器的管理器
///</summary>

[RequireComponent(typeof(PlayerManager))]  //自動添加管理器,確保存在不同的管理器
[RequireComponent(typeof(InventoryManager))]

public class Managers : MonoBehaviour
{
    public static PlayerManager Player //其他代碼用來訪問管理器的靜態屬性
    {
        get;
        private set;
    }
    public static InventoryManager Inventory
    {
        get;
        private set;
    }

    private List<IGameManager> _startSequence;  //啟動時要遍歷的管理器列表

    void Awake()  //列出了啟動序列,然后啟動協同程序來運行所有的管理器
    {
        Player = GetComponent<PlayerManager>();
        Inventory = GetComponent<InventoryManager>();

        _startSequence = new List<IGameManager>();  //創建了List對象,用List.Add的方法來添加管理器。列表對象與數組相似,以一種具體的類型聲明,把一系列元素依次存入,但可在創建后改變大小。
        _startSequence.Add(Player);
        _startSequence.Add(Inventory);

        StartCoroutine(StartupManagers());  //異步啟動序列
    }

    private IEnumerator StartupManagers()
    {
        foreach (IGameManager manager in _startSequence)
        {
            manager.Startup();
        }
        yield return null;

        int numModules = _startSequence.Count;
        int numReady = 0;

        while (numReady < numModules)  //循環至所有管理器都啟動為止
        {
            int lastReady = numReady;
            numReady = 0;

            foreach (IGameManager manager in _startSequence)
            {
                if (manager.status == ManagerStatus.Started)
                {
                    numReady++;
                }
            }
            if (numReady > lastReady)
                Debug.Log("progress:" + numReady + "/" + numModules);
            yield return null;
        }

        Debug.Log("All managers started up");
    }

}

參考資料

協程(協同程序)的概括(StartCoroutine 和yield return和StopCoroutine )
IEnumerator和IEnumerable詳解
《Unity 5實戰——使用C#和Unity開發多平臺游戲》,作者Joseph Hocking ,譯者蔡俊鴻。

總結

以上是生活随笔為你收集整理的Unity3D游戏开发——编程实现游戏管理器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。