/// <summary> /// Copies shared data in from Preferences, used to upgrade versions of AnimationImporter /// </summary> public void CopyFromPreferences() { if (HasKeyInPreferences(PREFS_PREFIX + "spritePixelsPerUnit")) { _spritePixelsPerUnit = GetFloatFromPreferences(PREFS_PREFIX + "spritePixelsPerUnit"); } if (HasKeyInPreferences(PREFS_PREFIX + "spriteTargetObjectType")) { _targetObjectType = (AnimationTargetObjectType)GetIntFromPreferences(PREFS_PREFIX + "spriteTargetObjectType"); } if (HasKeyInPreferences(PREFS_PREFIX + "spriteAlignment")) { _spriteAlignment = (SpriteAlignment)GetIntFromPreferences(PREFS_PREFIX + "spriteAlignment"); } if (HasKeyInPreferences(PREFS_PREFIX + "spriteAlignmentCustomX")) { _spriteAlignmentCustomX = GetFloatFromPreferences(PREFS_PREFIX + "spriteAlignmentCustomX"); } if (HasKeyInPreferences(PREFS_PREFIX + "spriteAlignmentCustomY")) { _spriteAlignmentCustomY = GetFloatFromPreferences(PREFS_PREFIX + "spriteAlignmentCustomY"); } if (HasKeyInPreferences(PREFS_PREFIX + "saveSpritesToSubfolder")) { _saveSpritesToSubfolder = GetBoolFromPreferences(PREFS_PREFIX + "saveSpritesToSubfolder"); } if (HasKeyInPreferences(PREFS_PREFIX + "saveAnimationsToSubfolder")) { _saveAnimationsToSubfolder = GetBoolFromPreferences(PREFS_PREFIX + "saveAnimationsToSubfolder"); } if (HasKeyInPreferences(PREFS_PREFIX + "automaticImporting")) { _automaticImporting = GetBoolFromPreferences(PREFS_PREFIX + "automaticImporting"); } // Find all nonLoopingClip Prefences, load them into the sharedData. int numOldClips = 0; string loopCountKey = PREFS_PREFIX + "nonLoopCount"; if (HasKeyInPreferences(loopCountKey)) { numOldClips = GetIntFromPreferences(loopCountKey); } for (int i = 0; i < numOldClips; ++i) { string clipKey = PREFS_PREFIX + "nonLoopCount" + i.ToString(); // If the clip hasn't already been moved to the shared data, do it now. if (HasKeyInPreferences(clipKey)) { var stringAtKey = GetStringFromPreferences(clipKey); if (!_animationNamesThatDoNotLoop.Contains(stringAtKey)) { _animationNamesThatDoNotLoop.Add(stringAtKey); } } } }
public static void GetComponentPathsFromExistingClip(AnimationClip clip, AnimationTargetObjectType targetType, out string spriteRendererComponentPath, out string imageComponentPath) { var curveBindings = AnimationUtility.GetObjectReferenceCurveBindings(clip); spriteRendererComponentPath = string.Empty; imageComponentPath = string.Empty; for (int i = 0; i < curveBindings.Length; i++) { if (targetType != AnimationTargetObjectType.Image && curveBindings[i].type == typeof(SpriteRenderer)) { spriteRendererComponentPath = curveBindings[i].path; } else if (targetType != AnimationTargetObjectType.SpriteRenderer && curveBindings[i].type == typeof(UnityEngine.UI.Image)) { imageComponentPath = curveBindings[i].path; } } return; }
public void CreateAnimation(ImportedAnimation anim, string basePath, string masterName, AnimationTargetObjectType targetType) { AnimationClip clip; // attempt to ignore characters in master name after _ character string newName = masterName.Split('_')[0]; ///NOTE - DDobyns experiment to fix animation naming issues ///string fileName = basePath + "/" + masterName + "_" + anim.name + ".anim"; // original behavior is this line //string fileName = basePath + "/" + newName + "_" + anim.name + ".anim"; // DDobyns original fix is this line string fileName = basePath + "/" + anim.name + ".anim"; // DDobyns Feb 26 2020 fix is this line // this results in naming animations with the Action and Angle only. e.g., Character_run45 will save a clip named run45 bool isLooping = anim.isLooping; // check if animation file already exists clip = AssetDatabase.LoadAssetAtPath <AnimationClip>(fileName); if (clip != null) { // get previous animation settings targetType = PreviousImportSettings.GetAnimationTargetFromExistingClip(clip); } else { clip = new AnimationClip(); AssetDatabase.CreateAsset(clip, fileName); } // change loop settings if (isLooping) { clip.wrapMode = WrapMode.Loop; clip.SetLoop(true); } else { clip.wrapMode = WrapMode.Clamp; clip.SetLoop(false); } ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[anim.Count + 1]; // one more than sprites because we repeat the last sprite for (int i = 0; i < anim.Count; i++) { ObjectReferenceKeyframe keyFrame = new ObjectReferenceKeyframe { time = anim.GetKeyFrameTime(i) }; Sprite sprite = anim.frames[i].sprite; keyFrame.value = sprite; keyFrames[i] = keyFrame; } // repeating the last frame at a point "just before the end" so the animation gets its correct length ObjectReferenceKeyframe lastKeyFrame = new ObjectReferenceKeyframe { time = anim.GetLastKeyFrameTime(clip.frameRate) }; Sprite lastSprite = anim.frames[anim.Count - 1].sprite; lastKeyFrame.value = lastSprite; keyFrames[anim.Count] = lastKeyFrame; // save curve into clip, either for SpriteRenderer, Image, or both if (targetType == AnimationTargetObjectType.SpriteRenderer) { AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.spriteRendererCurveBinding, keyFrames); AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.imageCurveBinding, null); } else if (targetType == AnimationTargetObjectType.Image) { AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.spriteRendererCurveBinding, null); AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.imageCurveBinding, keyFrames); } else if (targetType == AnimationTargetObjectType.SpriteRendererAndImage) { AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.spriteRendererCurveBinding, keyFrames); AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.imageCurveBinding, keyFrames); } EditorUtility.SetDirty(clip); anim.animationClip = clip; }
public void CreateAnimation(ImportedAnimation anim, string basePath, string masterName, AnimationTargetObjectType targetType) { AnimationClip clip; string fileName = basePath + "/" + masterName + "_" + anim.name + ".anim"; bool isLooping = anim.isLooping; // check if animation file already exists clip = AssetDatabase.LoadAssetAtPath <AnimationClip>(fileName); if (clip != null) { // get previous animation settings targetType = PreviousImportSettings.GetAnimationTargetFromExistingClip(clip); isLooping = clip.isLooping; } else { clip = new AnimationClip(); AssetDatabase.CreateAsset(clip, fileName); } // change loop settings if (isLooping) { clip.wrapMode = WrapMode.Loop; clip.SetLoop(true); } else { clip.wrapMode = WrapMode.Clamp; clip.SetLoop(false); } ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[anim.Count + 1]; // one more than sprites because we repeat the last sprite for (int i = 0; i < anim.Count; i++) { ObjectReferenceKeyframe keyFrame = new ObjectReferenceKeyframe { time = anim.GetKeyFrameTime(i) }; Sprite sprite = anim.frames[i].sprite; keyFrame.value = sprite; keyFrames[i] = keyFrame; } // repeating the last frame at a point "just before the end" so the animation gets its correct length ObjectReferenceKeyframe lastKeyFrame = new ObjectReferenceKeyframe { time = anim.GetLastKeyFrameTime(clip.frameRate) }; Sprite lastSprite = anim.frames[anim.Count - 1].sprite; lastKeyFrame.value = lastSprite; keyFrames[anim.Count] = lastKeyFrame; // save curve into clip, either for SpriteRenderer, Image, or both if (targetType == AnimationTargetObjectType.SpriteRenderer) { AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.spriteRendererCurveBinding, keyFrames); AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.imageCurveBinding, null); } else if (targetType == AnimationTargetObjectType.Image) { AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.spriteRendererCurveBinding, null); AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.imageCurveBinding, keyFrames); } else if (targetType == AnimationTargetObjectType.SpriteRendererAndImage) { AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.spriteRendererCurveBinding, keyFrames); AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.imageCurveBinding, keyFrames); } EditorUtility.SetDirty(clip); anim.animationClip = clip; }
public void CreateAnimation(ImportedAnimation anim, string basePath, string masterName, AnimationTargetObjectType targetType) { AnimationClip clip; string fileName = basePath + "/" + masterName + "_" + anim.name + ".anim"; bool isLooping = anim.isLooping; // check if animation file already exists clip = AssetDatabase.LoadAssetAtPath <AnimationClip>(fileName); if (clip != null) { // get previous animation settings targetType = PreviousImportSettings.GetAnimationTargetFromExistingClip(clip); } else { clip = new AnimationClip(); AssetDatabase.CreateAsset(clip, fileName); } // change loop settings if (isLooping) { clip.wrapMode = WrapMode.Loop; clip.SetLoop(true); } else { clip.wrapMode = WrapMode.Clamp; clip.SetLoop(false); } // convert keyframes ImportedAnimationFrame[] srcKeyframes = anim.ListFramesAccountingForPlaybackDirection().ToArray(); ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[srcKeyframes.Length + 1]; float timeOffset = 0f; for (int i = 0; i < srcKeyframes.Length; i++) { // first sprite will be set at the beginning (t=0) of the animation keyFrames[i] = new ObjectReferenceKeyframe { time = timeOffset, value = srcKeyframes[i].sprite }; // add duration of frame in seconds timeOffset += srcKeyframes[i].duration / 1000f; } // repeating the last frame at a point "just before the end" so the animation gets its correct length keyFrames[srcKeyframes.Length] = new ObjectReferenceKeyframe { time = timeOffset - (1f / clip.frameRate), // substract the duration of one frame value = srcKeyframes.Last().sprite }; // save curve into clip, either for SpriteRenderer, Image, or both if (targetType == AnimationTargetObjectType.SpriteRenderer) { AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.spriteRendererCurveBinding, keyFrames); AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.imageCurveBinding, null); } else if (targetType == AnimationTargetObjectType.Image) { AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.spriteRendererCurveBinding, null); AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.imageCurveBinding, keyFrames); } else if (targetType == AnimationTargetObjectType.SpriteRendererAndImage) { AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.spriteRendererCurveBinding, keyFrames); AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.imageCurveBinding, keyFrames); } EditorUtility.SetDirty(clip); anim.animationClip = clip; }