ScriptableObject 클래스를 사용하면 스크립트 인스턴스에 관계없이 대량의 공유 데이터를 저장할 수 있습니다. 이 클래스를 이름이 비슷하지만 용도가 다른 에디터 클래스인 SerializableObject와 혼동하면 안 됩니다. 예를 들어 정수 백만 개의 배열이 포함된 스크립트로 프리팹을 만들었다고 생각해 보십시오. 이 배열은 4MB의 메모리를 사용하고 프리팹이 소유합니다. 이 프리팹을 인스턴스화할 때마다 배열이 복제되므로, 게임 오브젝트 10개를 생성하면 10개 인스턴스에 40MB의 배열 데이터가 생깁니다.
Unity는 Vector3와 같이 프리미티브 타입, 스트링, 배열, 리스트, Unity에 특화된 타입뿐만 아니라 복사 처럼 선언된 오브젝트에 속한 Serializable 속성을 가진 커스텀 클래스 모두를 시리얼라이즈 합니다. 이는 사용자가 ScriptableObject를 생성하고 백만 개의 정수를 배열에 저장하면 배열이 해당 인스턴스와 함께 저장됨을 뜻합니다. 인스턴스는 개별 데이터를 보유하는 것으로 판단됩니다. ScriptableObject 필드나 MonoBehaviour, Mesh, GameObject와 같은 모든 UnityEngine.Object 필드는 값이 아니라 _ 레퍼런스_로 저장됩니다. 백만 개의 정수가 있는 ScriptableObject를 참조하는 스크립트가 있다면 Unity는 스크립트 데이터에 ScriptableObject에 대한 레퍼런스만 저장합니다. 그리고 ScriptableObject는 배열을 저장합니다. ScriptableObject에 대한 레퍼런스를 갖는 4MB의 프리팹 인스턴스 10개는 그 데이터 총합이 다른 예처럼 40MB가 아니라 약 4MB입니다.
ScriptableObject는 값의 복제를 피해 메모리 소비를 줄이려는 경우에 사용하지만, 장착형 데이터 집합을 정의할 때도 활용할 수 있습니다. RPG 게임의 NPC 샵이 그 예입니다. 사용자 지정 ShopContents ScriptableObject에서 여러 에셋을 생성하고 각각 구매 가능한 항목을 정의할 수 있습니다. 게임에 존이 세 곳 있는 경우 각 존은 서로 다른 등급의 아이템을 제공할 수 있습니다. 샵의 스크립트는 어떤 항목이 가능할지 정의하는 ShopContents 오브젝트를 참조합니다. 예시는 스크립팅 레퍼런스를 참조하십시오.
ScriptableObject 파생 클래스를 정의하고 나면 클래스를 사용해 커스텀 에셋을 손쉽게 생성하도록 CreateAssetMenu 속성을 사용할 수 있습니다.
팁: 인스펙터에서 ScriptableObject 레퍼런스로 작업할 때 레퍼런스 필드를 더블 클릭해서 ScriptableObject의 인스펙터를 열 수 있습니다. 또한 인스펙터가 나타내는 데이터의 관리에 도움이 되도록 모양을 정의해 커스텀 에디터를 만들 수도 있습니다.
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.