Unity Анотації

2075 / Unity / 2D / 3D / Анотації

 

[Range(0f,100f)] //  діапазон дійсних чисел
public float range = 10.0f;
 

[Range(0,100)] // діапазон цілих чисел
public int range = 10;
 

[SerializeField] // робить приватне поле доступним в Інспекторі
private float speed = 3.0f;
 

[Tooltip("Health value between 0 and 100.")] // спливаюча підказка в інспекторі
public int health = 0;
 

public GameObject[] monsters; // масив
 

public List<GameObject> list; // список
 

public Color color; // віконце вибору кольору
 

public PlayerStateEnum playerState; // випадайка
 

[HideInInspector] // прихована змінна
public float walkSpeed;
 

[Header("Movement")] // текст перед групою змінних

[ContextMenu("F1")] // Запуск функції з Unity-меню
public void F1(){ print("1"); }  
// клацніть правою кнопкою миші по скрипту і оберіть F1

[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(BoxCollider))]

або

[RequireComponent(typeof(Rigidbody), typeof(BoxCollider))]
  • ставиться перед класом
  • додає компоненти, якщо нема
  • не дає видалити їх
Функція, що запускається сама

[RuntimeInitializeOnLoadMethod]
static void OnRuntimeMethodLoad()
{
  Debug.Log("Після того як сцена завантажена і гра запущена");
}

 

Змінні
public bool blueOrYellow;



public Color color;



public enum Direction { Right, Left, Up, Down } 
public Direction direction; 


В редакторі

#if UNITY_EDITOR 
transform.position += Vector3.right * 0.001f; 
#endif   

або 

if (Application.isEditor) { transform.position += Vector3.up * 0.001f; }

Playmode

if (Application.isPlaying) { transform.position += Vector3.down * 0.001f; }

В exe

if (!Application.isEditor) { transform.position += Vector3.down * 0.001f; }

 

Своя статична функція у меню

[MenuItem("MyMenu/Do Something")]

static void DoSomething() { Debug.Log("Doing Something..."); }