[Memo-Unity] 12. ScriptableObject - CreateAssetMenu

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: ,


ScriptableObject - CreateAssetMenu


ScriptableObject - CreateAssetMenu








1. ScriptableObject

  • ์Šคํฌ๋ฆฝํ„ฐ๋ธ” ์˜ค๋ธŒ์ ํŠธ๋Š” Unity์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ์œ ์—ฐํ•œ ๋ฐ์ดํ„ฐ ์ปจํ…Œ์ด๋„ˆ์ž…๋‹ˆ๋‹ค.
  • ๊ฒŒ์ž„์—์„œ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ ๋˜๋Š” ์„ค์ •์„ ์ €์žฅํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ
  • ์ฝ”๋“œ์™€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌ
  • ์Šคํฌ๋ฆฝํŠธ ์ƒ์„ฑ ์‹œ ์ƒ์†๋œ Monobehaviour ๋ฅผ ์ง€์šฐ๊ณ  ScriptableObject๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค.

AttackSO ๋ฌด๊ธฐ ๋ฐ์ดํ„ฐ

AttackSO


[CreateAssetMenu(fileName = "DefaultAttackData", menuName = "TopDownController/Attacks/Default", order = 0)]
public class AttackSO : ScriptableObject
{
    [Header("Attack Info")]
    public float size;
    public float delay;
    public float power;
    public float speed;
    public LayerMask target;

    [Header("Knock Back Info")]
    public bool isOnKnockback;
    public float knockbackPower;
    public float knockbackTime;
}

RangedAttackData

RangedAttackData ์›๊ฑฐ๋ฆฌ ๋ฌด๊ธฐ ๋ฐ์ดํ„ฐ

RangedAttackData

[CreateAssetMenu(fileName = "RangedAttackData", menuName = "TopDownController/Attacks/Ranged", order = 1)]
public class RangedAttackData : AttackSO
{
    [Header("Ranged Attack Data")]
    public string bulletNameTag;
    public float duration;
    public float spread;
    public int numberofProjectilesPerShot;
    public float multipleProjectilesAngel;
    public Color projectileColor;
}








2. CreateAssetMenu

CreateAssetMenu Asset ์—์„œ ์ƒ์„ฑ์‹œ ๋ฉ”๋‰ด์— ํ‘œ์‹œ

AttackSO
[CreateAssetMenu(fileName = "DefaultAttackData", menuName = "TopDownController/Attacks/Default", order = 0)]

RangedAttackData
[CreateAssetMenu(fileName = "RangedAttackData", menuName = "TopDownController/Attacks/Ranged", order = 1)]

  • fileName : ์ด ์œ ํ˜•์˜ ์ƒˆ๋กœ ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ํŒŒ์ผ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค.
  • menuName : ์œ ๋‹ˆํ‹ฐ ๋ฉ”๋‰ด์— ํ‘œ๊ธฐ๋˜๋Š” ์ด๋ฆ„
  • order : ๋ฉ”๋‰ด ํ•ญ๋ชฉ์˜ ์œ„์น˜์ž…๋‹ˆ๋‹ค.

image








3. CharacterStatsHandler

CharacterStatsHandler Player ์บ๋ฆญํ„ฐ ์ •๋ณด, ๋ฌด๊ธฐSO ์ •๋ณด

CharacterStatsHandler

public class CharacterStatsHandler : MonoBehaviour
{
    [SerializeField] private CharacterStats baseStats;
    public CharacterStats CurrentStates { get; private set; }
    //๋‚˜์ค‘์— ๋งŽ์€ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋“ค์„ ์‚ฌ์šฉํ•  ๋•Œ List
    public List<CharacterStats> statsModifiers = new List<CharacterStats>();

    private void Awake()
    {
        UpdateCharacterStats();
    }

    private void UpdateCharacterStats()
    {
        AttackSO attackSO = null;
        if (baseStats.attackSO != null)
        {
            attackSO = Instantiate(baseStats.attackSO);
        }

        CurrentStates = new CharacterStats { attackSO = attackSO };
        // TODO
        CurrentStates.statsChangeType = baseStats.statsChangeType;
        CurrentStates.maxHealth = baseStats.maxHealth;
        CurrentStates.speed = baseStats.speed;
    }
}

CharacterStat

CharacterStats ํ”Œ๋ ˆ์ด์–ด์˜ ์ •๋ณด

using System;
using UnityEngine;

public enum StatsChangeType
{
    Add,
    Multiple,
    Override,
}
// ํด๋ž˜์Šค Serializabel ์ ์šฉ์‹œํ‚ค๊ธฐ
[Serializable]  
public class CharacterStats
{
    public StatsChangeType statsChangeType;
    [Range(1, 100)] public int maxHealth;
    [Range(1f, 20f)] public float speed;
    public AttackSO attackSO;
}

ํด๋ž˜์Šค Serializabel ์ ์šฉ์‹œํ‚ค๊ธฐ
image








์ •๋ฆฌ

ScriptableObject : ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ ์ปจํ…Œ์ด๋„ˆ

image


[Unity] SO (ScriptableObject) TOP


๐Ÿ“”

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ