[Unity] RequireComponent
[Unity] RequireComponent
오브젝트에 컴포넌트를 추가혀여 사용하다보면 서로 의존관계에 있는 컴포넌트가 있기마련이다.
예를들어 플레이어의 애니메이션을 제어하는 스크립트 csPlayerAnim과 총알 발사를 제어하는 csFire 스크립트가 플레이어 오브젝트에 있다고 가정하자.
csFire스크립트는 csPlayerAnim에서 사용자의 애니메이션을 ‘조준’ 이란 상태에 뒀을 때만 총알을 발사하도록 되어있다면,
csFire는 csPlayerAnim으로부터 애니메이션 상태를 확인해야만 한다.
csFire가 작동하기 위해서 csPlayerAnim스크립트가 반드시 필요하다.
하지만, 복잡하게 여러게의 스크립트가 프로젝트내에 존제한다면 어떤 스크립트를 같이 사용해야하는지 햇갈린다.
이때, 사용자가 csFire에 csPlayerAnim스크립트가 필요하다는 것을 RequireComponent를 사용하여 명시할 수 있다.
스크립트의 상단 클래스 외부에 [RequireComponent(typeof(TYPE))]을 기술하며, TYPE에 필요한 컴포넌트의 타입을 지정한다.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class csFireControl : MonoBehaviour {
public GameObject bullet; // pfBullet 프리팹
public Transform firePos; // 총알의 리스폰포인트
public GameObject fireFlash;
citizen titanium watch - The Argonaut
답글삭제A model of a watches titanium price per ounce with dials. The first thing titanium wedding band sets that's important is the ability to make an informed 2014 ford focus titanium hatchback decision titanium bracelet on your own, regardless of mens titanium earrings