119 lines
4.7 KiB
C#
119 lines
4.7 KiB
C#
using Northbound.Data;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace Northbound.Editor
|
|
{
|
|
public class CreepPrefabSetup : IPrefabSetup
|
|
{
|
|
public string GetTemplateName()
|
|
{
|
|
return "CreepTemplate";
|
|
}
|
|
|
|
public void SetupPrefab(GameObject prefab, ScriptableObject data)
|
|
{
|
|
if (!(data is CreepData creepData))
|
|
{
|
|
Debug.LogWarning($"[CreepPrefabSetup] Expected CreepData, got {data.GetType().Name}");
|
|
return;
|
|
}
|
|
|
|
var creepDataComponent = prefab.GetComponent<CreepDataComponent>();
|
|
if (creepDataComponent != null)
|
|
{
|
|
creepDataComponent.creepData = creepData;
|
|
creepDataComponent.ApplyCreepData();
|
|
}
|
|
|
|
var animationController = prefab.GetComponent<MonsterAnimationController>();
|
|
if (animationController == null)
|
|
{
|
|
animationController = prefab.AddComponent<MonsterAnimationController>();
|
|
Debug.Log($"[CreepPrefabSetup] Added MonsterAnimationController component");
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(creepData.modelPath))
|
|
{
|
|
RemoveOldModel(prefab);
|
|
|
|
if (creepData.modelPath.ToLower().EndsWith(".fbx"))
|
|
{
|
|
GameObject fbxModel = AssetDatabase.LoadAssetAtPath<GameObject>(creepData.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($"[CreepPrefabSetup] Applied FBX model: {creepData.modelPath}");
|
|
|
|
Avatar modelAvatar = fbxModel.GetComponent<Animator>()?.avatar;
|
|
|
|
if (modelAvatar != null)
|
|
{
|
|
Animator prefabAnimator = prefab.GetComponent<Animator>();
|
|
if (prefabAnimator != null)
|
|
{
|
|
prefabAnimator.avatar = modelAvatar;
|
|
Debug.Log($"[CreepPrefabSetup] Applied Avatar: {modelAvatar.name}");
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"[CreepPrefabSetup] Could not load FBX model: {creepData.modelPath}");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var meshFilter = prefab.GetComponent<MeshFilter>();
|
|
if (meshFilter != null)
|
|
{
|
|
Mesh mesh = AssetDatabase.LoadAssetAtPath<Mesh>(creepData.modelPath);
|
|
if (mesh != null)
|
|
{
|
|
meshFilter.sharedMesh = mesh;
|
|
Debug.Log($"[CreepPrefabSetup] Applied mesh: {creepData.modelPath}");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"[CreepPrefabSetup] Could not load mesh: {creepData.modelPath}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(creepData.animationControllerPath))
|
|
{
|
|
Animator animator = prefab.GetComponent<Animator>();
|
|
if (animator != null)
|
|
{
|
|
RuntimeAnimatorController controller = AssetDatabase.LoadAssetAtPath<RuntimeAnimatorController>(creepData.animationControllerPath);
|
|
if (controller != null)
|
|
{
|
|
animator.runtimeAnimatorController = controller;
|
|
Debug.Log($"[CreepPrefabSetup] Applied Animator Controller: {creepData.animationControllerPath}");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"[CreepPrefabSetup] Could not load Animator Controller: {creepData.animationControllerPath}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void RemoveOldModel(GameObject prefab)
|
|
{
|
|
Transform oldModel = prefab.transform.Find("Model");
|
|
if (oldModel != null)
|
|
{
|
|
GameObject.DestroyImmediate(oldModel.gameObject);
|
|
}
|
|
}
|
|
}
|
|
}
|