Unity

DisallowMultipleComponent

브랑제리 2022. 3. 20. 22:23

https://docs.unity3d.com/ScriptReference/DisallowMultipleComponent.html

 

Unity - Scripting API: DisallowMultipleComponent

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

 

DisallowMultipleComponent 선언

Component가 GameObject에 중복으로 등록되는것을 방지하는 Attribute입니다.

Class 선언문 위에 작성하면 됩니다.

예제

[DisallowMultipleComponent]
public class DisallowMultipleComponentTest : MonoBehaviour
{

}

인스펙터에서 추가 할 경우

해당 스크립트를 GameObject에 한개 이상 등록 할 경우 에러팝업이 노출 됩니다.

코드에서 추가 할 경우

// 테스트 코드
public class Test : MonoBehaviour
{
    void Start()
    {
        gameObject.AddComponent<DisallowMultipleComponentTest>();
        gameObject.AddComponent<DisallowMultipleComponentTest>();
        gameObject.AddComponent<DisallowMultipleComponentTest>();
        gameObject.AddComponent<DisallowMultipleComponentTest>();
    }
}

 

실행 시 1개만 추가된 것을 확인 할 수 있습니다.

 

반응형

'Unity' 카테고리의 다른 글

error CS8357 Deterministic compilation failed  (0) 2022.06.21
Failed to create CoreCLR, HRESULT: 0x8007000E  (0) 2022.06.11
[Tip] Unity Chan URP 셰이더 에러 수정  (2) 2021.10.30
Input System #1  (0) 2021.09.30
Addressable Asset System 예제  (2) 2021.05.25