개발/Unity
Unity) 스크립트에서 자동으로 컴포넌트 추가하기
테샤르
2020. 2. 18. 10:38
스크립트에서 자동으로 컴포넌트 추가하기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(BoxCollider))]
public class Test : MonoBehaviour
{
private BoxCollider m_Collider = null;
void Start()
{
this.m_Collider = this.GetComponent<BoxCollider>();
}
void Update()
{
}
}
RequireComponent 속성은 요구되는 의존 컴포넌트를 자동으로 추가해줍니다.
특정 스크립트에서 항상 필요한 컴포넌트가 있는 경우에 의존 컴포넌트로 처리를 하면 따로 일일이 신경 쓰지 않아도 된다.
코드작업을 하다보면 자동화가 되어있으면 예외상황 및 사이드 이펙트를 줄일 수 있다.
URL :https://docs.unity3d.com/kr/530/ScriptReference/RequireComponent.html
★☆☆☆☆
반응형