FixedUpdate, LateUpdate 적절히 사용Coroutine으로 대체**Update 대신 필요할 때만 실행하는 코루틴으로 로직 처리this 키워드를 붙이면 그것이 확장될 대상 타입이 된다.public static class 확장메서드를담는클래스
{
public static 반환형식 메서드이름(this 확장할_타입 인스턴스, [추가 인자들])
{
// 확장 로직 작성
}
}
public static class StringExtensions
{
public static string ReverseString(this string input)
{
char[] charArray = input.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
public static void LookAtDirection(this Transform transform, Vector3 direction)
{
if (direction != Vector3.zero)
{
transform.rotation = Quaternion.LookRotation(direction);
}
}
static class 안에 있어야 함.static이어야 함.this 키워드와 함께 확장할 타입이어야 함확장 메서드는 Unity 개발뿐 아니라 일반적인 C# 개발에서도 재사용성과 유지보수성을 높일 수 있는 강력한 기능이다.
특히, Unity 프로젝트에서는 Transform, Vector, GameObject 등 자주 쓰는 타입에 대해 반복적으로 사용하는 로직을 확장 메서드로 정리하면 다음과 같은 이점이 있다:
앞으로의 프로젝트들에는 확장메서드나, 제네릭 등을 활용해보면서 천천히 익히면 나중에 나만의 개발 프레임워크를 만들 때 큰 도움이 될 거 같다.