컵 데이터 생성 에디터 툴

John Jean·2025년 8월 23일

Unity

목록 보기
19/19


씬에 배치된 컵 프리팹들을 선택하고 메뉴에서 한 번 클릭하면, 해당 위치 정보가 자동으로 CupPatternData ScriptableObject 로 저장됩니다.
즉, 에디터에서 직접 패턴 데이터를 추출할 수 있어 반복 작업을 줄이고 손쉽게 새로운 패턴을 만들 수 있습니다.


✏️ 구현 과정

🛠 핵심 아이디어

  1. 선택한 GameObject의 자식 오브젝트 좌표를 스캔한다.
  2. 좌표를 (Vector2Int 로 변환한다.
  3. 변환된 좌표를 CupPatternData ScriptableObject 에 저장한다.
  4. AssetDatabase.CreateAsset 으로 프로젝트에 저장해 재사용한다.

코드 구조

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

public class CreateCupPatternFromPrefab : MonoBehaviour
{
    [MenuItem("Tools/Cup Pattern/Generate From Selected GameObject")]
    public static void GenerateCupPatternData()
    {
        GameObject selected = Selection.activeGameObject;
        if (selected == null)
        {
            Debug.LogWarning("먼저 컵들을 포함한 GameObject를 선택하세요.");
            return;
        }

        List<Vector2Int> cupPositions = new List<Vector2Int>();

        foreach (Transform child in selected.transform)
        {
            Vector3 pos = child.position;
            Vector2Int gridPos = new Vector2Int(
                Mathf.RoundToInt(pos.x),
                Mathf.RoundToInt(pos.y)
            );
            cupPositions.Add(gridPos);
        }

        // ScriptableObject 생성
        CupPatternData pattern = ScriptableObject.CreateInstance<CupPatternData>();
        pattern.cupPositions = cupPositions;

        // 저장 경로 선택
        string path = EditorUtility.SaveFilePanelInProject(
            "Save Cup Pattern", "NewCupPattern", "asset", "패턴을 저장할 위치를 선택하세요.");

        if (!string.IsNullOrEmpty(path))
        {
            AssetDatabase.CreateAsset(pattern, path);
            AssetDatabase.SaveAssets();
            Debug.Log("CupPatternData 생성 완료!");
        }
    }
}

📖 사용 방법

씬에서 완성된 컵 패턴 프리팹을 선택한다.
Unity 메뉴에서 Tools > Cup Pattern > Generate From Selected GameObject 클릭.
저장 위치를 지정하면, 패턴 데이터 에셋이 자동 생성된다.

이후 게임 매니저나 CupStackManager에서 불러와 바로 사용할 수 있습니다.

profile
크래프톤 6기 정글러

0개의 댓글