반응형
같은 데이터를 여러 컴포넌트나 씬에서 참조해야 한다면,
ScriptableObject를 적극 활용하도록 하자.
ScriptableObject는 게임 오브젝트 없이,
독립적으로 데이터를 관리할 수 있는 객체이다.
.asset 파일로 저장되어, 에디터와 런타임에서 사용 가능한 데이터 컨테이너 역할을 한다.
아래에서 생성 방법과 활용 예시를 알아보도록 하자.
① ScriptableObject 생성하기
Project에서 Create > Scripting > ScriptableObject Script를 생성해준다.
위와 같이 스크립트가 생성된다.
+ 직접 스크립트를 생성한 후, ScriptableObject 클래스를 상속받아도 된다.
using UnityEngine;
[CreateAssetMenu(fileName = "NewScriptableObjectScript", menuName = "Scriptable Objects/NewScriptableObjectScript")]
public class NewScriptableObjectScript : ScriptableObject
{
}
생성된 스크립트의 초기 내용은 다음과 같다.
fileName | 파일을 생성할 때 자동으로 설정되는 이름 (미설정 시, 클래스 이름이 기본값) menuName | ScriptableObject를 생성하는 메뉴 경로 (미설정 시, 클래스 이름이 기본값) |
menuName 경로에 따라 ScriptableObject를 생성한다.
② 데이터 추가하기
using UnityEngine;
using System;
[CreateAssetMenu(fileName = "NewScriptableObjectScript", menuName = "Scriptable Objects/NewScriptableObjectScript")]
public class NewScriptableObjectScript : ScriptableObject
{
[Header("Component")]
public GameObject newGameObject;
public GameObject[] newGameObjectArray;
public Transform newTransform;
public Transform[] newTransformArray;
[Header("Value")]
public int newInt;
public int[] newIntArray;
public float newFloat;
public float[] newFloatArray;
public string newString;
public string[] newStringArray;
public NewStruct newStruct;
public NewStruct[] newStructArray;
[Serializable]
public struct NewStruct
{
public string value;
}
}
여러 자료형을 스크립트에 추가해보도록 하자.
그러면, 에디터 Inspector에서 값을 확인하고 수정할 수 있다.
③ 데이터 사용하기
using UnityEngine;
public class SampleScript : MonoBehaviour
{
public NewScriptableObjectScript scriptableObject; //Inspector에서 직접 ScriptableObject를 드래그해서 할당
public void Awake()
{
Debug.Log(scriptableObject.newGameObject);
Debug.Log(scriptableObject.newTransform);
Debug.Log(scriptableObject.newInt);
Debug.Log(scriptableObject.newFloat);
Debug.Log(scriptableObject.newString);
Debug.Log(scriptableObject.newStruct.value);
scriptableObject.newInt = 10; // .asset 파일을 직접 수정
}
}
위와 같은 방법으로 값을 참조할 수 있고, 읽기 뿐만 아니라 할당할 수도 있다.
하지만, 값을 할당하면 .asset 파일이 직접 수정되므로,
런타임에서도 수정된 값이 반영된다.
using UnityEngine;
public class SampleScript : MonoBehaviour
{
public NewScriptableObjectScript scriptableObject; //Inspector에서 직접 ScriptableObject를 드래그해서 할당
public void Awake()
{
NewScriptableObjectScript cloneScriptableObject = Instantiate(scriptableObject); // 런타임 전용 복사본 생성
Debug.Log(cloneScriptableObject.newGameObject);
Debug.Log(cloneScriptableObject.newTransform);
Debug.Log(cloneScriptableObject.newInt);
Debug.Log(cloneScriptableObject.newFloat);
Debug.Log(cloneScriptableObject.newString);
Debug.Log(cloneScriptableObject.newStruct.value);
cloneScriptableObject.newInt = 10; // 수정해도 .asset 파일에 반영 X
}
}
따라서, 복사본을 생성하는 방법을 추천한다.
ScriptableObject를 활용하면 흩어져 있던 데이터를 깔끔하게 정리할 수 있어, 프로젝트의 구조가 훨씬 단순해진다.
규모가 커질수록 유지보수가 어려워지는데, 이럴 때 진가를 발휘할 수 있을 것으로 보인다.
반응형
'[Unity]' 카테고리의 다른 글
[Unity] 쓰레드에 무거운 작업 떠넘기기 (0) | 2025.05.04 |
---|---|
[Unity] URP 스텐실(Stencil) 활용하기 (1) | 2024.09.05 |
[Unity] Input System으로 입력 처리하기 (35) | 2024.06.08 |
[Unity] 오브젝트 풀링을 간단히 구현해보자(ObjectPool) (38) | 2024.05.24 |
[Unity] 코루틴을 대체하자(UniTask) (43) | 2024.05.15 |
댓글