유니티로 배우는 c# 16

코루틴

using System.Collections; using System.Collections.Generic; using UnityEngine; using System; public class NewBehaviourScript : MonoBehaviour { Coroutine myCoroutine1; Coroutine myCoroutine2; // private IEnumerator myCoroutine1; 4번째 방법 void Start() { // myCoroutine1 = LoopA(1,2,3,4,5,6) myCoroutine1 = StartCoroutine(LoopA()); StartCoroutine("LoopB");//LoopA와 LoopB가 동시에 실행 문자열이 상대적으로 과부화에 더 걸린다. 최..

예외 처리

using System.Collections; using System.Collections.Generic; using UnityEngine; using System; public class NewBehaviourScript : MonoBehaviour { int a = 5; int b = 0; int c; void Start() { try // 예외를 잡아주기 위해 { c = a / b; } catch (DivideByZeroException ie) { print(ie); b = 1; print(b); c = a / b; print(c); } catch (NullReferenceException ie)//아무것도 없을 때 발생하는 오류 { // 오류 원인마다 처리할 수 있다. print(ie); b = ..

형식 매개 변수 T

using System.Collections; using System.Collections.Generic; using UnityEngine; public class NewBehaviourScript : MonoBehaviour { void Print(T value) where T : class{//class타입만 허용합니다. 가령 float타입 같은 경우는 struct이기 때문에 오류가 뜹니다. print(value); } // Start is called before the first frame update void Start() { // Print('a'); // string이기 때문에 void Print(string value)에 들어간다. Print("abc"); Print(4.3f); } // ..

인터페이스

using System.Collections; using System.Collections.Generic; using UnityEngine; abstract public class A: MonoBehaviour { abstract public void Abc(); } abstract public class B: MonoBehaviour { abstract public void Bbc(); } public interface iTest { // 다중 상속 void Bbc(); // 인터페이스에는 변수가 올 수 없다. // 함수, 프로퍼티, 인덱서, 이벤트만 올 수 있다. // 뼈대만 만들어야한다. } public class NewBehaviourScript : A, iTest { public override..

인덱서

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Record { public int[] temp = new int[5]; // 인덱서 this는 해당 클래스를 지칭하는 예약어 // this는 인덱서의 이름 // int index로 매개변수를 받아줍니다. public int this[int index] { get { if(index >= temp.length) { Debug.Log("인덱스가 너무 큽니다."); return 0; }else { return temp[index]; } } // Record 클래스가 monoBehaviour를 상속받지 못하였기 때문에 print를 사용할 수 없습니..

상속

using System.Collections; using System.Collections.Generic; using UnityEngine; abstract public class Human : MonoBehaviour { public string humanName; public int humanAge; protected virtual void info(){ //virtual은 자식 클래스에서 재정의 할 수 있게 해주는 녀석 print("나는 인간입니다."); } // 추상함수 // 자식 클래스에서 기능을 완성시켜야 하는 함수 // 이걸 쓰게 되면 클래스 자체도 미완성이 되기 때문에 class에도 abstract를 써야한다. abstract protected void Name(); } using Syst..

델리게이트

using System.Collections; using System.Collections.Generic; using UnityEngine; public class test : MonoBehaviour { // 함수들을 관리해주는 녀석들 public delegate void ChainFunction(int value); public static event ChainFunction onStart; // ChainFunction chain; 이벤트를 선언하지 않을 경우 해줘야합니다. int power; int defence; public void SetPower(int value) { power += value; print("power의 값이" + value + "만큼 증가했습니다. 총 power의 값 = ..