프로퍼티 방문자는 프로퍼티 API에 빌드된 알고리즘입니다.
방문자를 사용하여 직접 수정하지 않고도 타입에 기능을 추가할 수 있습니다. 매우 일반적인 방문자를 생성하여 알고리즘 자체와 방문 프로세스를 모두 제어할 수 있습니다. 이는 일반적으로 알려진 AOT(ahead-of-time) 타입 구조에서 방문이 발생하는 방문자 패턴의 클래식 구현과는 다릅니다. 직렬화, 인스펙터와 유사한 UI 생성 등의 기능을 사용할 수 있습니다.
다음은 방문자의 기본 패턴입니다. 이는 프로퍼티 백과 프로퍼티 컴패니언 오브젝트에서 발생합니다.
다음 방법을 통해 방문자를 생성하여 프로퍼티를 가져올 수 있습니다.
Unity.Properties.PropertyVisitor
기본 클래스를 사용합니다. PropertyVisitor
를 사용하여 프로퍼티 방문자 생성에서 예시를 참조하십시오.IPropertyBagVisitor
및 IPropertyVisitor
인터페이스를 구현합니다. 로우레벨 API를 사용하여 프로퍼티 방문자 생성에서 예시를 참조하십시오.첫 번째 방법은 가장 쉬운 시작 방법입니다. 하지만 프로퍼티 백과 프로퍼티에 대한 방문 동작을 보다 광범위하게 커스터마이징하려면 더 큰 유연성과 성능 개선 가능성을 제공하는 두 번째 방법을 사용하십시오.
다음 예시에서는 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을 생성한다는 점입니다. 이로 인해 방문자가 오브젝트를 처음으로 수락할 때 실행 속도가 느려질 수 있습니다.
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.