简体   繁体   English

Unity 3D / 2D中的C#:我是否需要为每个脚本使用Classes?

[英]C# in Unity 3D/2D: Am I required to use Classes for every script?

A little background: I'm new to C# and Unity, but catching on very quickly. 一点背景: 我是C#和Unity的新手,但是很快就能赶上。 I'm also hoping this thread will not spark a debate about the merits of classes and abstract coding, as that debate is unrelated and well-worn (and unnecessarily heated); 我也希望这个主题不会引发关于课程和抽象编码优点的争论,因为那场辩论是无关紧要和陈旧的(并且不必要地加热); so please keep that in mind. 所以请记住这一点。

I'm simply wondering if every C# script in Unity is required to have a main class in any way or for any reason. 我只是想知道Unity中的每个C#脚本是否都需要以任何方式或任何原因拥有主类。

Or instead, can methods, and variables can be written outside of a class in a blank file (with namespaces) to be used in a video game? 或者,可以将方法和变量写在一个空白文件(带名称空间)的类之外,以便在视频游戏中使用吗?

I'm asking because, when I create a new C# script, it seems to force a class into my file and I'm afraid of breaking things. 我问,因为当我创建一个新的C#脚本时,它似乎强迫一个类进入我的文件,我害怕破坏它。

  • I hope to keep code abstraction to a minimum, and the current project I'm working on has several situations where a class is not needed, or only one instance of the class will be used. 我希望将代码抽象保持在最低限度,而我正在处理的当前项目有几种情况,其中不需要类,或者只使用该类的一个实例。 I'd like to simply avoid using classes in those cases. 我想在这些情况下避免使用类。

In terms of declaring/defining variables and methods outside of any class, you can't really do that in C# . 在声明/定义任何类之外的变量和方法方面, 你无法在C#中真正做到这一点 It just isn't how the language was designed (the answers to the question I linked to expand on that idea, so I won't duplicate them here). 这不是语言的设计方式(我链接的问题的答案扩展了这个想法,所以我不会在这里复制它们)。

You're not without options, though; 但是,你并非没有选择; if you have a number of variables or methods that need to be accessible from different places and don't need an object reference, you can make them static , so you won't need to instantiate the class to make use of them: 如果你有许多变量或方法需要从不同的地方访问而不需要对象引用,你可以将它们设置为static ,这样你就不需要实例化类来使用它们:

public class UtilityClass
{
    public static float GravityConstant = 3.51f;
    public static string GameName = "MyFirstGame";

    public static float CalculateProduct(float a, float b)
    {
        return a * b;
    }
}

Then, you can reference the class's methods/members by accessing it through its name: 然后,您可以通过名称访问该类的方法/成员来引用它们:

float product = UtilityClass.CalculateProduct(6, 1.5f);

An example of where you might use this pattern is when defining mathematical formulae which aren't included in Unity's Mathf methods, and using them in multiple classes. 您可以使用此模式的示例是定义未包含在Unity的Mathf方法中的数学公式,并在多个类中使用它们。

Additional note: Creating a new C# script through Unity's editor UI will default to declaring a class of the same name that inherits from Monobehaviour . 附加说明:通过Unity的编辑器UI创建一个新的C#脚本将默认声明一个从Monobehaviour继承的同名类。 You can alter it to remove the inheritance from Monobehaviour if you don't need any of the methods/attributes of the class, which avoids unnecessary overhead. 如果您不需要Monobehaviour任何方法/属性,则可以更改它以从Monobehaviour删除继承,从而避免不必要的开销。 One example for this would be with a static class that you never need to instantiate. 这方面的一个例子是你永远不需要实例化的静态类。

  1. First of All you need to check where Methods define as offical docs stated 首先,您需要检查方法定义的官方文档

    " Methods are declared in a class or struct by specifying the access level such as public or private...." “通过指定访问级别(如公共或私有......) 在类或结构 中声明方法。

    So, Method should be declare in a Class or struct and A given class should be, ideally, responsible for just one task. 因此,Method应该在类或结构中声明,理想情况下,给定的类应该只负责一个任务。 ( see also ) 另见

  2. Your this question "Or instead, can methods, and variables can be written outside of a class in a blank file (with namespaces) to be used in a video game?" 您的这个问题“或者,方法和变量可以写在一个空白文件(带名称空间)的类之外,以便在视频游戏中使用吗?” answer is hidden in the below question. 答案隐藏在下面的问题中。

    Can there be stand alone functions in C# without a Class ? 没有Class可以在C#中独立运行吗? No. Make them static and put them in a static utility class if they indeed don't fit within any of your existing classes. 不。将它们设置为静态,如果它们确实不适合您现有的任何类,则将它们放在静态实用程序类中。

    You have to make a class in order to use methods or its variable either instance class or static class. 您必须创建一个类才能使用方法或其变量实例类或静态类。

  3. Am I required to use Classes for every script? 我是否需要为每个脚本使用Classes? Every script means you required a class. 每个脚本都意味着您需要一个类。 Unity Support Component Based Architectural Design and if you require any script related work then you definitely require a script component which means a class require. Unity支持基于组件的架构设计 ,如果您需要任何与脚本相关的工作,那么您肯定需要一个脚本组件,这意味着一个类需要。

  4. Finally for singleton , thanks to Unity3dWiki great detail available. 最后对于singleton ,感谢Unity3dWiki提供了非常详细的信息。 I think you will be feel comfortable to code and writing class if you keep in mind component based architecture of Unity3d. 如果您记住Unity3d的基于组件的体系结构,我认为编写和编写类会很舒服。

  5. Singleton vs Static : I will also recommend to check this: Why do you use a Singleton class if a Static class serves the purpose Singleton vs Static :我还建议检查一下: 如果Static类用于此目的,为什么要使用Singleton类

Hope it will help. 希望它会有所帮助。

[Note: If this helpful Any one can update this answer for future reference and use]. [注意:如果这有用,任何人都可以更新此答案以供将来参考和使用]。

Yes, you are. 是的,你是。

In C#, things like global variables and functions just do not exist. 在C#中,全局变量和函数之类的东西不存在。 Everything must be contained in a class. 一切都必须包含在课堂上。

"But what should I do in order to declare some stuff that can be accessed everywhere, without creating an object?" “但是我应该做些什么来宣布一些可以在任何地方访问的东西,而无需创建一个对象?” you asked. 您询问。 There is something called the static modifier . 有一种称为静态修饰符的东西。 You can access the methods or variables or fields or properties marked with this modifier without creating an object of that class. 您可以访问使用此修饰符标记的方法或变量或字段或属性,而无需创建该类的对象。

You just add the word static in a method and it becomes a static method! 您只需在方法中添加单词static ,它就变成了静态方法! How simple! 多么简单!

Let's see an example. 我们来看一个例子吧。

I have this non-static method: 我有这种非静态方法:

public class MyClass {
    public void DoStuff () {

    }
}

I can call it like this: 我可以这样称呼它:

var obj = new MyClass();
obj.DoStuff();

But if I modify it with static , 但如果我用static修改它,

public class MyClass {
    public static void DoStuff () {

    }
}

I can call it like this: 我可以这样称呼它:

MyClass.DoStuff();

How convenient! 多方便啊!

Note: 注意:

Please do not misuse the static modifier! 请不要滥用静态修饰符! Only use it when it makes sense! 只有在有意义的时候才使用它! When? 什么时候? When the method is a utility method or when the method does not belong to individual objects but the class itself. 当方法是实用方法或方法不属于单个对象而是类本身时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM