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(); if (monsterDataComponent != null) { monsterDataComponent.monsterData = monsterData; monsterDataComponent.ApplyMonsterData(); } var animationController = prefab.GetComponent(); if (animationController == null) { animationController = prefab.AddComponent(); Debug.Log($"[MonsterPrefabSetup] Added MonsterAnimationController component"); } if (!string.IsNullOrEmpty(monsterData.modelPath)) { RemoveOldModel(prefab); if (monsterData.modelPath.ToLower().EndsWith(".fbx")) { GameObject fbxModel = AssetDatabase.LoadAssetAtPath(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()?.avatar; if (modelAvatar != null) { Animator prefabAnimator = prefab.GetComponent(); 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(); if (meshFilter != null) { Mesh mesh = AssetDatabase.LoadAssetAtPath(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(); if (animator != null) { RuntimeAnimatorController controller = AssetDatabase.LoadAssetAtPath(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); } } } }