[Memo-Unity] 12. ScriptableObject - CreateAssetMenu
์นดํ ๊ณ ๋ฆฌ: MeMo Unity
ํ๊ทธ: C#, MeMo Unity
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
: ๋ฉ๋ด ํญ๋ชฉ์ ์์น์ ๋๋ค.
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 ์ ์ฉ์ํค๊ธฐ
์ ๋ฆฌ
ScriptableObject : ๋ฐ์ดํฐ๋ฅผ ์ ์ฅ ๊ด๋ฆฌํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ๋ฐ์ดํฐ ์ปจํ ์ด๋
[Unity] SO (ScriptableObject)
TOP
๋๊ธ๋จ๊ธฐ๊ธฐ