๐ŸŽฎ Unity Study/Unity

[Unity] Scriptable Object ์‚ฌ์šฉ๋ฒ•, ์žฅ์ 

ibelieveinme 2023. 4. 12. 14:44
728x90

* Scriptable Object?

ํŒŒ์ผ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ์ฒด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๋‹ค. Script๋ฅผ ์—์…‹์œผ๋กœ ๋งŒ๋“ค์–ด์„œ ๊ฐ’์„ ์ €์žฅ ๋ฐ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

ํŠนํžˆ ์—์…‹์œผ๋กœ ๋งŒ๋“ค๊ณ  AssetBundle์ด๋‚˜ Addressable Asset์œผ๋กœ ๊ด€๋ฆฌํ•˜๋ฉด ๋ฐ์ดํ„ฐ ์ˆ˜์ •์‹œ ๋นŒ๋“œ&๋ฐฐํฌ๊ฐ€ ํ•„์š” ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ํšจ์œจ์ด ๊ทน๋Œ€ํ™” ๋œ๋‹ค.(Scriptable Object ์žฅ์ )


* Scriptable Object ์‚ฌ์šฉ๋ฒ•

 

1. ์ •์˜ ๋ฐ ์—์…‹ ์ƒ์„ฑ

using UnityEngine;

[CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/SpawnManagerScriptableObject", order = 1)]
public class SpawnManagerScriptableObject : ScriptableObject
{
    public string prefabName;

    public int numberOfPrefabsToCreate;
    public Vector3[] spawnPoints;
}

1) ScriptableObejct ํด๋ž˜์Šค ์ƒ์†๋ฐ›๊ธฐ

2) CreateAssetMenu ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ƒ๋‹จ ๋ฉ”๋‰ด์—์„œ Asset์„ ์ƒ์„ฑํ•˜๊ฒŒ ํ•˜๊ธฐ.

 

2. Scriptable Object ์—ฐ๊ฒฐ

using UnityEngine;

public class Spawner : MonoBehaviour
{
    // The GameObject to instantiate.
    public GameObject entityToSpawn;

    // An instance of the ScriptableObject defined above.
    public SpawnManagerScriptableObject spawnManagerValues;

    // This will be appended to the name of the created entities and increment when each is created.
    int instanceNumber = 1;

    void Start()
    {
        SpawnEntities();
    }

    void SpawnEntities()
    {
        int currentSpawnPointIndex = 0;

        for (int i = 0; i < spawnManagerValues.numberOfPrefabsToCreate; i++)
        {
            // Creates an instance of the prefab at the current spawn point.
            GameObject currentEntity = Instantiate(entityToSpawn, spawnManagerValues.spawnPoints[currentSpawnPointIndex], Quaternion.identity);

            // Sets the name of the instantiated entity to be the string defined in the ScriptableObject and then appends it with a unique number. 
            currentEntity.name = spawnManagerValues.prefabName + instanceNumber;

            // Moves to the next spawn point index. If it goes out of range, it wraps back to the start.
            currentSpawnPointIndex = (currentSpawnPointIndex + 1) % spawnManagerValues.spawnPoints.Length;

            instanceNumber++;
        }
    }
}

public SpawnManagerScriptableObject spawnManagerValues; ๋กœ Scriptable Object ์—ฐ๊ฒฐํ•ด์„œ ๊ฐ’ ์‚ฌ์šฉํ•˜๊ธฐ.


* Unity Documentation ์ฐธ๊ณ 

https://docs.unity3d.com/kr/2022.2/Manual/class-ScriptableObject.html

 

ScriptableObject - Unity ๋งค๋‰ด์–ผ

ScriptableObject๋Š” ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค์™€๋Š” ๋ณ„๋„๋กœ ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ์ปจํ…Œ์ด๋„ˆ์ž…๋‹ˆ๋‹ค. ScriptableObject์˜ ์ฃผ์š” ์‚ฌ์šฉ ์‚ฌ๋ก€ ์ค‘ ํ•˜๋‚˜๋Š” ๊ฐ’์˜ ์‚ฌ๋ณธ์ด ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜

docs.unity3d.com

 

728x90

'๐ŸŽฎ Unity Study > Unity' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Unity] IL2CPP, Mono, JIT  (1) 2023.04.17
[Unity] Texture Compression ๋ฐฉ์‹  (0) 2023.04.13
[Unity] Editor Programming  (0) 2023.03.28
[Unity] Advanced InputField ์‚ฌ์šฉ๋ฒ•  (0) 2023.02.01
[Unity] UGUI - Anchor(๊ธฐ์ค€), Pivot(๋‚˜)  (0) 2022.12.15