Files
Northbound/Assets/Scripts/Editor/MonsterPrefabSetup.cs
dal4segno 5d0ed26578 몬스터용 데이터파이프라인 개선
애니메이션 컨트롤러 및 모델 설정 기능 추가
몬스터용 애니메이션 컨트롤러 생성
2026-02-01 01:42:45 +09:00

119 lines
4.7 KiB
C#

using Northbound.Data;
using UnityEditor;
using UnityEngine;
namespace Northbound.Editor
{
public class MonsterPrefabSetup : IPrefabSetup
{
public string GetTemplateName()
{
return "MonsterTemplate";
}
public void SetupPrefab(GameObject prefab, ScriptableObject data)
{
if (!(data is MonsterData monsterData))
{
Debug.LogWarning($"[MonsterPrefabSetup] Expected MonsterData, got {data.GetType().Name}");
return;
}
var monsterDataComponent = prefab.GetComponent<MonsterDataComponent>();
if (monsterDataComponent != null)
{
monsterDataComponent.monsterData = monsterData;
monsterDataComponent.ApplyMonsterData();
}
var animationController = prefab.GetComponent<MonsterAnimationController>();
if (animationController == null)
{
animationController = prefab.AddComponent<MonsterAnimationController>();
Debug.Log($"[MonsterPrefabSetup] Added MonsterAnimationController component");
}
if (!string.IsNullOrEmpty(monsterData.modelPath))
{
RemoveOldModel(prefab);
if (monsterData.modelPath.ToLower().EndsWith(".fbx"))
{
GameObject fbxModel = AssetDatabase.LoadAssetAtPath<GameObject>(monsterData.modelPath);
if (fbxModel != null)
{
GameObject fbxInstance = GameObject.Instantiate(fbxModel);
fbxInstance.name = "Model";
fbxInstance.transform.SetParent(prefab.transform, false);
fbxInstance.transform.localPosition = Vector3.zero;
fbxInstance.transform.localRotation = Quaternion.identity;
fbxInstance.transform.localScale = Vector3.one;
Debug.Log($"[MonsterPrefabSetup] Applied FBX model: {monsterData.modelPath}");
Avatar modelAvatar = fbxModel.GetComponent<Animator>()?.avatar;
if (modelAvatar != null)
{
Animator prefabAnimator = prefab.GetComponent<Animator>();
if (prefabAnimator != null)
{
prefabAnimator.avatar = modelAvatar;
Debug.Log($"[MonsterPrefabSetup] Applied Avatar: {modelAvatar.name}");
}
}
}
else
{
Debug.LogWarning($"[MonsterPrefabSetup] Could not load FBX model: {monsterData.modelPath}");
}
}
else
{
var meshFilter = prefab.GetComponent<MeshFilter>();
if (meshFilter != null)
{
Mesh mesh = AssetDatabase.LoadAssetAtPath<Mesh>(monsterData.modelPath);
if (mesh != null)
{
meshFilter.sharedMesh = mesh;
Debug.Log($"[MonsterPrefabSetup] Applied mesh: {monsterData.modelPath}");
}
else
{
Debug.LogWarning($"[MonsterPrefabSetup] Could not load mesh: {monsterData.modelPath}");
}
}
}
}
if (!string.IsNullOrEmpty(monsterData.animationControllerPath))
{
Animator animator = prefab.GetComponent<Animator>();
if (animator != null)
{
RuntimeAnimatorController controller = AssetDatabase.LoadAssetAtPath<RuntimeAnimatorController>(monsterData.animationControllerPath);
if (controller != null)
{
animator.runtimeAnimatorController = controller;
Debug.Log($"[MonsterPrefabSetup] Applied Animator Controller: {monsterData.animationControllerPath}");
}
else
{
Debug.LogWarning($"[MonsterPrefabSetup] Could not load Animator Controller: {monsterData.animationControllerPath}");
}
}
}
}
private void RemoveOldModel(GameObject prefab)
{
Transform oldModel = prefab.transform.Find("Model");
if (oldModel != null)
{
GameObject.DestroyImmediate(oldModel.gameObject);
}
}
}
}