## Introducere in Unity

### Cerinte

• Creati o scena de Main Menu
• Adaugati un Buton de Start Game
• Creati o noua scena de joc cu minim un cub in ierarhie
• La apasarea butonului de Start Game din Main Menu trebuie sa se lanseze scena de joc

### Sumar documentatie

Hotkeys

Folders  inside unity

Unity reserves some project folder names to indicate that the contents have a special purpose. There are a number of folder names that Unity interprets as an instruction that the folder's contents should be treated in a special way. Some of these folders have an effect on the order of script compilation. These folder names are:

• Assets: The Assets folder is the main folder that contains the Assets used by a Unity project.
• Editor: Scripts placed in a folder called Editorare treated as Editor scripts rather than runtime scripts. These scripts add functionality to the Editor during development, and are not available in buildsat runtime.
• Editor default resources:Editor scripts can make use of Asset files loaded on-demand using the [EditorGUIUtility.Load](https://docs.unity3d.com/ScriptReference/EditorGUIUtility.Load.html) function. This function looks for the Asset files in a folder called Editor Default Resources.
• Gizmos: [Gizmos](https://docs.unity3d.com/ScriptReference/Gizmos.html) allow you to add graphics to the Scene Viewto help visualise design details that are otherwise invisible.
• Plugins: You can add [plug-ins](https://docs.unity3d.com/Manual/Plugins.html) to your Project to extend Unity's features. Plug-ins are native DLLs that are typically written in C/C++. They can access third-party code libraries, system calls and other Unity built-in functionality.
• Resources:You can load Assets on-demand from a script instead of creating instances of Assets in a Scene for use in gameplay. You do this by placing the Assets in a folder called Resources. Load these Assets by using the [Resources.Load](https://docs.unity3d.com/ScriptReference/Resources.Load.html) function.
• Standard Assets: When you import a Standard Asset package (menu: Assets > Import Package) the Assets are placed in a folder called Standard Assets
• StreamingAssets: You may want the Asset to be available as a separate file in its original format although it is more common to directly incorporate Assets into a build. To do so, place a file in a folder called StreamingAssets, so it is copied unchanged to the target machine, where it is then available from a specific folder.
Asset Bundles, Resources and StreamingAssets

There are several ways to serve up resources to Unity at runtime, and each method has its place in game development: asset bundles, resource folders, and streaming assets.

# Asset Bundles:

Asset bundles let you deliver content to your application outside of your Unity build. Generally, you'd host these files on a remote web server for users to access dynamically. Asset bundles can contain anything from individual assets to entire scenes, which also makes them ideal for delivering downloadable content (DLC) for your game.

# Resource Folders:

The Resource folders are bundled managed assets. That means they will be compressed by Unity, following the settings you apply in the IDE. Unlike Asset bundles, resource folders are baked into the Unity Player as part of the game. You can do this by adding the required assets to a folder named Resources in your assets directory.

# Streaming Assets:

Like Resource Folders, a Streaming Assets directory can be created by intuitively creating a folder named StreamingAssetsthat, that you can use to put bundled un-managed assets. Unlike Resource folders, this directory remains intact and accessible in the Unity player. This creates a unique access point for users to add their own files to the game.

Prefabs In Unity 3d

Prefab in Unity 3D is referred for pre-fabricated object template (Class combining objects and scripts). At design time, a prefab can be dragged from project window into the scene window and added the scene's hierarchy of game objects. If desired the object then can be edited.

At the runtime, a script can cause a new object instance to be created at a given location or with a given transform set of properties.

### Unity Scripting

#### Lifecycle

In Unity scripting, there are a number of event functions that get executed in a predetermined order as a script executes. This execution order is described below:

1. Awake(): This function is always called before any Start functions and also just after a prefabis instantiated.
2. OnEnable(): This function is called just after the object is enabled. This happens when a MonoBehaviour instance is created, such as when a level is loaded or a GameObjectwith the script componentis instantiated.
3. Start(): Start is called before the first frame update only if the script instance is enabled.
4. FixedUpdate(): FixedUpdate is often called more frequently than Update. It can be called multiple times per frame
5. Update(): Update is called once per frame. It is the main workhorse function for frame updates.
6. LateUpdate(): LateUpdate is called once per frame, after Update has finished. Any calculations that are performed in Update will have completed when LateUpdate begins.
7. OnGUI(): Called multiple times per frame in response to GUI events.
8. OnApplicationQuit(): This function is called on all game objects before the application is quit. In the editor it is called when the user stops playmode.
9. OnDisable(): This function is called when the behaviour becomes disabled or inactive.
10. OnDestroy(): This function is called after all frame updates for the last frame of the object's existence (the object might be destroyed in response to Object.Destroy or at the closure of a scene).

Difference between Start and Awake Unity Events

Awake as ” initialize me” and Start as ” initialize my connections to others.” You can't rely on the state of other objects during Awake, since they may not have Awoken themselves yet, so it's not safe to call their methods.

Start function is called only if the script component is enabled. Awake function called when the script instance is being loaded.

If the script is NOT enabled at the beginning of your game, and you don't need the variables to be initialized, start() would be saving performance as awake() would be called regardless.

Difference between Update, Fixed Update and Late Update.

Update: (Most things)

• Update is called once per frame from every script in which it is defined. Calling of Update function is dependent on the frame rate.
• The time interval to call update is not fixed; it depends on how much time required to complete the individual frame.

FixedUpdate:(Physics things)

• FixedUpdate is independent of the frame rate. The time interval is to call FixedUpdate is constant; as it is called on a reliable timer.
• FixedUpdate can be called multiple times per frame if frame rate is low or it may not be called per frame if the frame rate will be high.
• All the physics related calculations and updates are called immediately after FixedUpdate. So, its good to handle all physics related calculation inside FixedUpdate.

LateUpdate:(After update)

• LateUpdate is also called per frame. It is called after all other Update functions.
• This is useful to ensure all other Update related calculation is complete and other dependent calculation can be called.
• For example, if you need to integrate a third-person camera to follow any object; then it should be implemented inside the LateUpdate. It is make sure that camera will track the object after its movement and rotation update.
Script compilation order inside Unity

There are four separate phases of script compilation. The phase where a script is compiled is determined by its parent folder. In a script compilation, the basic rule is that anything that is compiled in a phase _after_ the current one cannot be referenced. Anything that is compiled in the current phase or an earlier phase is fully available.

The phases of compilation are as follows:

• Phase 1: Runtime scripts in folders called Standard AssetsPro Standard Assets and Plugins
• Phase 2: Editor scripts in folders called Editor that are anywhere inside top-level folders called Standard AssetsPro Standard Assets and Plugins.
• Phase 3: All other scripts that are not inside a folder called Editor.
• Phase 4: All remaining scripts (those that are inside a folder called Editor).

A common example of the significance of this order occurs when a UnityScript file needs to reference a class defined in a C# file. To achieve this, you need to place the C# file inside a Plugins folder, and the UnityScript file in a non-special folder. Because C# code is compiled before JS code, so in general, while JS code can access C# classes, the opposite is not possible If you don't do this, an error is thrown saying that the C# class cannot be found.

#### Unity C# References

Variables and Parameters

Variables can be controlled from the Unity Editor as parameters, as long as they are defined public or serializable. We can use both simple variable or arrays.

public float speed;
public Sprite[] images;
private float internalBleed; //cannot be controller from Unity Editor interface

[SerializeField]
private Text scoreText;

[SerializeField]
private GameObject[] enemyTypes;
MonoBehaviour Event Execution Order

Ordered by first to last method to execute.

private void Awake() { /* Called when the script is being loaded */ }
private void OnEnable() { /* Called every time the object is enabled */ }
private void Start() { /* Called on the frame when the script is enabled */ }
private void Update() { /* Called once per frame */ }
private void LateUpdate() { /* Called every frame after Update */ }
private void OnBecameVisible() { /* Called when the renderer is visible by any Camera */ }
private void OnBecameInvisible() { /* Called when the renderer is no longer visible by any Camera */ }
private void OnDrawGizmos() { /* Allows you to draw Gizmos in the Scene View */ }
private void OnGUI() { /* Called multiple times per frame in response to GUI events */ }
private void OnApplicationPause() { /* Called at the end of a frame when a pause is detected */ }
private void OnDisable() { /* Called every time the object is disabled */ }
private void OnDestroy() { /* Only called on previously active GameObjects that have been destroyed */ }

Physics updates on a Fixed Timestep are defined under Edit ▸ Project Settings ▸ Time ▸ Fixed Timestep and may execute more or less than once per actual frame.

private void FixedUpdate() { /* Called every Fixed Timestep */ }
Debug
Debug.Log(transform.position);
Debug.Log("text");