73 lines
2.5 KiB
C#
73 lines
2.5 KiB
C#
using UnityEditor;
|
|
using UnityEditor.SceneManagement;
|
|
|
|
using UnityEngine;
|
|
|
|
namespace Passer.CreatureControl {
|
|
|
|
[CustomEditor(typeof(Creature), true)]
|
|
public class Creature_Editor : Editor {
|
|
|
|
protected Creature creature;
|
|
|
|
public virtual void OnEnable() {
|
|
creature = target as Creature;
|
|
if (creature == null)
|
|
return;
|
|
|
|
creature.targetToRootTranslation = creature.transform.position - creature.targetRig.transform.position;
|
|
creature.targetToRootRotation = Quaternion.Inverse(creature.targetRig.transform.rotation) * creature.transform.rotation;
|
|
|
|
serializedObject.Update();
|
|
|
|
if (!IsPrefab(creature))
|
|
InitTargets(creature);
|
|
|
|
serializedObject.ApplyModifiedProperties();
|
|
}
|
|
|
|
public static bool IsPrefab(Creature creature) {
|
|
PrefabStage prefabStage = PrefabStageUtility.GetPrefabStage(creature.gameObject);
|
|
return prefabStage != null;
|
|
}
|
|
|
|
#region Targets
|
|
|
|
protected virtual void InitTargets(Creature creature) {
|
|
if (creature.CheckTargetRig("TargetRig")) {
|
|
EditorUtility.SetDirty(creature);
|
|
AssetDatabase.SaveAssets();
|
|
}
|
|
}
|
|
|
|
#endregion Targets
|
|
|
|
private bool initialize = true;
|
|
// public Vector3 targetToRootTranslation;
|
|
// public Quaternion targetToRootRotation;
|
|
|
|
public virtual void OnSceneGUI() {
|
|
if (Application.isPlaying)
|
|
return;
|
|
|
|
if (creature.enabled == false || creature.targetRig == null)
|
|
return;
|
|
|
|
// if (this.initialize) {
|
|
// creature.targetToRootTranslation = creature.transform.position - creature.targetRig.transform.position;
|
|
// creature.targetToRootRotation = Quaternion.Inverse(creature.targetRig.transform.rotation) * creature.transform.rotation;
|
|
// this.initialize = false;
|
|
// }
|
|
|
|
creature.targetRig.PoseLimbs();
|
|
creature.UpdateBones();
|
|
}
|
|
|
|
// protected virtual void UpdateBones() {
|
|
// Vector3 newPosition = creature.targetRig.transform.position + targetToRootTranslation;
|
|
// Quaternion newOrientation = creature.targetRig.transform.rotation * targetToRootRotation;
|
|
// creature.transform.SetPositionAndRotation(newPosition, newOrientation);
|
|
// }
|
|
|
|
}
|
|
} |