Skip to content

Commit

Permalink
Merge branch 'release/v0.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
nicloay committed Jul 15, 2015
2 parents 17234e0 + c821fc7 commit e6e9719
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 50 deletions.
26 changes: 4 additions & 22 deletions Assets/UnitySpineImporter/Scripts/Editor/SpineImporterWizard.cs
Expand Up @@ -6,15 +6,11 @@


namespace UnitySpineImporter{
public enum AnimationImportType{
MECANIM,
LEGACY
}

public class SpineImporterWizard :ScriptableWizard {
public int pixelsPerUnit = 100;
public bool buildAvatarMask = true;
public AnimationImportType animationImportType = AnimationImportType.MECANIM;
public bool useLegacyAnimation = false; //false - means mecanim
public bool updateResources = true;
public float zStep = 0.01f;
[HideInInspector]
Expand Down Expand Up @@ -42,7 +38,7 @@ public class SpineImporterWizard :ScriptableWizard {
else
errorString ="";
isValid = errorString.Equals("");
if (animationImportType == AnimationImportType.LEGACY && buildAvatarMask)
if (useLegacyAnimation && buildAvatarMask)
helpString += "\n buildAvatarMask will be ignored";
}

Expand All @@ -67,16 +63,15 @@ public class SpineImporterWizard :ScriptableWizard {
rootGO.name = name;
SpineUtil.addAllAttahcmentsSlots(spineData, spriteByName, slotByName, pixelsPerUnit, out skins, out attachmentGOByNameBySlot);
SkinController sk = SpineUtil.addSkinController(rootGO, spineData, skins, slotByName);
if (animationImportType == AnimationImportType.MECANIM){
if (!useLegacyAnimation){
Animator animator = SpineUtil.addAnimator(rootGO);
if (buildAvatarMask)
SpineUtil.builAvatarMask(rootGO,spineData, animator, directory, name);
}

ModelImporterAnimationType modelImporterAnimationType = getModelImporterAnimationType();
if (spineData.animations !=null && spineData.animations.Count > 0)
SpineUtil.addAnimation(rootGO, directory, spineData, boneGOByName, slotByName, attachmentGOByNameBySlot, skins,
pixelsPerUnit, zStep, modelImporterAnimationType, updateResources );
pixelsPerUnit, zStep, useLegacyAnimation, updateResources );
sk.showDefaulSlots();
SpineUtil.buildPrefab(rootGO, directory, name);
GameObject.DestroyImmediate(rootGO);
Expand All @@ -90,19 +85,6 @@ public class SpineImporterWizard :ScriptableWizard {
}
}
}

ModelImporterAnimationType getModelImporterAnimationType(){
ModelImporterAnimationType result = ModelImporterAnimationType.Generic;
switch(animationImportType){
case AnimationImportType.LEGACY:
result = ModelImporterAnimationType.Legacy;
break;
case AnimationImportType.MECANIM:
result = ModelImporterAnimationType.Generic;
break;
}
return result;
}

static string getAtlasFilePath(string path){
string dir = Path.GetDirectoryName(path);
Expand Down
20 changes: 20 additions & 0 deletions Assets/UnitySpineImporter/Scripts/Editor/UnityInternalMethods.cs
@@ -0,0 +1,20 @@

using UnityEditor.Animations;
using UnityEngine;
using System.Reflection;
using UnityEditor;

public class UnityInternalMethods {
public static AnimatorController GetEffectiveAnimatorController(Animator animator){
return (AnimatorController) (typeof(AnimatorController).GetMethod("GetEffectiveAnimatorController"
, BindingFlags.Static | BindingFlags.NonPublic).Invoke(null, new object[]{ animator}));
}

public static void GetTextureSize(TextureImporter textureImporter, out int width, out int height){
object[] args = new object[2] { 0, 0 };
MethodInfo mi = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Invoke(textureImporter, args);
width = (int)args[0];
height = (int)args[1];
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 36 additions & 28 deletions Assets/UnitySpineImporter/Scripts/Editor/Util/SpineUtil.cs
Expand Up @@ -7,7 +7,7 @@
using UnityEditorInternal;
using CurveExtended;
using LitJson;
using System.Reflection;
using UnityEditor.Animations;

namespace UnitySpineImporter{
public class AtlasImageNotFoundException: System.Exception{
Expand Down Expand Up @@ -85,12 +85,8 @@ public class SpineUtil {
static void fixTextureSize(string imagePath){
TextureImporter importer = TextureImporter.GetAtPath(imagePath) as TextureImporter;
if (importer != null) {
object[] args = new object[2] { 0, 0 };
MethodInfo mi = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Invoke(importer, args);

int width = (int)args[0];
int height = (int)args[1];
int width, height;
UnityInternalMethods.GetTextureSize(importer, out width, out height);

int max = Mathf.Max(width,height);
if (max > 4096){
Expand Down Expand Up @@ -393,7 +389,7 @@ public class SpineUtil {
List<Skin> skinList,
int pixelsPerUnit,
float zStep,
ModelImporterAnimationType modelImporterAnimationType,
bool useLegacyAnimation,
bool updateResources)
{
float ratio = 1.0f / (float)pixelsPerUnit;
Expand All @@ -413,8 +409,7 @@ public class SpineUtil {
updateCurve = true;
}
}

AnimationUtility.SetAnimationType(animationClip, modelImporterAnimationType);
animationClip.legacy = useLegacyAnimation;
if (spineAnimation.bones!=null)
addBoneAnimationToClip(animationClip,spineAnimation.bones, spineData, boneGOByName, ratio);
if (spineAnimation.slots!=null)
Expand All @@ -434,10 +429,11 @@ public class SpineUtil {
AssetDatabase.CreateAsset(animationClip, assetPath);
AssetDatabase.SaveAssets();

if (modelImporterAnimationType == ModelImporterAnimationType.Generic)
AddClipToAnimatorComponent(rootGO,animationClip);
else
if (useLegacyAnimation){
AddClipToLegacyAnimationComponent(rootGO, animationClip);
} else {
AddClipToAnimatorComponent(rootGO,animationClip);
}
}

}
Expand Down Expand Up @@ -968,6 +964,7 @@ public static bool NearlyEqual(float a, float b, float epsilon)
AnimationCurve localRotationW = new AnimationCurve();

JsonData[] curveData = new JsonData[boneAnimation.rotate.Count];
Quaternion baseRotation = Quaternion.identity;
for (int i = 0; i < boneAnimation.rotate.Count; i++) {
float origAngle = (float)boneAnimation.rotate[i].angle;
if (origAngle > 0)
Expand All @@ -989,17 +986,10 @@ public static bool NearlyEqual(float a, float b, float epsilon)

}

fixAngles (localRotationX , curveData);
setTangents(localRotationX , curveData);

fixAngles (localRotationY , curveData);
setTangents(localRotationY , curveData);

fixAngles (localRotationZ , curveData);
setTangents(localRotationZ , curveData);

fixAngles (localRotationW , curveData);
setTangents(localRotationW , curveData);
fixAngleCurve (localRotationX , curveData, baseRotation.x);
fixAngleCurve (localRotationY , curveData, baseRotation.y);
fixAngleCurve (localRotationZ , curveData, baseRotation.z);
fixAngleCurve (localRotationW , curveData, baseRotation.w);

AnimationUtility.SetEditorCurve(clip,EditorCurveBinding.FloatCurve(bonePath,typeof(Transform),"m_LocalRotation.x"), localRotationX);
AnimationUtility.SetEditorCurve(clip,EditorCurveBinding.FloatCurve(bonePath,typeof(Transform),"m_LocalRotation.y"), localRotationY);
Expand Down Expand Up @@ -1035,6 +1025,23 @@ public static bool NearlyEqual(float a, float b, float epsilon)
}



static void fixAngleCurve(AnimationCurve animationCurve, JsonData[] curveData, float defSingleStepValue){
fixSingleStep(animationCurve, defSingleStepValue);
fixAngles (animationCurve, curveData);
setTangents (animationCurve, curveData);
}

static void fixSingleStep (AnimationCurve animationCurve, float defSingleStepValue)
{
if (animationCurve.keys.Length == 1 && animationCurve.keys[0].time != 0.0f){
Keyframe key = animationCurve.keys[0];
key.time = 0.0f;
key.value = defSingleStepValue;
animationCurve.AddKey(key);
}
}

static void fixAngles(AnimationCurve curve, JsonData[] curveData){
if (curve.keys.Length <3)
return;
Expand Down Expand Up @@ -1065,21 +1072,22 @@ public static AnimationClip AddClipToAnimatorComponent(GameObject animatedObject
Animator animator = animatedObject.GetComponent<Animator>();
if ( animator == null)
animator = animatedObject.AddComponent<Animator>();
AnimatorController animatorController = AnimatorController.GetEffectiveAnimatorController(animator);

UnityEditor.Animations.AnimatorController animatorController = UnityInternalMethods.GetEffectiveAnimatorController(animator);
if (animatorController == null)
{
string path = Path.GetDirectoryName( AssetDatabase.GetAssetPath(newClip)) +"/"+animatedObject.name+".controller";

AnimatorController controllerForClip = AnimatorController.CreateAnimatorControllerAtPathWithClip(path, newClip);
AnimatorController.SetAnimatorController(animator, controllerForClip);
UnityEditor.Animations.AnimatorController controllerForClip = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPathWithClip(path, newClip);
UnityEditor.Animations.AnimatorController.SetAnimatorController(animator, controllerForClip);
if (controllerForClip != null)
return newClip;
else
return null;
}
else
{
AnimatorController.AddAnimationClipToController(animatorController, newClip);
animatorController.AddMotion((Motion)newClip);
return newClip;
}
}
Expand Down
Binary file modified ProjectSettings/GraphicsSettings.asset
Binary file not shown.
Binary file added ProjectSettings/NavMeshAreas.asset
Binary file not shown.
Binary file modified ProjectSettings/ProjectSettings.asset
Binary file not shown.
2 changes: 2 additions & 0 deletions ProjectSettings/ProjectVersion.txt
@@ -0,0 +1,2 @@
m_EditorVersion: 5.1.1p3
m_StandardAssetsVersion: 0

0 comments on commit e6e9719

Please sign in to comment.