Unity的C#編程教程_56_Namespace 詳解

目錄:

Namespaces

  • 命名空間使得我們可以組織和管理我們的程式碼庫

假設我們設置一個腳本名叫 Weapon:

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

public class Weapon : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

在這個腳本中,已經有了 3 個命名空間:

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

每個命名空間中包含了一系列不同的的 classes

比如這裡前面兩個庫是和 C# 相關

而 UnityEngine 是 Unity 自帶的庫,使用了這個庫後,我們就可以繼承 MonoBehaviour。假設我們把這行程式碼去掉,那就不能繼承 MonoBehaviour 了。

MonoBehaviour 讓我們可以把腳本和遊戲中的遊戲對象交互起來。

如果我們想要使用 Unity 中的 UI 系統,比如顯示個畫面,顯示個文字什麼的,那我們需要使用:

using UnityEngine.UI;

然後就可以直接使用 UnityEngine.UI 這個庫下面的程式碼了。

如果我們想要設置遊戲中的場景 scene,那我們需要使用:

using UnityEngine.SceneManagement;

這樣更好的分類,可以讓你容易尋找到自己需要的程式碼。

還有一個好處是可以解決命名衝突問題。比如我自定義了一個 weapon 的類想要添加到某個現有的遊戲中,但是該遊戲可能已經有了一個 weapon 的類,這時候命名空間的隔離就可以讓我們使用同樣的名字而不至於衝突。

比如我們從網上下載了一個武器包 Weapon Pack,裡面包含一個 weapon 的類,這時候命名衝突了,我們如何處理呢?

新設置一個命名空間即可:

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

namespace WeaponPack
{
    public class Weapon : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {

        }

        // Update is called once per frame
        void Update()
        {

        }
    }
}

這樣就不會有命名衝突了。

如何訪問這個類呢:

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

public class Weapon : MonoBehaviour
{
    WeaponPack.Weapon sword; // 訪問別的命名空間裡面的類

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

我們甚至還可以進行更細的分類,比如在 Weapon 下面分成近程武器和遠程武器等:

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

namespace WeaponPack.ShortRange // 近程武器
{
    public class Weapon : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {

        }

        // Update is called once per frame
        void Update()
        {

        }
    }
}

namespace WeaponPack.LongRange // 遠程武器
{
    public class Weapon : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {

        }

        // Update is called once per frame
        void Update()
        {

        }
    }
}

調用的時候方法類似:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WeaponPack.ShortRange; // 調用近程武器庫

public class Weapon : MonoBehaviour
{
    WeaponPack.ShortRange.Weapon sword; // 訪問別的命名空間裡面的類

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

按照特定邏輯設計獨有的 namespace,可以在把你的腳本給被人用的時候不會產生命名衝突問題。

Tour of Namespaces

  • 安排 namespaces 的案例:

假設我們要設計一個武器包用於在 asset store 上進行出售,如何安排層級結構和 namespaces 呢?

  • 首先頂層目錄可以是你的公司名稱,比如 MacroDev

  • 第二層目錄可以是 asset 類型,比如 3D

  • 第三層目錄可以是具體的類別,比如 Weapon

  • 第四層目錄可以是具體的名稱,比如 BigGun

    • 這層目錄下面可以包含:Demo 演示文件夾,Audio 音頻文件夾,Prefabs 文件夾,Particles 例子效果文件夾,Scripts 文件夾等;
    • 對應該武器控制腳本 BigGun 就放在 Scripts 文件中,而這個腳本導入到別人的項目中時,要確保其不會破壞別人的項目,不會產生命名衝突;
    • 所以我們可以把這個腳本中的內容放到一個命名空間namespace MacroDev.Weapon.BigGun
    • 添加必要的說明資訊,告知用戶如何使用:using MacroDev.Weapon.BigGun 即可接入該命名空間,比如利用該腳本下的 BigGun 這個類。
  • 導入 asset 後,只需要新建一個腳本命名為 BigGun,unity 沒有報錯,就說明沒有命名衝突問題。

接入方式,首先把 BigGun 的 Prefab 拖拽到場景中,該遊戲對象下面掛載了對應的腳本 BigGun,然後我們可以在別的腳本中進行接入:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MacroDev.Weapon.BigGun;

public class Weapon : MonoBehaviour
{
    BigGun bigGun; // 實例化

    // Start is called before the first frame update
    void Start()
    {
        bigGun = GameObject.Find("BigGun").GetComponent<BigGun>(); // 初始化
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
Tags: