100 lines
3.8 KiB
C#
100 lines
3.8 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();
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(monsterData.meshPath))
|
|
{
|
|
RemoveOldModel(prefab);
|
|
|
|
if (monsterData.meshPath.ToLower().EndsWith(".fbx"))
|
|
{
|
|
GameObject fbxModel = AssetDatabase.LoadAssetAtPath<GameObject>(monsterData.meshPath);
|
|
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.meshPath}");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"[MonsterPrefabSetup] Could not load FBX model: {monsterData.meshPath}");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var meshFilter = prefab.GetComponent<MeshFilter>();
|
|
if (meshFilter != null)
|
|
{
|
|
Mesh mesh = AssetDatabase.LoadAssetAtPath<Mesh>(monsterData.meshPath);
|
|
if (mesh != null)
|
|
{
|
|
meshFilter.sharedMesh = mesh;
|
|
Debug.Log($"[MonsterPrefabSetup] Applied mesh: {monsterData.meshPath}");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"[MonsterPrefabSetup] Could not load mesh: {monsterData.meshPath}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(monsterData.animatorControllerPath))
|
|
{
|
|
Animator animator = prefab.GetComponent<Animator>();
|
|
if (animator != null)
|
|
{
|
|
RuntimeAnimatorController controller = AssetDatabase.LoadAssetAtPath<RuntimeAnimatorController>(monsterData.animatorControllerPath);
|
|
if (controller != null)
|
|
{
|
|
animator.runtimeAnimatorController = controller;
|
|
Debug.Log($"[MonsterPrefabSetup] Applied Animator Controller: {monsterData.animatorControllerPath}");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"[MonsterPrefabSetup] Could not load Animator Controller: {monsterData.animatorControllerPath}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void RemoveOldModel(GameObject prefab)
|
|
{
|
|
Transform oldModel = prefab.transform.Find("Model");
|
|
if (oldModel != null)
|
|
{
|
|
GameObject.DestroyImmediate(oldModel.gameObject);
|
|
}
|
|
}
|
|
}
|
|
}
|