# 프로퍼티 방문자

> Understand the role of property visitors and the performance considerations when using them.

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

## 개념##create-a-property-visitor-to-get-properties

직접 수정하지 않고 방문자를 사용하여 유형에 기능을 추가할 수 있습니다. 매우 일반적인 방문자를 생성하여 알고리즘 자체와 방문 프로세스를 모두 제어할 수 있습니다. 이는 일반적으로 미리 알려진 유형의 구조에서 방문이 발생하는 방문자 패턴의 기존 구현과는 다릅니다. [직렬화](https://docs.unity3d.com/Packages/com.unity.serialization@3.1/manual/index.html), 인스펙터와 유사한 UI 생성 등의 기능을 지원합니다.

다음은 방문자의 기본 패턴입니다. 이는 프로퍼티 백과 프로퍼티 컴패니언 오브젝트에서 발생합니다.

1. 한 유형의 인스턴스가 방문자를 **수락**합니다.
2. 방문자는 인스턴스의 프로퍼티 백을 **방문**합니다.
3. 프로퍼티 백은 해당 프로퍼티를 반복하여 방문자를 **수락**할 수 있습니다.

## 프로퍼티를 가져오는 프로퍼티 방문자 생성##performance-considerations

다음 방식을 통해 프로퍼티를 가져오는 방문자를 생성할 수 있습니다.

* [`Unity.Properties.PropertyVisitor`](/engine/6000.0/script-reference/unity-properties/property-visitor.md) 기본 클래스를 사용합니다. [`PropertyVisitor`를 사용하여 프로퍼티 방문자 생성](/engine/6000.0/manual/scripting/object-oriented-development/properties/property-visitors-propertyvisitor.md)에서 예시를 참조하십시오.
* [`IPropertyBagVisitor`](/engine/6000.0/script-reference/unity-properties/i-property-bag-visitor.md) 및 [`IPropertyVisitor`](/engine/6000.0/script-reference/unity-properties/i-property-visitor.md) 인터페이스를 구현합니다. [저수준 API를 사용하여 프로퍼티 방문자 생성](/engine/6000.0/manual/scripting/object-oriented-development/properties/property-visitors-low-level-api.md)에서 예시를 참조하십시오.

첫 번째 방식은 가장 쉬운 방법입니다. 하지만 프로퍼티 백과 프로퍼티 모두에 대한 방문 동작을 더 광범위하게 커스터마이즈하려면 유연성이 더 뛰어나고 성능 개선 가능성이 있는 두 번째 방식을 사용하십시오.

다음 예시에서는 [`PropertyVisitor`](/engine/6000.0/script-reference/unity-properties/property-visitor.md) 클래스를 사용하여 특정 속성으로 태그가 지정된 특정 유형의 프로퍼티를 가져오는 간단한 방문자를 생성합니다.

```lang-csharp
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` 인터페이스를 사용하여 방문자를 생성하는 동일한 예시입니다.

```lang-csharp
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));
        }
    }
}
```

저수준 방문자는 프로퍼티 백의 모든 프로퍼티를 반복해서 값을 추출할 필요가 없으므로 성능이 더 뛰어납니다. 저수준 방문자를 사용하여 프로퍼티 백에 속하지 않는 프로퍼티를 방문할 수도 있습니다.

## 성능 고려 사항##additional-resources

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

## 추가 리소스

* [프로퍼티 백](/engine/6000.0/manual/scripting/object-oriented-development/properties/property-bags.md)
* [프로퍼티 경로](/engine/6000.0/manual/scripting/object-oriented-development/properties/property-paths.md)
* [`PropertyVisitor`를 사용하여 프로퍼티 방문자 생성](/engine/6000.0/manual/scripting/object-oriented-development/properties/property-visitors-propertyvisitor.md)
* [저수준 API를 사용하여 프로퍼티 방문자 생성](/engine/6000.0/manual/scripting/object-oriented-development/properties/property-visitors-low-level-api.md)
