public static void SetLoop(this AnimationClip clip, bool value) { SerializedObject serializedClip = new SerializedObject(clip); AnimationClipSettings clipSettings = new AnimationClipSettings(serializedClip.FindProperty("m_AnimationClipSettings")); clipSettings.loopTime = value; clipSettings.loopBlend = value; serializedClip.ApplyModifiedProperties(); }
/// <summary> /// Create an animation clip from passed in sprites /// </summary> /// <returns>The created clip.</returns> /// <param name="sprites">Sprites, in order.</param> /// <param name="clipName">Clip name.</param> public static AnimationClip CreateClip(Sprite[] sprites, string clipName) { // Output nothing if there is no clip name if (string.IsNullOrEmpty(clipName)) { return null; } // Could be inputs int sampleRate = 12; bool isLooping = false; // Create a new Clip AnimationClip clip = new AnimationClip(); // Apply the name and framerate clip.name = clipName; clip.frameRate = sampleRate; // Apply Looping Settings AnimationClipSettings clipSettings = new AnimationClipSettings(); clipSettings.loopTime = isLooping; AnimationUtility.SetAnimationClipSettings(clip, clipSettings); // Initialize the curve property for the animation clip EditorCurveBinding curveBinding = new EditorCurveBinding(); curveBinding.propertyName = "m_Sprite"; // Assumes user wants to apply the sprite property to the root element curveBinding.path = string.Empty; curveBinding.type = typeof(SpriteRenderer); // Build keyframes for the property using the supplied Sprites ObjectReferenceKeyframe[] keys = CreateKeysForSprites(sprites, sampleRate); // Build the clip if valid if (keys.Length > 0) { // Set the keyframes to the animation AnimationUtility.SetObjectReferenceCurve(clip, curveBinding, keys); } return clip; }
public void AssignToPreviewClip(AnimationClip clip) { AnimationClipSettings srcClipInfo = new AnimationClipSettings { startTime = this.firstFrame / clip.frameRate, stopTime = this.lastFrame / clip.frameRate, orientationOffsetY = this.orientationOffsetY, level = this.level, cycleOffset = this.cycleOffset, loopTime = this.loopTime, loopBlend = this.loopBlend, loopBlendOrientation = this.loopBlendOrientation, loopBlendPositionY = this.loopBlendPositionY, loopBlendPositionXZ = this.loopBlendPositionXZ, keepOriginalOrientation = this.keepOriginalOrientation, keepOriginalPositionY = this.keepOriginalPositionY, keepOriginalPositionXZ = this.keepOriginalPositionXZ, heightFromFeet = this.heightFromFeet, mirror = this.mirror }; AnimationUtility.SetAnimationClipSettingsNoDirty(clip, srcClipInfo); }
public void AssignToPreviewClip(AnimationClip clip) { AnimationClipSettings info = new AnimationClipSettings(); info.startTime = firstFrame / clip.frameRate; info.stopTime = lastFrame / clip.frameRate; info.orientationOffsetY = orientationOffsetY; info.level = level; info.cycleOffset = cycleOffset; info.loopTime = loopTime; info.loopBlend = loopBlend; info.loopBlendOrientation = loopBlendOrientation; info.loopBlendPositionY = loopBlendPositionY; info.loopBlendPositionXZ = loopBlendPositionXZ; info.keepOriginalOrientation = keepOriginalOrientation; info.keepOriginalPositionY = keepOriginalPositionY; info.keepOriginalPositionXZ = keepOriginalPositionXZ; info.heightFromFeet = heightFromFeet; info.mirror = mirror; info.hasAdditiveReferencePose = hasAdditiveReferencePose; info.additiveReferencePoseTime = additiveReferencePoseFrame / clip.frameRate; AnimationUtility.SetAnimationClipSettingsNoDirty(clip, info); }
extern internal static void SetAnimationClipSettingsNoDirty([NotNull] AnimationClip clip, AnimationClipSettings srcClipInfo);
extern public static void SetAnimationClipSettings([NotNull] AnimationClip clip, AnimationClipSettings srcClipInfo);
internal static extern void SetAnimationClipSettingsNoDirty(AnimationClip clip, AnimationClipSettings srcClipInfo);
public static extern void SetAnimationClipSettings(AnimationClip clip, AnimationClipSettings srcClipInfo);
static void SetAnimationSettings (AnimationClip clip, AnimationClipSettings settings) { #if UNITY_5 AnimationUtility.SetAnimationClipSettings(clip, settings); #else MethodInfo methodInfo = typeof(AnimationUtility).GetMethod("SetAnimationClipSettings", BindingFlags.Static | BindingFlags.NonPublic); methodInfo.Invoke(null, new object[] { clip, settings }); EditorUtility.SetDirty(clip); #endif }
/* Method Signature: [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] internal static extern void SetAnimationClipSettings(AnimationClip clip, AnimationClipSettings srcClipInfo); */ /// <summary> /// Uses reflection to call the internal (seriously, guys?!) SetAnimationClipSettings method /// Especially funny because the method doesn't even appear to be USED internally... /// </summary> public static void SetAnimationSettings(this AnimationClip animClip, AnimationClipSettings settings) { //Use reflection to get the internal method BindingFlags bindingFlags = BindingFlags.Static | BindingFlags.NonPublic; MethodInfo mInfo = typeof(AnimationUtility).GetMethod("SetAnimationClipSettings", bindingFlags); mInfo.Invoke(null, new object[] { animClip, settings }); }
/// <summary> /// Generates a clip from this SpriteAnim /// </summary> /// <param name="savePath">Save path.</param> /// <param name="filenamePrefix">Filename prefix.</param> public void GenerateClip(string savePath, string filenamePrefix) { // Output nothing if there is no clip name if (string.IsNullOrEmpty(this.clipName)) { return; } // Output nothing if no frames are defined if (this.animationKeyframes == null || this.animationKeyframes.Length == 0) { return; } // Get the clip to add our Sprite Animation into, or create a new one. AnimationClip builtClip; bool clipIsNew; if (this.savedClip == null) { builtClip = new AnimationClip(); clipIsNew = true; } else { builtClip = this.savedClip; clipIsNew = false; } builtClip.name = this.clipName; builtClip.frameRate = this.Samples; // Set the Looping status of the clip AnimationClipSettings clipSettings = new AnimationClipSettings(); clipSettings.loopTime = this.isLooping; AnimationUtility.SetAnimationClipSettings(builtClip, clipSettings); // Clear ALL existing sprite bindings in the clip EditorCurveBinding[] existingObjectBinding = AnimationUtility.GetObjectReferenceCurveBindings(builtClip); for (int i = 0; i < existingObjectBinding.Length; i++) { EditorCurveBinding currentBinding = existingObjectBinding[i]; if (currentBinding.type == typeof(SpriteRenderer)) { AnimationUtility.SetObjectReferenceCurve(builtClip, currentBinding, null); } } // Clear existing Scale since it will be replaced EditorCurveBinding[] existingValueBindings = AnimationUtility.GetCurveBindings(builtClip); for (int i = 0; i < existingValueBindings.Length; i++) { EditorCurveBinding currentBinding = existingValueBindings[i]; if (currentBinding.type == typeof(Transform) && currentBinding.propertyName == "m_LocalScale.x") { builtClip.SetCurve(currentBinding.path, typeof(Transform), "m_LocalScale", null); break; } } // Initialize the curve property EditorCurveBinding curveBinding = new EditorCurveBinding(); curveBinding.propertyName = "m_Sprite"; curveBinding.path = this.PathToSpriteRenderer; curveBinding.type = typeof(SpriteRenderer); // Build keyframes for the property Sprite[] sprites = AssetDatabaseUtility.LoadSpritesInTextureSorted(this.SourceTexture); ObjectReferenceKeyframe[] keys = this.CreateKeysForKeyframeRanges(sprites, this.animationKeyframes, this.Samples); // Build the clip if valid if (keys != null && keys.Length > 0) { // Set the keyframes to the animation AnimationUtility.SetObjectReferenceCurve(builtClip, curveBinding, keys); // Add scaling to mirror sprites // Need to also restore scale in case a clip was previously mirrored and then unflagged AnimationCurve normalCurve = AnimationCurve.Linear(0.0f, 1.0f, builtClip.length, 1.0f); AnimationCurve mirrorCurve = AnimationCurve.Linear(0.0f, -1.0f, builtClip.length, -1.0f); AnimationCurve xCurve = this.isMirroredX ? mirrorCurve : normalCurve; AnimationCurve yCurve = this.isMirroredY ? mirrorCurve : normalCurve; builtClip.SetCurve(this.PathToSpriteRenderer, typeof(Transform), "localScale.x", xCurve); builtClip.SetCurve(this.PathToSpriteRenderer, typeof(Transform), "localScale.y", yCurve); builtClip.SetCurve(this.PathToSpriteRenderer, typeof(Transform), "localScale.z", normalCurve); // Create or replace the file string filenameSansExtension = filenamePrefix + "_" + this.clipName; if (clipIsNew) { string filename = filenameSansExtension + ".anim"; string fullpath = savePath + filename; AssetDatabase.CreateAsset(builtClip, fullpath); } else { string pathToAsset = AssetDatabase.GetAssetPath(this.savedClip); // renaming file doesn't expect extension for some reason AssetDatabase.RenameAsset(pathToAsset, filenameSansExtension); } // Store reference to created clip to allow overwriting / renaming this.savedClip = builtClip; } else { if (keys == null) { Debug.LogWarning("Skipping clip due to no keys found: " + this.clipName); } else { Debug.LogWarning("Encountered invalid clip. Not enough keys. Skipping clip: " + this.clipName); } } }