📅  最后修改于: 2023-12-03 14:48:11.320000             🧑  作者: Mango
Unity is a popular game engine that uses the programming language C# to develop games. C# is a powerful and modern language that allows developers to write efficient and maintainable code.
In this article, we'll go through some of the common public C# codes that you need to know as a Unity developer. With these codes, you can create powerful and engaging games on Unity.
The public class is a fundamental building block of C# programming in Unity. It defines the blueprint for objects that will be used in a game. To create a public class in Unity, use the following code:
public class MyClass : MonoBehaviour {}
In the above code, we create a public class named MyClass
. The MonoBehavior
class is inherited from Unity, which provides a wide range of capabilities and functionality.
Public variables are variables within a public class that can be accessed from other classes or objects. This allows for easy manipulation of a public class's properties or fields. To declare a public variable, use the following code:
public int myInt;
public float myFloat;
public string myString;
In the above code, we create three public variables of different data types - int
, float
, and string
. These variables can be accessed and modified from other classes or objects within the same Unity project.
Public methods are functions within a public class that can be accessed from other classes or objects. These methods are used to perform specific tasks or actions within a game. To declare a public method, use the following code:
public void MyMethod() {}
In the above code, we create a public method named MyMethod
. This method can be accessed and invoked from other classes or objects within the same Unity project.
Public events are similar to public methods, but they are used to signal when a particular action or event occurs within a game. For example, an event could be triggered when a player collects a power-up, or when a game level is completed. To declare a public event, use the following code:
public delegate void MyEvent();
public static event MyEvent onMyEvent;
In the above code, we create a public event named onMyEvent
using a delegate. This event can be subscribed to and invoked from other classes or objects within the same Unity project.
In this article, we've gone through some of the basic public C# codes that you need to know as a Unity developer. These codes are essential in creating powerful and engaging games on Unity. By mastering these codes, you can build complex and immersive games that will captivate and entertain players.