새로운 UI 시스템은 SendMessage를 대체하기 위해 설계된 메시징 시스템을 사용합니다. 시스템은 순수한 C#이며 메시지 전송에 존재하는 일부 문제를 해결하는 것을 목표로 하고 있습니다. 시스템은 컴포넌트가 메시징 시스템에서 콜백을 수신할 수 있다는 것을 나타내기 위해 MonoBehaviour에 구현할 수 있는 커스텀 인터페이스를 사용하여 작동합니다. 호출로 타겟 게임 오브젝트가 지정되면 특정한 인터페이스를 구현한 게임 오브젝트의 모든 컴포넌트로 호출이 이뤄집니다. 메시징 시스템은 커스텀 사용자 데이터를 전달하게 허용하고 이벤트가 전파해야 하는 게임 오브젝트 계층 구조를 통해서도 전달됩니다. 이것은 지정된 게임 오브젝트에서 그냥 실행돼야 하거나 자식과 부모 오브젝트를 대상으로도 실행돼야 합니다. 이 외에도 메시징 프레임워크는 주어진 메시징 인터페이스를 구현하는 게임 오브젝트를 검색하여 찾는 헬퍼 함수를 제공합니다.
메시징 시스템은 일반용으로 UI 시스템뿐만 아니라 일반적인 게임 코드에도 사용할 수 있게 설계되어 있습니다. 커스텀 메시징 이벤트를 추가하는 것은 비교적 간단한 일이며 UI 시스템은 모든 이벤트 처리를 위해 사용하는 것과 같은 프레임워크를 통해 이벤트를 추가합니다.
커스텀 메시지를 정의하는 것은 비교적 간단합니다. UnityEngine.EventSystems 네임스페이스에 ‘IEventSystemHandler’라는 베이스 인터페이스가 있습니다. 앞으로 이어지는 것은 무엇이든 메시징 시스템으로부터 이벤트를 받는 대상으로 고려할 수 있습니다.
public interface ICustomMessageTarget : IEventSystemHandler
{
// functions that can be called via the messaging system
void Message1();
void Message2();
}
일단 인터페이스가 정의되면 MonoBehaviour에 의해 구현할 수 있습니다. 구현되면 MonoBehaviour 게임 오브젝트에 대해 주어진 메시지가 표시될 때 실행되는 함수를 정의합니다.
public class CustomMessageTarget : MonoBehaviour, ICustomMessageTarget
{
public void Message1()
{
Debug.Log ("Message 1 received");
}
public void Message2()
{
Debug.Log ("Message 2 received");
}
}
스크립트가 존재하면 스크립트는 메시지를 표시해야 한다는 메시지를 받을 수 있습니다. 이것은 일반적으로 느슨하게 연결되어 발생하는 몇 가지 이벤트에 대한 응답에서 확인할 수 있습니다. 예를 들어, 애플리케이션에서 PointerEnter와 PointerExit 같은 다양한 사용자 입력이 발생하면 UI 시스템은 이에 대해 이벤트를 표시해야 합니다.
static helper 클래스는 메시지를 보내기 위해 존재합니다. 인수로서 메시지를 위한 대상 오브젝트, 일부 사용자 특정 데이터, 타겟으로 하려는 메시지 인터페이스의 특정 기능에 매핑할 함수가 필요합니다.
ExecuteEvents.Execute<ICustomMessageTarget>(target, null, (x,y)=>x.Message1());
코드는 ICustomMessageTarget 인터페이스를 구현하는 게임 오브젝트 대상의 모든 컴포넌트에 Message1 함수를 실행합니다. ExecuteEvents 클래스의 스크립팅 문서에서는 이와 같은 자식이나 부모에서 실행하는 실행 함수의 다른 형태를 다루고 있습니다.
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.