โ ScriptableObject๋?
๐ ๊ฐ๋ ์ ๋ฆฌ
- ScriptableObject๋ MonoBehaviour์ฒ๋ผ Unity์์ ์ฌ์ฉํ๋ ํด๋์ค์ง๋ง,
- ์ฌ์ ๋ถ๋ ์ปดํฌ๋ํธ๊ฐ ์๋๊ณ , ๋ ๋ฆฝ์ ์ธ ๋ฐ์ดํฐ ๊ฐ์ฒด์ผ.
- ๋ฐ์ดํฐ๋ฅผ ์์ฐ(Asset)์ผ๋ก ๋ง๋ค์ด ์ ์ฅํ๊ณ ๊ณต์ ํ ์ ์๋ ๋ฐฉ์์ ์ ๊ณตํด.
โ ์ธ์ ์ฌ์ฉํ๋์?
์ฌ๋ฌ ๊ฐ์ฒด๊ฐ ๋์ผํ ๋ฐ์ดํฐ๋ฅผ ์ฐธ์กฐํ ๋ | ์: ์คํฏ, ์ค์ ๊ฐ, ์์ดํ ์ ๋ณด ๋ฑ |
ํ๋ฆฌํน์ด๋ ์ฝ๋์ ๋ ๋ฆฝ์ ์ผ๋ก ๋ฐ์ดํฐ ๊ด๋ฆฌํ๊ณ ์ถ์ ๋ | ์ค์ ๋ณ๊ฒฝ์ด ์ฝ๊ณ ์ ์ง๋ณด์ ํธํจ |
์ธ์คํํฐ์์ ์ง์ ๋ฐ์ดํฐ ์ ๋ ฅํ๊ณ ์ ์ฅํ๊ณ ์ถ์ ๋ | ์ปค์คํ ์๋ํฐ ์ฐ๋๋ ์ฌ์ |
โ ๊ฐ๋จํ ์์ : ์บ๋ฆญํฐ ๋ฅ๋ ฅ์น ๋ฐ์ดํฐ ๋ง๋ค๊ธฐ
1. CharacterStats.cs
using UnityEngine;
[CreateAssetMenu(fileName = "NewCharacterStats", menuName = "ScriptableObjects/CharacterStats")]
public class CharacterStats : ScriptableObject
{
public string characterName;
public int maxHP;
public int attackPower;
public float moveSpeed;
}
2. ์์ฑ ๋ฐฉ๋ฒ
- Unity ์๋ํฐ์์ ์ฐํด๋ฆญ → Create → ScriptableObjects → CharacterStats
- ์ด๋ฆ์ WarriorStats, MageStats ๋ฑ์ผ๋ก ์ง์ ํ๊ณ ๊ฐ ์ ๋ ฅ
3. ์ฌ์ฉํ๋ ์คํฌ๋ฆฝํธ
public class Player : MonoBehaviour
{
public CharacterStats stats;
private void Start()
{
Debug.Log($"{stats.characterName}์ ๊ณต๊ฒฉ๋ ฅ: {stats.attackPower}");
}
}
- Player ์ค๋ธ์ ํธ์ ๋ถ์ด๊ณ , stats ํ๋์ ScriptableObject ์์ฐ์ ํ ๋นํ๋ฉด ๋!
โ ScriptableObject vs MonoBehaviour
์ฌ์ ์กด์ฌ | โ (์ฌ์ ์ ๋ถ์) | โ (์ค๋ธ์ ํธ์ ๋ถ์) |
๋ฉ๋ชจ๋ฆฌ ํจ์จ | โ ๋งค์ฐ ์ข์ | โ ๋ฐ๋ณต ์ฌ์ฉ ์ ๋นํจ์จ |
์ญํ | ๋ฐ์ดํฐ ์ ์ฅ, ์ค์ | ๋์ ๋ก์ง, ์ด๋ฒคํธ ์ฒ๋ฆฌ |
์ฌ์ฌ์ฉ | ์ฌ๋ฌ ์ค๋ธ์ ํธ๊ฐ ์ฐธ์กฐ ๊ฐ๋ฅ | ์ค๋ธ์ ํธ๋ง๋ค ๊ฐ๋ณ |
โ ์ฅ์ ์ ๋ฆฌ
- โ๏ธ ์ฌ ๋ ๋ฆฝ์ ๋ฐ์ดํฐ ๊ด๋ฆฌ ๊ฐ๋ฅ (๊ฒ์ ๋ฐธ๋ฐ์ค ์กฐ์ ์ ์ ๋ฆฌ)
- โ๏ธ ํ๋ฆฌํน ์ฌ์ฌ์ฉ ์ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๊ณต์ ํ ์ ์์ด ๋ฉ๋ชจ๋ฆฌ ์ ์ฝ
- โ๏ธ ๋ฒ์ ๊ด๋ฆฌ์ ์ ๋ฆฌ (๋ฐ์ดํฐ๊ฐ ํ์ผ๋ก ์กด์ฌํ๋๊น Git์๋ ๋ฑ!)
- โ๏ธ ์ ๋ ํ ์คํธ์ ์ ํฉ
โ ๋จ์ or ์ฃผ์์ฌํญ
- ScriptableObject๋ **์ํ(state)**๋ฅผ ์ ์ฅํ๊ธฐ๋ณด๋จ ์ค์ ๊ฐ(config) ์ ์ฅ์ ์ ํฉํจ
- ๋ฐํ์์ ๊ฐ์ ๋ฐ๊พธ๋ฉด ์๋ํฐ์ ์ ์ฅ๋ ์์ฐ์ด ์์ ๋ ์๋ ์์ผ๋ฏ๋ก ์ฃผ์
โ ์ค์ํ ๋น์
- MonoBehaviour๋ "๊ฒ์์ ๋์ค๋ ๋ฐฐ์ฐ(์ค๋ธ์ ํธ)"
- ScriptableObject๋ "๊ทธ ๋ฐฐ์ฐ๊ฐ ์ฐธ๊ณ ํ๋ ๋๋ณธ(๋ฐ์ดํฐ)"
์ผ์ ๋ฐ์ดํฐ๋ก ํ์ฉํ๋๊ฒ์ ๋๋ฌด ์ข์๋ณด์ธ๋ค ์ฒ์ ์ด๊ฑธ ๋ดค์๋ ์ ์ด๊ฑธ ๋ชฐ๋์๊น? ์ถ์๋ค ์์ฒญ ์ ์ฉํ๊ฒ ๋ง์ด ์ฌ์ฉ๋ ๋ฏํ๋ค ๋ณดํต ์ธ๋ฒคํ ๋ฆฌ ์์คํ ์ ๋ง์ด ์ฌ์ฉํ๋๊ฑฐ ๊ฐ๋ค ์คํฌ/์์ดํ ๋ฐ์ดํฐ๋ฑ๋ฑ..
๊ฐ๋จํ๊ฒ ์ ๋ํฐ๋ก ๋ญ๋ผ๋ ๋ง๋ค์ด ๋ณด์!
๋์ถฉ ์์ฝํ์๋ฉด
๐ฆ ์ ์ฒด ๊ตฌ์ฑ ์์ฝ
1. ScriptableObject ๊ธฐ๋ฐ ์์ดํ ๋ฐ์ดํฐ ๊ด๋ฆฌ
- Item_ScriptableObject ํด๋์ค ์์ฑ
- ์ธ์คํํฐ์์ ์ฌ๋ฌ ๊ฐ์ ์์ดํ ๋ฐ์ดํฐ๋ฅผ .asset ํ์ผ๋ก ์์ฑ (ItemData1~4)
- ๊ฐ ์์ดํ ์ ๊ณ ์ ๋ฒํธ, ์ด๋ฆ, ์ค๋ช , ํ์ ๋ฑ์ ์ ๋ณด๋ฅผ ๊ฐ์ง
2. ์์ดํ ์ค๋ธ์ ํธ (Item)
- ItemObject ์คํฌ๋ฆฝํธ ๋ถ์ฐฉ
- ๊ฐ๊ฐ์ ์ค๋ธ์ ํธ๊ฐ ํ๋์ ScriptableObject ์ฐธ์กฐ
- Tag๋ "Item"์ผ๋ก ์ง์
3. ํ๋ ์ด์ด
- WASD ํค ์ ๋ ฅ์ผ๋ก 2D ์ด๋
- Rigidbody2D + Collider2D ๊ตฌ์ฑ
- Item๊ณผ ์ถฉ๋ ์ ํด๋น ๋ฐ์ดํฐ ํ๋
4. UI ์์คํ
- ItemUIManager ์คํฌ๋ฆฝํธ๋ก Text ์ปดํฌ๋ํธ ์ ์ด
- ์์ดํ ์ ๋ณด ํ์ ๋ฐ ์ด๊ธฐํ ๋ด๋น
ํ ..๊ทผ๋ฐ ์ฌ์ค ๊ณ ์ ๋ฒํธ๋ ํ์ ์์์๊ฑฐ๊ฐ๋ค. ์ด๋ฐ์์ผ๋ก ์์ดํ ์ ๋ณด๋ฅผ ์ ์ฅํด์ ์ฌ์ฉํ ์์๋๊ฑธ ์๊ฒ๋๋ค!
๋๋จธ์ง ์ฝ๋
Item_ScriptableObject.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName ="ItemData", menuName = "ScriptableObject/ItemData")]
public class Item_ScriptableObject : ScriptableObject
{
public int itemNum;
public string itemName;
public string iteminfo;
public int itemPower;
}
ItemObject.cs - ์์ดํ ์ ๋ค์ด๊ฐ๋ ์คํฌ๋ฆฝํธ - Tag Item์ผ๋ก ์ถ๊ฐํด์ค์ผํ๋ค.
using UnityEngine;
public class ItemObject : MonoBehaviour
{
public Item_ScriptableObject itemData;
}
ItemUIManager.cs ์ด๋ค ์ฌ๊ธฐ์ ์์ดํ ์ ์ค๋ช ๊ณผ ์ ๋ณด๋ฅผ ์ถ๋ ฅ
using UnityEngine;
using UnityEngine.UI;
public class ItemUIManager : MonoBehaviour
{
public Text nameText;
public Text infoText;
public Text powerText;
public GameObject panel;
private void Start()
{
panel.SetActive(false);
}
public void ShowItemInfo(Item_ScriptableObject item)
{
panel.SetActive(true);
nameText.text = $"์ด๋ฆ: {item.itemName}";
infoText.text = $"์ค๋ช
: {item.iteminfo}";
powerText.text = $"ํ์: {item.itemPower}";
}
public void HideInfo()
{
panel.SetActive(false);
nameText.text = "";
infoText.text = "";
powerText.text = "";
}
}
Player.cs ์ด๋ค. Item๊ณผ ๋ฟ๋๋ค๋ฉด UI๋งค๋์ ๋ฅผ ์ด์ฉํด์ ์์ดํ ์ ์ ๋ณด๋ฅผ ๋ฐ์์จ๋ค.
using UnityEngine;
public class Player : MonoBehaviour
{
public ItemUIManager uiManager;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Item"))
{
ItemObject itemObj = other.GetComponent<ItemObject>();
if (itemObj != null && itemObj.itemData != null)
{
uiManager.ShowItemInfo(itemObj.itemData);
}
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Item"))
{
uiManager.HideInfo();
}
}
}
๋๋จธ์ง ์ฝ๋ 1๊ฐ๋ PlayerMovement์ธ๋ฐ ๊ฐ๋จํ๋๊น ๋์ด๊ฐ๊ฒ ๋ค.
'Unity' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Unity - (Template vs Template Method Pattern) vs (Class vs Instance) (0) | 2025.07.03 |
---|---|
Unity - Template Method Pattern(ํ ํ๋ฆฟ ๋ฉ์๋ ํจํด) vs virtual (0) | 2025.07.03 |
Unity - Update(), FixedUpdate(), LateUpdate() (0) | 2025.07.01 |
์ ๋ํฐ ๋จธ์ ๋ฌ๋ 2(Unity ML-Agents) (2) | 2024.03.19 |
์ ๋ํฐ ๋จธ์ ๋ฌ๋ 1(Unity ML-Agents) (13) | 2024.03.19 |