Professional Documents
Culture Documents
Appunti Di C# Per Unity
Appunti Di C# Per Unity
transform.position
if(Input.getKeyDown(KeyCode.Space))
{
Debug.Log("space pressed");
}
Time.TimeScale
Quaternion.Slerp(..);
switch(condition){
caso0:… break;
case1: …. break;
default:… break; }
C# constructors-:
public class WeaponStats()
{
public string name;
public float attackRate;
//The following is the constructor:
public WeaponStats(string name, float attackRate)
{
//initialize things there
this.name = name;
this.attackRate = attackRate;
}
}
//define the objects in the "player" class that inherits monobehavior:
private WeaponStats blasters;
//then inizialize the objects in Start():
void Start()
{
blasters = new WeaponStats("Blaster", 0.25f);
}
I have to use monobehavior objects when I have to attach them game logic
use custom class to define blueprint for your programs
use static type when you have data that is shared across the entire game and there
is only one of them
I can access a static data at class level:
Score.score += 10;
the data remains in the memory for the life of the program
when something is static I can’t see it in the inspector
static data (defined in the custom class) is shared across all instances of an object,
and can be accessed at class level (use Item.staticData, not sword.static.Data)
utility classes are static classes (cannot inherit monobehavior) filled with static
members
static constructor are called only once before the instance constructor.
properties (getters and setters in java) allow you to control access modifiers:
private bool isGameOver;
public bool IsGameOver
{
get
{
return isGameOver;
}
set
{
isGameOver = value
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)
{
IsGameOver = true; //uses the setter of the IsGameOver property
}}
namespaces allow us to organize and mantain our code libraries, to prevent conflict
of names:
namespace WeaponPack
{
public class Weapon : monobehavior
{
}
}
I can access the Weapon class I created in another script by declaring in the first
lines ( before the class declaration):
using WeaponPack;
enums allow you to create readable selections that are based on integer values and
you can use them in an english format when you’re writing code.
public enum LevelSelector
{
Easy, //is assigned to 0
Normal, //assigned to 1
Hard, //2
//I can also assign the number manually:
Expert = 43,
SuperDuper = 324
}
to make use of it I create a variable of type LevelSelector:
public LevelSelector currentLevel;
now I can use the switch statement:
{
switch (currentLevel)
{
case LevelSelector.Easy:
break;
}}
[enums for custom class]
casting enums to int:
The data type I want to cast it to in parenthesis before the variable name:
SceneManager.LoadScene((int)LevelSelector);
with dictionary i can associate objects with values with a key/value pair:
public List<Item> itemList = new List<Item>();
public Dictionary<int, Item> itemDictionary = new Dictionary<int, Item>();
…
itemDictionary.Add(0, new Item());
var item = itemDictionary[0];
looping in dictionary:
foreach (KeyValuePair<int, Item> item in itemDictionary){}
(var key in itemDictionary.Keys){}
(Item item in itemDictionary.Value){}
the keys must be unique and must exist, you should always check before access an
object in a dictionary through key, if that key exist:
if ( itemDictionary.ContainsKey(70){}
use dictionaries when you. have large lists
interfaces are similar to abstract classes in the sense that they can force
implementations, anything that is in the interface must be implemented
interfaces cannot have the public or private fields (anything is assumed public) and I
can only use properties and methods
interfaces cannot inherit anything
public interface IDamageable
{
int Health {get; set;}
void Damage(int damageAmount);
}
public class Enemy : Monobehavior, IDamageable
{
public int Health {get; set;}
public void Damage(int damageAmount){}}
one benefit of using interfaces is that they allow multiple inheritance (classes can
inherit from only one class), I can implement as many interfaces as I want
polymorphism:
public interface IDamageable
{
int Health {get; set;}
void Damage(int damageAmount);
void Damage(float damageAmount);
}
Otherwise, I can also use polymorphism this way:
public interface IDamageable<T> //The T keyword signify that it is a generic type
{
int Health {get; set;}
void Damage(T damageAmount);
}
//so whatever I specify when I implement the interface, will be the type of T:
public class Enemy : Monobehavior, IDamageable<int>
{
public int Health {get; set;}
public void Damage(int damageAmount){}}
—>interfaces are very useful when I control if any of my objects implement the
interface:
RaycastHit hitInfo;
IDamageable obj = hitInfo.collider.GetComponent<IDamageable>();
if (obj != null) //it means that it is implementing the interface
you can think of delegates as a variable that holds a method or several methods:
public delegate void ChangeColor(Color = new Color);
{