mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
Merge pull request #308 from Fenrisul/master
[Unity] Added Bone FlipX support to Skeleton Utility
This commit is contained in:
commit
9fa6d8fff6
@ -41,7 +41,7 @@ using Spine;
|
|||||||
|
|
||||||
[CustomEditor(typeof(SkeletonUtilityBone)), CanEditMultipleObjects]
|
[CustomEditor(typeof(SkeletonUtilityBone)), CanEditMultipleObjects]
|
||||||
public class SkeletonUtilityBoneInspector : Editor {
|
public class SkeletonUtilityBoneInspector : Editor {
|
||||||
SerializedProperty mode, boneName, zPosition, position, rotation, scale, overrideAlpha, parentReference;
|
SerializedProperty mode, boneName, zPosition, position, rotation, scale, overrideAlpha, parentReference, flip, flipX;
|
||||||
|
|
||||||
//multi selected flags
|
//multi selected flags
|
||||||
bool containsFollows, containsOverrides, multiObject;
|
bool containsFollows, containsOverrides, multiObject;
|
||||||
@ -60,6 +60,8 @@ public class SkeletonUtilityBoneInspector : Editor {
|
|||||||
scale = this.serializedObject.FindProperty("scale");
|
scale = this.serializedObject.FindProperty("scale");
|
||||||
overrideAlpha = this.serializedObject.FindProperty("overrideAlpha");
|
overrideAlpha = this.serializedObject.FindProperty("overrideAlpha");
|
||||||
parentReference = this.serializedObject.FindProperty("parentReference");
|
parentReference = this.serializedObject.FindProperty("parentReference");
|
||||||
|
flip = this.serializedObject.FindProperty("flip");
|
||||||
|
flipX = this.serializedObject.FindProperty("flipX");
|
||||||
|
|
||||||
EvaluateFlags();
|
EvaluateFlags();
|
||||||
|
|
||||||
@ -134,11 +136,24 @@ public class SkeletonUtilityBoneInspector : Editor {
|
|||||||
EditorGUILayout.PropertyField(position);
|
EditorGUILayout.PropertyField(position);
|
||||||
EditorGUILayout.PropertyField(rotation);
|
EditorGUILayout.PropertyField(rotation);
|
||||||
EditorGUILayout.PropertyField(scale);
|
EditorGUILayout.PropertyField(scale);
|
||||||
|
EditorGUILayout.PropertyField(flip);
|
||||||
|
|
||||||
EditorGUI.BeginDisabledGroup( containsFollows );
|
EditorGUI.BeginDisabledGroup( containsFollows );
|
||||||
{
|
{
|
||||||
EditorGUILayout.PropertyField(overrideAlpha);
|
EditorGUILayout.PropertyField(overrideAlpha);
|
||||||
EditorGUILayout.PropertyField(parentReference);
|
EditorGUILayout.PropertyField(parentReference);
|
||||||
|
|
||||||
|
EditorGUI.BeginDisabledGroup(multiObject || !flip.boolValue);
|
||||||
|
{
|
||||||
|
EditorGUI.BeginChangeCheck();
|
||||||
|
EditorGUILayout.PropertyField(flipX);
|
||||||
|
if (EditorGUI.EndChangeCheck())
|
||||||
|
{
|
||||||
|
FlipX(flipX.boolValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EditorGUI.EndDisabledGroup();
|
||||||
|
|
||||||
}
|
}
|
||||||
EditorGUI.EndDisabledGroup();
|
EditorGUI.EndDisabledGroup();
|
||||||
|
|
||||||
@ -173,6 +188,15 @@ public class SkeletonUtilityBoneInspector : Editor {
|
|||||||
serializedObject.ApplyModifiedProperties();
|
serializedObject.ApplyModifiedProperties();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FlipX(bool state)
|
||||||
|
{
|
||||||
|
utilityBone.FlipX(state);
|
||||||
|
if (Application.isPlaying == false)
|
||||||
|
{
|
||||||
|
skeletonUtility.skeletonAnimation.LateUpdate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void BoneSelectorContextMenu(string current, List<Bone> bones, string topValue, GenericMenu.MenuFunction2 callback){
|
void BoneSelectorContextMenu(string current, List<Bone> bones, string topValue, GenericMenu.MenuFunction2 callback){
|
||||||
GenericMenu menu = new GenericMenu();
|
GenericMenu menu = new GenericMenu();
|
||||||
|
|
||||||
|
|||||||
@ -61,6 +61,8 @@ public class SkeletonUtilityBone : MonoBehaviour {
|
|||||||
public bool position;
|
public bool position;
|
||||||
public bool rotation;
|
public bool rotation;
|
||||||
public bool scale;
|
public bool scale;
|
||||||
|
public bool flip;
|
||||||
|
public bool flipX;
|
||||||
|
|
||||||
[Range(0f,1f)]
|
[Range(0f,1f)]
|
||||||
public float overrideAlpha = 1;
|
public float overrideAlpha = 1;
|
||||||
@ -138,23 +140,41 @@ public class SkeletonUtilityBone : MonoBehaviour {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
float flipRotation = (skeleton.flipX ^ skeleton.flipY) ? -1f : 1f;
|
float skeletonFlipRotation = (skeleton.flipX ^ skeleton.flipY) ? -1f : 1f;
|
||||||
|
|
||||||
|
float flipCompensation = 0;
|
||||||
|
if (flip && (flipX || ( flipX != bone.flipX)) && bone.parent != null)
|
||||||
|
{
|
||||||
|
flipCompensation = bone.parent.WorldRotation * -2;
|
||||||
|
}
|
||||||
|
|
||||||
if(mode == Mode.Follow){
|
if(mode == Mode.Follow){
|
||||||
|
if (flip)
|
||||||
|
{
|
||||||
|
flipX = bone.flipX;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if(position){
|
if(position){
|
||||||
cachedTransform.localPosition = new Vector3(bone.x, bone.y, 0);
|
cachedTransform.localPosition = new Vector3(bone.x, bone.y, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(rotation){
|
if(rotation){
|
||||||
|
|
||||||
if(bone.Data.InheritRotation){
|
if(bone.Data.InheritRotation){
|
||||||
cachedTransform.localRotation = Quaternion.Euler(0,0,bone.rotationIK);
|
if (bone.FlipX)
|
||||||
|
{
|
||||||
|
cachedTransform.localRotation = Quaternion.Euler(0, 180, bone.rotationIK - flipCompensation);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
cachedTransform.localRotation = Quaternion.Euler(0,0,bone.rotationIK);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
Vector3 euler = skeletonTransform.rotation.eulerAngles;
|
Vector3 euler = skeletonTransform.rotation.eulerAngles;
|
||||||
cachedTransform.rotation = Quaternion.Euler(euler.x, euler.y, skeletonTransform.rotation.eulerAngles.z + (bone.worldRotation * flipRotation) );
|
cachedTransform.rotation = Quaternion.Euler(euler.x, euler.y, skeletonTransform.rotation.eulerAngles.z + (bone.worldRotation * skeletonFlipRotation) );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -167,8 +187,11 @@ public class SkeletonUtilityBone : MonoBehaviour {
|
|||||||
|
|
||||||
}
|
}
|
||||||
else if(mode == Mode.Override){
|
else if(mode == Mode.Override){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if(transformLerpComplete)
|
if(transformLerpComplete)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if(parentReference == null){
|
if(parentReference == null){
|
||||||
if(position){
|
if(position){
|
||||||
@ -177,8 +200,23 @@ public class SkeletonUtilityBone : MonoBehaviour {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(rotation){
|
if(rotation){
|
||||||
bone.rotation = Mathf.LerpAngle(bone.Rotation, cachedTransform.localRotation.eulerAngles.z, overrideAlpha);
|
float angle = Mathf.LerpAngle(bone.Rotation, cachedTransform.localRotation.eulerAngles.z, overrideAlpha) + flipCompensation;
|
||||||
}
|
|
||||||
|
if (flip) {
|
||||||
|
if ((!flipX && bone.flipX))
|
||||||
|
{
|
||||||
|
angle -= flipCompensation;
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO fix this...
|
||||||
|
if (angle >= 360)
|
||||||
|
angle -= 360;
|
||||||
|
else if (angle <= -360)
|
||||||
|
angle += 360;
|
||||||
|
}
|
||||||
|
|
||||||
|
bone.Rotation = angle;
|
||||||
|
}
|
||||||
|
|
||||||
if(scale){
|
if(scale){
|
||||||
bone.scaleX = Mathf.Lerp(bone.scaleX, cachedTransform.localScale.x, overrideAlpha);
|
bone.scaleX = Mathf.Lerp(bone.scaleX, cachedTransform.localScale.x, overrideAlpha);
|
||||||
@ -186,8 +224,17 @@ public class SkeletonUtilityBone : MonoBehaviour {
|
|||||||
|
|
||||||
nonUniformScaleWarning = (bone.scaleX != bone.scaleY);
|
nonUniformScaleWarning = (bone.scaleX != bone.scaleY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (flip)
|
||||||
|
{
|
||||||
|
bone.flipX = flipX;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
|
||||||
|
if (transformLerpComplete)
|
||||||
|
return;
|
||||||
|
|
||||||
if(position){
|
if(position){
|
||||||
Vector3 pos = parentReference.InverseTransformPoint(cachedTransform.position);
|
Vector3 pos = parentReference.InverseTransformPoint(cachedTransform.position);
|
||||||
bone.x = Mathf.Lerp(bone.x, pos.x, overrideAlpha);
|
bone.x = Mathf.Lerp(bone.x, pos.x, overrideAlpha);
|
||||||
@ -195,22 +242,69 @@ public class SkeletonUtilityBone : MonoBehaviour {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(rotation){
|
if(rotation){
|
||||||
bone.rotation = Mathf.LerpAngle(bone.Rotation, Quaternion.LookRotation( Vector3.forward, parentReference.InverseTransformDirection( cachedTransform.up ) ).eulerAngles.z, overrideAlpha);
|
float angle = Mathf.LerpAngle(bone.Rotation, Quaternion.LookRotation(flipX ? Vector3.forward * -1 : Vector3.forward, parentReference.InverseTransformDirection(cachedTransform.up)).eulerAngles.z, overrideAlpha) + flipCompensation;
|
||||||
|
|
||||||
|
if (flip)
|
||||||
|
{
|
||||||
|
if ((!flipX && bone.flipX))
|
||||||
|
{
|
||||||
|
angle -= flipCompensation;
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO fix this...
|
||||||
|
if (angle >= 360)
|
||||||
|
angle -= 360;
|
||||||
|
else if (angle <= -360)
|
||||||
|
angle += 360;
|
||||||
|
}
|
||||||
|
|
||||||
|
bone.Rotation = angle;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Something about this
|
//TODO: Something about this
|
||||||
if(scale){
|
if(scale){
|
||||||
bone.scaleX = Mathf.Lerp(bone.scaleX, cachedTransform.localScale.x, overrideAlpha);
|
bone.scaleX = Mathf.Lerp(bone.scaleX, cachedTransform.localScale.x, overrideAlpha);
|
||||||
bone.scaleY = Mathf.Lerp(bone.scaleY, cachedTransform.localScale.y, overrideAlpha);
|
bone.scaleY = Mathf.Lerp(bone.scaleY, cachedTransform.localScale.y, overrideAlpha);
|
||||||
|
|
||||||
nonUniformScaleWarning = (bone.scaleX != bone.scaleY);
|
nonUniformScaleWarning = (bone.scaleX != bone.scaleY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (flip)
|
||||||
|
{
|
||||||
|
bone.flipX = flipX;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
transformLerpComplete = true;
|
transformLerpComplete = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void FlipX(bool state)
|
||||||
|
{
|
||||||
|
if (state != flipX)
|
||||||
|
{
|
||||||
|
flipX = state;
|
||||||
|
if (flipX && Mathf.Abs(transform.localRotation.eulerAngles.y) > 90)
|
||||||
|
{
|
||||||
|
skeletonUtility.skeletonAnimation.LateUpdate();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if (!flipX && Mathf.Abs(transform.localRotation.eulerAngles.y) < 90)
|
||||||
|
{
|
||||||
|
skeletonUtility.skeletonAnimation.LateUpdate();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bone.FlipX = state;
|
||||||
|
transform.RotateAround(transform.position, skeletonUtility.transform.up, 180);
|
||||||
|
Vector3 euler = transform.localRotation.eulerAngles;
|
||||||
|
euler.x = 0;
|
||||||
|
euler.y = bone.FlipX ? 180 : 0;
|
||||||
|
transform.localRotation = Quaternion.Euler(euler);
|
||||||
|
}
|
||||||
|
|
||||||
void OnDrawGizmos(){
|
void OnDrawGizmos(){
|
||||||
if(NonUniformScaleWarning){
|
if(NonUniformScaleWarning){
|
||||||
Gizmos.DrawIcon(transform.position + new Vector3(0,0.128f,0), "icon-warning");
|
Gizmos.DrawIcon(transform.position + new Vector3(0,0.128f,0), "icon-warning");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user