ویرگول
ورودثبت نام
soheil moonesi
soheil moonesi
خواندن ۵ دقیقه·۷ ماه پیش

Unity Cheat Sheet

MonoBehaviour

The MonoBehaviour class is defined by Unity to help you transform your class into a component for game objects:

خوب، کلاس MonoBeahviour توی unity تعریف شده برای این که بیایم کلاسمون رو تبدیل کنیم به یک کامپوننت برای game object

using UnityEngine; public class ExampleClass : MonoBehaviour { }

Script Lifecycle

All components that derive from the MonoBehaviour class executes a number of event functions in a predetermined order. They're the following:

  • Awake() - This function is called after a game object has been instantiated.
  • OnEnable() - This function is called when a game object is enabled.
  • Start() - This function is called before the first frame update.
  • Update() - This function is called on every frame.
  • LateUpdate() - This function is called on every frame after the Update() function is called.
  • OnBecameVisible() - This function is called when the current game object becomes visible via any camera.
  • OnBecameInvisible() - This function is called when the current game object no longer becomes visible via any camera.
  • OnDrawGizmos() - This function is called for drawing gizmos in the scene window.
  • OnGUI() - This function is called multiple times for GUI events.
  • OnApplicationPause() - This function is called when the game is paused via the Unity editor.
  • OnDisable() - This function is called when the game object is disabled.
  • OnDestroy() - This function is called when the game object is destroyed.

There's a lifecycle function called FixedUpdate, which is called a fixed number of times per second. You can configure the frequency in the Edit ▸ Project Settings ▸ Time ▸ Fixed Timestep.

در حال حاضر این ها رو میدونم چی کار میکنن:

اول start : این فانکشن قبل از اجرای اولین آپدیت فریم اجرا میشه.

دوم update : این فانکشن توی هر فریم اجرا میشه.

بقیه رو هم توضیحاتش نوشته شده ولی چون هنوز کاربردش رو دقیقا خودم نمیدونم اینجا ننوشتم ادامه شو.

Serializing Variables

Unity is capable of performing serialization on your variables. Serialization is the process of transforming your data into a format that can be read and edited from the Unity editor. Variables can be serialized based on either the attribute or access modifiers applied to a variable.

یونیتی این قابلیت رو با serialization به ما میده که به هر مدل دیتا فارغ از access modifiers بهشون دسترسی پیدا کنیم.

If a variable is public, it'll be automatically serialized:

public int age = 10;

خوب، در حالت اول وقتی که دیتامون public هستش به صورت اتوماتیک serialized شده و میتونیم بهش دسترسی پیدا کنیم

If you don't want public variables to be serialized, you can use the NonSerialized attribute from the System namespace like so:

[NonSerialized] public int age = 10;

خوب، حالا اگر بخوایم یه دیتایی که public هستش serialized نشه میایم از NonSerialized استفاده میکنیم.

Private variables not serialized. However, if you want them to be serialized, Unity has an attribute called SerializeField that can be found from the Unity namespace. It can be applied like so:

متغییر های private در حالت عادی serialized نیستند ولی ما میتونیم با اتریبیوتی که در یونیتی تعریف شده به اسم serializedField بیایم و serialized اش کنیم.

[SerializeField] private int age = 10;

Instantiating Game Objects

New game objects can be inserted into the scene programmatically by calling the Instantiate() function. This function has three arguments.

  • The Game Object
  • (Optional) Global Position
  • (Optional) Rotation

درست کردن game object

وقتی میخوایم یه سری آبجکت بازی رو وارد فضای برنامه نویسی کنیم میایم از فانکشن Instantiate استفاده میکنیم. این فانکشن ۳ تا ورودی میگیره که دو تاش انتخابیه و یکیش رو باید وارد کنیم.

Instantiate(someGameObject);
Instantiate(someGameObject, new Vector3(0, 0, 10));
Instantiate(someGameObject, new Vector3(0, 0, 10), Quaternion.identity);

خوب، حالا قسمت وکتور و quaterion رو جلوتر بهش میرسیم که داستانش چیه.

Components

By themselves, game objects can't do anything by themselves. We must add components to add specific functionality. If you add custom components, you might want to select other components. Unity offers various solutions to grab components from a game object.

The first solution is to use the GetComponent() function. There are different ways to call this function. The most common is to pass in the name of the class for the component as a generic like so:

خوب game object ها به تنهایی کاری رو انجام نمیدن. ما باید یه سری کامپوننت رو برای این که یه کار خاص انجام بدن باید بهش اضافه کنیم.

حالا راه های مختلفی برای ساختن کامپوننت ها از game object ها داریم:

اولین راه اینه که بیایم از GetComponent استفاده کنیم. راه هایی مختلفی هم هست برای این که از این فانکشن استفاده کنیم:

یه راه معمولش اینه که بیایم اسم کلاس رو به صورت generic بفرستیم به کامپوننت:

AudioSource audioSource = GetComponent<AudioSource>();

Alternatively, you can use the typeof keyword and assert the value like so:

یه راه دیگه استفاده از typeOf هستش:

AudioSource audioSource = GetComponent(typeof(AudioSource)) as AudioSource;

Lastly, you can pass in the name of the component as a string like so:

یه راه دیگه هم اینه که اسم اون کامپوننت رو با string بفرسیتم:

AudioSource audioSource = GetComponent("AudioSource") as AudioSource;


Vectors

Vectors are used for the positions of game objects. You can use them to help you calculate distance and movement. There are two vector classes, which are Vector2 and Vector3.

Vector2 is primarily used for 2D games that have two coordinates (x, y). Vector3 is primarily used for 3D games that have three coordinates (x, y, z).

X = Left/Right Y = Up/Down Z = Forward/Back

Unity supplies you with constants that can be accessed from their respective vector class.

میرسیم به قسمت vector ها :

از vector ها برای تعیین موقعیت game object ها استفاده میکنیم. دو مدل vector class داریم vector2 , vector3 که اولی معمولا برای بازی های دو بعدی استفاده میشه با مختصات های x y و دومی برای بازی های سه بعدی با محور ها xyz استفاده میشه

از x برای راست و چپ

از y برای بالا و پایین

از z برای عقب و جلو ( همون عمق منظورشه)

این مقاله از این سایت گرفته شده : سایت

و خیلی از موارد گفته شد در سایت اصلی فعلا اینجا آورده نشده و در آینده این متن کامل تر خواهد شد.



this functionfunction calledgame objectunity
C# enthusiast
شاید از این پست‌ها خوشتان بیاید