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: