2026-04-13 17:42:21 +02:00

58 lines
1.9 KiB
C#

using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
namespace CreatureControl {
[CustomEditor(typeof(Creature), true)]
public class Creature_Editor : Editor {
/// <summary>
/// The creature managed by this editor
/// </summary>
protected Creature creature;
#region Start
/// <summary>
/// Enable the creature editor
/// </summary>
public virtual void OnEnable() {
this.creature = target as Creature;
if (creature.targetRig != null)
creature.animator = creature.targetRig.GetComponent<Animator>();
// Keep track if anything changed while enabling the creature editor
bool somethingChanged = false;
// if (IsPrefab(this.creature) == false) {
// // Only do this when it is not a prefab
// somethingChanged |= this.creature.CheckTargetRig();
// somethingChanged |= this.creature.CheckModel();
// }
this.creature.targetRig.MatchTo(this.creature, ref somethingChanged);
// As the above functions do not use the serialized object
// We need to manually persist the changes.
if (somethingChanged) {
Debug.Log("something has changed");
EditorUtility.SetDirty(this.creature);
AssetDatabase.SaveAssets();
}
}
/// <summary>
/// Check if the given creature is a prefab
/// </summary>
/// <param name="creature">The creature to check</param>
/// <returns>True when it is a prefab</returns>
public static bool IsPrefab(Creature creature) {
PrefabStage prefabStage = PrefabStageUtility.GetPrefabStage(creature.gameObject);
return prefabStage != null;
}
#endregion Start
}
}