기술 자료

Unity Engine

Unity Engine

프로퍼티 방문자

Understand the role of property visitors and the performance considerations when using them.
읽는 시간 2분최근 업데이트: 한 달 전

프로퍼티 방문자는 Properties API 위에 빌드된 알고리즘입니다.

개념

직접 수정하지 않고 방문자를 사용하여 유형에 기능을 추가할 수 있습니다. 매우 일반적인 방문자를 생성하여 알고리즘 자체와 방문 프로세스를 모두 제어할 수 있습니다. 이는 일반적으로 미리 알려진 유형의 구조에서 방문이 발생하는 방문자 패턴의 기존 구현과는 다릅니다. 직렬화, 인스펙터와 유사한 UI 생성 등의 기능을 지원합니다.
다음은 방문자의 기본 패턴입니다. 이는 프로퍼티 백과 프로퍼티 컴패니언 오브젝트에서 발생합니다.
  1. 한 유형의 인스턴스가 방문자를 수락합니다.
  2. 방문자는 인스턴스의 프로퍼티 백을 방문합니다.
  3. 프로퍼티 백은 해당 프로퍼티를 반복하여 방문자를 수락할 수 있습니다.

프로퍼티를 가져오는 프로퍼티 방문자 생성

다음 방식을 통해 프로퍼티를 가져오는 방문자를 생성할 수 있습니다.
첫 번째 방식은 가장 쉬운 방법입니다. 하지만 프로퍼티 백과 프로퍼티 모두에 대한 방문 동작을 더 광범위하게 커스터마이즈하려면 유연성이 더 뛰어나고 성능 개선 가능성이 있는 두 번째 방식을 사용하십시오.
다음 예시에서는
PropertyVisitor
클래스를 사용하여 특정 속성으로 태그가 지정된 특정 유형의 프로퍼티를 가져오는 간단한 방문자를 생성합니다.
public class BindableAttribute : Attribute{}public class GatherBindablePropertiesVisitor : PropertyVisitor{ public List<PropertyPath> BindableProperties { get; set; } protected override void VisitProperty<TContainer, TValue>(Property<TContainer, TValue> property, ref TContainer container, ref TValue value) { if (property.HasAttribute<BindableAttribute>()) BindableProperties.Add(PropertyPath.AppendProperty(default, property)); }}
다음은
IPropertyBagVisitor
인터페이스를 사용하여 방문자를 생성하는 동일한 예시입니다.
public class BindableAttribute : Attribute{}public class GatherBindablePropertiesVisitor : IPropertyBagVisitor{ public List<PropertyPath> BindableProperties { get; set; } void IPropertyBagVisitor.Visit<TContainer>(IPropertyBag<TContainer> propertyBag, ref TContainer container) { // Loop through the properties of the container object. foreach (var property in propertyBag.GetProperties(ref container)) { if (property.HasAttribute<BindableAttribute>()) BindableProperties.Add(PropertyPath.AppendProperty(default, property)); } }}
저수준 방문자는 프로퍼티 백의 모든 프로퍼티를 반복해서 값을 추출할 필요가 없으므로 성능이 더 뛰어납니다. 저수준 방문자를 사용하여 프로퍼티 백에 속하지 않는 프로퍼티를 방문할 수도 있습니다.

성능 고려 사항

프로퍼티 백, 프로퍼티, 방문자는 모두 일반적인 유형을 사용하여 구현되므로 최대한 강력하게 유형을 유지할 수 있으며, 대부분의 경우 방문 시 박싱 할당을 방지할 수 있습니다. 일반적인 유형을 사용하면 메서드가 처음 호출될 때 JIT 컴파일러가 특정 메서드의 IL을 생성한다는 장점이 있습니다. 이렇게 하면 오브젝트에서 처음으로 방문자가 수락될 때 실행 속도가 느려질 수 있습니다.

추가 리소스