UnityEvent는 추가 프로그래밍 및 스크립트 설정 없이도 사용자 기반 콜백이 편집 시간부터 런타임까지 유지되도록 하는 방법입니다.
UnityEvent는 다음과 같은 여러 경우에 유용합니다.
콘텐츠 기 콜백
디커플링 시스템
영구적 콜백
미리 설정된 호출 이벤트
UnityEvent
는 MonoBehaviour
에 추가할 수 있고 표준 .NET 대리자처럼 코드에서 실행됩니다. UnityEvent
가 MonoBehaviour
에 추가되면 인스펙터에 표시되고 영구적 콜백을 추가할 수 있습니다.
UnityEvent
의 한계는 표준 대리자와 유사합니다. 즉, 타겟인 요소에 대한 레퍼런스를 유지하여 타겟이 가비지 컬렉션의 대상이 되지 않습니다. UnityEngine.Object가 타겟이고 네이티브 표현이 사라지는 경우 콜백이 호출되지 않습니다.
에디터에서 콜백을 설정하려면 다음 몇 가지 단계를 수행해야 합니다.
스크립트에서 UnityEngine.Events
를 임포트/사용하는지 확인합니다.
콜백을 수신할 UnityEngine.Object를 선택합니다(오브젝트 선택기를 사용하여 선택 가능).
호출되도록 할 함수를 선택합니다.
이벤트에 대해 콜백을 하나 이상 추가할 수 있습니다.
UnityEvent
를 인스펙터에서 설정하는 경우 두 가지 타입의 함수 호출이 지원됩니다.
기본적으로 Monobehaviour
의 UnityEvent
는 void 함수에 동적으로 바인딩되지만, UnityEvent의 동적 호출은 인수가 4개 이하인 함수에 바인딩할 수 있으므로 꼭 그래야 하는 것은 아닙니다. 이렇게 하려면 여러 인수를 지원하는 커스텀 UnityEvent
클래스를 정의해야 합니다. 방법은 매우 쉽습니다.
[Serializable]
public class StringEvent : UnityEvent <string> {}
베이스 UnityEvent
클래스 대신 커스텀 클래스에 이 인스턴스를 추가하여 콜백을 string 함수에 동적으로 바인딩할 수 있습니다.
그런 다음 string
이 인수로 포함된 Invoke()
함수를 호출하여 콜백을 호출할 수 있습니다.
UnityEvent는 일반 정의에서 4개 이하의 인자로 정의할 수 있습니다.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.