dev-notes/DotNet/Unity/Scripting.md

136 lines
3.2 KiB
Markdown
Raw Normal View History

2021-01-31 11:05:37 +01:00
# Unity C# Scripting
## Logging
```c#
Debug.Log(string); //output message to console (more powerful and flexible than print())
Print(string); //output message to console
```
## Scripts
```c#
public class ClassName : MonoBehaviour {
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Time.deltaTime; // time since last frame
}
2021-09-20 19:35:32 +02:00
// FixedUpdate is calls every x seconds (not influenced by FPS instability)
// used for physics calculations which should be FPS independent
2021-01-31 11:05:37 +01:00
void FixedUpdate()
{
Time.fixedDeltaTime; // fixed amount of time
2021-09-20 19:35:32 +02:00
Time.timeDelta; // if called inside FIxedUpdate() behaves like fixedDeltaTime
2021-01-31 11:05:37 +01:00
}
}
```
2021-09-20 19:35:32 +02:00
### Script communication
2021-01-31 11:05:37 +01:00
2021-09-20 19:35:32 +02:00
Referencing data in a script from another.
2021-01-31 11:05:37 +01:00
```cs
//example of a script to be referenced in another
Using System;
2021-09-20 19:35:32 +02:00
public class Player : MonoBehaviour {
2021-01-31 11:05:37 +01:00
public float health = 10;
public event Action OnPlayerDeath; //event of type Action, needs using System
void Start() {
}
void Update() {
if (health <= 0) {
if (OnPlayerDeath != null) {
2021-09-20 19:35:32 +02:00
OnPlayerDeath(); // invoke Action (if no subscribers event will be NULL, can cause errors)
2021-01-31 11:05:37 +01:00
}
Destroy(GameObject); // needs to be notified
}
}
}
```
```cs
// example of script needing a reference to another
public class GameUI : MonoBehaviour {
2021-09-20 19:35:32 +02:00
Player player; //instance of referenced GameObject to be found by its type
2021-01-31 11:05:37 +01:00
void Start(){
GameObject playerObj = GameObject.Find("Player"); //reference to game object
GameObject playerObj = GameObject.FindGameObjectWithTag("Tag"); //reference to game object
player = playerObj.GetComponent<Player>(); // get script attached to the GameObject
player = FindObjectOfType<Player>(); // get reference to an object
2021-09-20 19:35:32 +02:00
// on event invocation all subscriber methods will be called
2021-01-31 11:05:37 +01:00
player.OnPlayerDeath += GameOver; // subscribe method to event
}
void Update() {
DrawHealthBar(plyer.health); // call method passing data of player GameObject
}
2021-09-20 19:35:32 +02:00
void DrawHealthBar(float playerHealth) {
2021-01-31 11:05:37 +01:00
// implementation
}
public void GameOver() {
//game over screen
}
}
```
## Screen
### 2D Screen Measures
Aspect Ratio = `(screen_width [px]) / (screen_height [px])`
2021-09-20 19:35:32 +02:00
Orthographic Size `[world units]` = `(screen_height [world units] / 2)`
Aspect Ratio * Orthographic Size = `(screen_width [world units] / 2)`
Screen Width `[world units]` = `(AspectRatio * OrthographicSize * 2)`
2021-01-31 11:05:37 +01:00
```cs
screenWidth = Camera.main.aspect * Camera.main.orthographicSize * 2;
```
## Scriptable Objects
Class to store data in stand alone assets, used to keep data out of scripts.
Can be used as a template.
```c#
[CreateAssetMenu(menuName = "ScriptableObjectName")] //enable creation of scriptable object
public class ScriptableObjectName : ScriptableObject {
//data structure here
}
```
### Game Object Serialization
```c#
2021-09-20 19:35:32 +02:00
[SerializeField] type variable; //access game object from code
2021-01-31 11:05:37 +01:00
```
### Game Object Data Access
```c#
public type GetVariable(){
return variable;
}
```