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