프로퍼티 백은 지정된 .Net 오브젝트 타입에 대한 프로퍼티 컬렉션으로, 해당 타입의 오브젝트 인스턴스에 대한 데이터에 액세스하고 설정하는 데 사용할 수 있습니다.
특정 타입에 대한 프로퍼티 백은 해당 타입의 인스턴스를 기반으로 효율적인 데이터 탐색 알고리즘을 가능하게 하는 컴패니언 오브젝트입니다. 기본적으로 Unity는 리플렉션을 사용하여 한 타입에 대한 프로퍼티 백을 생성합니다. 이 리플렉션 방식은 간편하며, 프로퍼티 백이 아직 등록되지 않은 경우 타입당 한 번만 느리게 발생합니다.
성능을 향상시키려면 타입에 [Unity.Properties.GeneratePropertyBag]
로 태그를 지정하여 코드 생성에 옵트인하면 됩니다. 또한 코드 생성을 활성화하려면 어셈블리에 [assembly: Unity.Properties.GeneratePropertyBagsForAssembly]
로 태그를 지정해야 합니다. 코드 생성된 프로퍼티 백은 도메인이 로드되면 자동으로 등록됩니다.
리플렉션 및 코드 생성 시나리오 모두에서 프로퍼티 백은 다음에 대한 프로퍼티를 생성합니다.
[SerializeField]
, [SerializeReference]
또는 [CreateProperty]
로 태그가 지정된 프라이빗 또는 내부 필드[Unity.Properties.CreateProperty]
로 태그가 지정된 공용, 프라이빗 또는 내부 프로퍼티프로퍼티 백은 [DontCreateProperty]
로 태그가 지정된 공용, 프라이빗 또는 내부 필드에 대한 프로퍼티를 생성하지 않습니다.
필드가 읽기 전용이거나 프로퍼티에 게터만 있는 경우 생성된 프로퍼티는 읽기 전용입니다.
또한 [Unity.Properties.CreateProperty(ReadOnly = true)]
를 사용하여 생성된 프로퍼티를 읽기 전용으로 만들 수도 있습니다.
편의를 위해 직렬화 속성을 사용하여 프로퍼티 백에 프로퍼티를 생성하는 것이 항상 바람직한 방법은 아닙니다. Unity의 직렬화 시스템은 필드와 자동 프로퍼티에서만 작동할 수 있기 때문에 확인을 하거나 변경 사항을 효과적으로 전파하기가 어렵습니다.
다음 예시는 Unity 직렬화 시스템과 Unity 프로퍼티 시스템을 결합한 것입니다.
using UnityEngine;
using Unity.Properties;
public class MyBehaviour : MonoBehaviour
{
// Serializations go through the field, but we don't want to create a property for it.
[SerializeField, DontCreateProperty]
private int m_Value;
// For the property bag, use the property instead of the field. This ensures that
// the value stays within the appropriate bounds.
[CreateProperty]
public int value
{
get => m_Value;
set => m_Value = value;
}
// This is a similar example, but for an auto-property.
[field: SerializeField, DontCreateProperty]
[CreateProperty]
public float floatValue { get; set; }
}
Unity 직렬화 시스템과 달리 프로퍼티 백 내의 프로퍼티는 [SerializeField]
를 사용하는 값 타입으로 인정되지 않습니다. 대신 구조체 타입은 값 타입으로 인식되는 반면, 클래스 타입은 레퍼런스로 인식됩니다.
Unity 직렬화에서 다형성이 지원되지만 명시적으로 옵트인하려면 [SerializeReference]
속성을 사용해야 합니다. 그렇지 않으면 인스턴스가 값 타입으로 직렬화됩니다. UnityEngine.Object
타입은 레퍼런스 타입으로 자동 직렬화되므로 이 규칙에서 예외라는 점에 유의하십시오.
Unity 프로퍼티는 .Net Reflection을 사용하여 강력하게 유형화된 프로퍼티 백과 프로퍼티를 생성하므로, 특정 컨테이너 타입에 대한 프로퍼티 백을 처음 요청할 때 성능 오버헤드가 발생할 수 있습니다.
리플렉션을 통해 필드 멤버에 대한 프로퍼티를 생성할 때 이러한 프로퍼티는 IL2CPP 빌드에 가비지를 할당할 수 있습니다. 이 할당은 System.Reflection.FieldInfo
를 직접 사용하기 때문에 발생하며, 이로 인해 불가피한 박싱이 발생합니다.
리플렉션을 피하려면 컴파일 중에 프로퍼티 백을 코드 생성하면 됩니다. 그러나 이 최적화로 인해 컴파일 시간이 길어질 수 있습니다. 어셈블리에 대한 코드 생성을 활성화하려면 어셈블리에 [Unity.Properties.GeneratePropertyBagsForAssemblyAttribute]
로 태그를 지정하고 개별 타입에 [Unity.Properties.GeneratePropertyBagAttribute]
로 태그를 지정합니다. 프로퍼티 백이 내부 및 프라이빗 필드와 프로퍼티에 액세스할 수 있도록 활성화하려면 타입을 partial
로 설정합니다.
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.