Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Accessing Loop Time through C#

Discussion in 'Scripting' started by Oddeye01, Dec 20, 2013.

  1. Oddeye01

    Oddeye01

    Joined:
    Dec 10, 2013
    Posts:
    5
    I'm attempting to access the loop time boolean of an animation clip. By default it is set to false if you create an animation clip through code. Its simple to change in the inspector, but I'm making a large amount of clips, and prefer to set this through code. Does anyone have any idea's how to change it in c#? Looping could be done solely through the wrapmode before, but I cannot figure out this loop time. :sad:

    Heres an image of how it looks in the inspector (at the top of the image)

    $loop time.png
     
    Last edited: Dec 20, 2013
  2. Sildaekar

    Sildaekar

    Joined:
    Jul 8, 2013
    Posts:
    95
    Depending on what you are using this:

    Code (csharp):
    1.  
    2. Animation a = new Animation();
    3. a.wrapMode= WrapMode.Loop;
    4.  
    Or this
    Code (csharp):
    1.  
    2. AnimationClip a = new AnimationClip();
    3.         a.wrapMode = WrapMode.Loop;
    Will set the animation to loop.
     
    Last edited: Dec 20, 2013
  3. Oddeye01

    Oddeye01

    Joined:
    Dec 10, 2013
    Posts:
    5
    If this interests anyone, I've been able to access the loop time using the AnimationUtility and getting the AnimationClipSettings, but when I change the boolean value, it does not actually change the loop time boolean for the clip. Heres the code:

    Code (csharp):
    1. AnimationClip t = AssetDatabase.LoadAssetAtPath("Assets/Animations/testing/MyAnim.anim", typeof(AnimationClip)) as AnimationClip;
    2.        
    3.         AnimationClipSettings tSettings = AnimationUtility.GetAnimationClipSettings(t);
    4.         Debug.Log (tSettings.loopTime);
    5.         tSettings.loopTime = true;
    6.         Debug.Log (tSettings.loopTime);
    The first log will return false, while the second will return true. But the clip does not actually change, and the inspector's checkbox still shows false. Is GetAnimationClipSettings() just returning a copy of the settings? I should note, there is no info on this in the documentation, so this would purely be from experience.

    Furthermore, the SetAnimationClipSettings() is private, so I'm unable to access it.
     
    Last edited: Dec 20, 2013
  4. Oddeye01

    Oddeye01

    Joined:
    Dec 10, 2013
    Posts:
    5
    Already tried that :(.

    The WrapMode will set your wrap mode to loop, but as of 4.3 the loop time variable needs to be set as well. The loop time variable is a part of the animation clip settings. If you create the animation clip through the gui it will always be set to true. Unfortuantly the reverse for creating clips through code.

    Here is a screen shot of the debug mode for the animation clip inspector. As you can see, wrap mode is set to loop, but loop time is false. The actual animator will loop on it, but the animation will not loop until loop time is checked.

    $loop time.png

    Unless of Course there is something I am just completely missing. :p
     
    Last edited: Dec 20, 2013
  5. Sildaekar

    Sildaekar

    Joined:
    Jul 8, 2013
    Posts:
    95
    From the way it looks the code you posted above should work, I'm not sure what is going wrong with it....but I am curious as to why there is nothing on the AnimationUtility.GerAnimationClipSettings() method in the documentation :confused: http://docs.unity3d.com/Documentation/ScriptReference/AnimationUtility.html

    I don't know, try saving it closing unity (or rebooting your computer) and trying again? That's the best I know, but maybe someone with a little more information will be able to help you out.
     
  6. Oddeye01

    Oddeye01

    Joined:
    Dec 10, 2013
    Posts:
    5
    Thanks anyway Sild! I tried rebooting, but didn't help. Theres gotta be some way to adjust that variable. It would just help if that area was documented :p.

    Does anyone have experience with accessing the loop time variable?
     
  7. Oddeye01

    Oddeye01

    Joined:
    Dec 10, 2013
    Posts:
    5
    Hate to bump this, but this issue could lead to a hell of a lot of work when creating animations through script. I'm looking at using this to import animations for about 10 -20 characters, each having 30-40 animations. That would mean I'd have to pass through 300 - 800 clips, and set the loop time value for each one. Unfortunately, this only became a problem with 4.3

    Is there some other route I should take with this issue? As I said before, the most difficult part is the lack of documentation in this area.:(
     
  8. Partel-Lang

    Partel-Lang

    Joined:
    Jan 2, 2013
    Posts:
    2,251
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,168
    Yep. Mecanim's biggest weakness: it's impossible to predict which bits are and are not accessible via script (when, of course, ALL bits should be.)
     
  10. Partel-Lang

    Partel-Lang

    Joined:
    Jan 2, 2013
    Posts:
    2,251
    Hey, seems like I was wrong, it can be done after all. See this thread.
     
  11. funnybunnymobile

    funnybunnymobile

    Joined:
    Mar 4, 2015
    Posts:
    5
    I bumped into the same issue. Found a solution if anyone needs it (althoug this topic is old):


    AnimationClipSettings settings = AnimationUtility.GetAnimationClipSettings(animClipToChange);
    settings.loopTime = true;
    AnimationUtility.SetAnimationClipSettings(animClipToChange, settings);
     
  12. landstalker310

    landstalker310

    Joined:
    Feb 22, 2018
    Posts:
    2
    it works, thanks
     
  13. BrainAndBrain

    BrainAndBrain

    Joined:
    Nov 27, 2014
    Posts:
    107
    I tried this, and it works! Except…AnimationUtility is part of UnityEditor, and is not available in builds. Has anyone found a way around this? Any other way to access AnimationClipSettings?

    Wrap Mode could be used, except loopTime doesn't change the Wrap Mode. It stays at Default regardless of whether loopTime is checked or not. Seems like a big oversight.
     
  14. irheks

    irheks

    Joined:
    Aug 26, 2017
    Posts:
    2
    Code (CSharp):
    1.    public static void CreateAnimation(Sprite[]sprites, string name, string path)
    2.    {    
    3.       // load all sprites in "assets/Resources/sprite" folder
    4.       AnimationClip animClip = new AnimationClip();
    5.       animClip.frameRate = 12;   // FPS    
    6.       animClip.wrapMode = WrapMode.Loop;
    7.  
    8.       EditorCurveBinding spriteBinding = new EditorCurveBinding();
    9.       spriteBinding.type = typeof(SpriteRenderer);
    10.       spriteBinding.path = "";
    11.       spriteBinding.propertyName = "m_Sprite";
    12.  
    13.    
    14.       ObjectReferenceKeyframe[] spriteKeyFrames = new ObjectReferenceKeyframe[sprites.Length];
    15.       for (int i = 0; i < (sprites.Length); i++)
    16.       {
    17.          spriteKeyFrames[i] = new ObjectReferenceKeyframe();
    18.          spriteKeyFrames[i].time = (float)i / 12;
    19.          spriteKeyFrames[i].value = sprites[i];
    20.       }
    21.  
    22.       AnimationClipSettings animationClipSettings = new AnimationClipSettings();
    23.  
    24.       animationClipSettings.loopTime = true;
    25.  
    26.       AnimationUtility.SetAnimationClipSettings(animClip, animationClipSettings);
    27.       AnimationUtility.SetObjectReferenceCurve(animClip, spriteBinding, spriteKeyFrames);
    28.    
    29.       //animClip.frameRate = 12;   // FPS    
    30.  
    31.       AssetDatabase.CreateAsset(animClip, "assets/Resources/"+ path + "/" + name + ".anim");
    32.       AssetDatabase.SaveAssets();
    33.       AssetDatabase.Refresh();
    34.  
    35.    
    36.       //AnimationClip clip = Resources.Load<AnimationClip>(path + "/" + name + ".anim");
    37.       //clip.frameRate = 12;
    38.       //AssetDatabase.CreateAsset(clip, "assets/Resources/" + path + "/" + name + ".anim");
    39.    }
    This is a function i made to create animations, from an array of sprites.
     
    hkung and a-t-hellboy like this.
  15. irheks

    irheks

    Joined:
    Aug 26, 2017
    Posts:
    2
    Code (CSharp):
    1.    public static void CreateAnimation(Sprite[]sprites, string name, string path)
    2.    {    
    3.       // load all sprites in "assets/Resources/sprite" folder
    4.       AnimationClip animClip = new AnimationClip();
    5.       animClip.frameRate = 12;   // FPS    
    6.       animClip.wrapMode = WrapMode.Loop;
    7.  
    8.       EditorCurveBinding spriteBinding = new EditorCurveBinding();
    9.       spriteBinding.type = typeof(SpriteRenderer);
    10.       spriteBinding.path = "";
    11.       spriteBinding.propertyName = "m_Sprite";
    12.  
    13.    
    14.       ObjectReferenceKeyframe[] spriteKeyFrames = new ObjectReferenceKeyframe[sprites.Length];
    15.       for (int i = 0; i < (sprites.Length); i++)
    16.       {
    17.          spriteKeyFrames[i] = new ObjectReferenceKeyframe();
    18.          spriteKeyFrames[i].time = (float)i / 12;
    19.          spriteKeyFrames[i].value = sprites[i];
    20.       }
    21.  
    22.       AnimationClipSettings animationClipSettings = new AnimationClipSettings();
    23.  
    24.       animationClipSettings.loopTime = true;
    25.  
    26.       AnimationUtility.SetAnimationClipSettings(animClip, animationClipSettings);
    27.       AnimationUtility.SetObjectReferenceCurve(animClip, spriteBinding, spriteKeyFrames);
    28.    
    29.       //animClip.frameRate = 12;   // FPS    
    30.  
    31.       AssetDatabase.CreateAsset(animClip, "assets/Resources/"+ path + "/" + name + ".anim");
    32.       AssetDatabase.SaveAssets();
    33.       AssetDatabase.Refresh();
    34.  
    35.    
    36.       //AnimationClip clip = Resources.Load<AnimationClip>(path + "/" + name + ".anim");
    37.       //clip.frameRate = 12;
    38.       //AssetDatabase.CreateAsset(clip, "assets/Resources/" + path + "/" + name + ".anim");
    39.    }
    This is a function i made to create animations, from an array of sprites.
     
  16. BrainAndBrain

    BrainAndBrain

    Joined:
    Nov 27, 2014
    Posts:
    107
    @irheks Interesting! Is this for creating them at runtime, or in the editor?
     
  17. lassade

    lassade

    Joined:
    Jan 27, 2013
    Posts:
    81
    You can create clips with loopTime enabled, its kind a hack but it works at least inside the editor and should also work in builds:
    1. Create a blank AnimationClip inside the Resources folder, call it whatever you want
    2. Make sure to turn on Loop Time on that clip
    3. Load the the clip using the Resouces.Load
    4. Use the Instantiate function to clone the clip instead of creating one from scratch
    5. Profit

    Hope it helps anyone :)
     
    fromage0 likes this.
  18. shawnblais

    shawnblais

    Joined:
    Oct 11, 2012
    Posts:
    322
    Payin it forward, this script lets you select any group of anim clips, and set them to Loop in the Project Folder.
    * Tools > Loop Selected Anims (Ctrl + Shift + L)
    * Will prompt before processing
    * Works recursively on folders, or multi-selected assets

    Code (CSharp):
    1.  
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. public class LoopSelectedAnimsShortcut
    6. {
    7.     [MenuItem("Tools/Loop Selected Anims #%l")]
    8.     private static void LoopSelectedAnims()
    9.     {
    10.         LoopSelectedAnims(apply: false);
    11.     }
    12.  
    13.     private static void LoopSelectedAnims(bool apply = false)
    14.     {
    15.         int clipCount = 0;
    16.         int updateCount = 0;
    17.         foreach (AnimationClip clip in Selection.GetFiltered(typeof(AnimationClip), SelectionMode.DeepAssets))
    18.         {
    19.             AnimationClipSettings settings = AnimationUtility.GetAnimationClipSettings(clip);
    20.             if (settings.loopTime == false)
    21.             {
    22.                 if (apply)
    23.                 {
    24.                     settings.loopTime = true;
    25.                     AnimationUtility.SetAnimationClipSettings(clip, settings);
    26.                 }
    27.                 updateCount++;
    28.             }
    29.             clipCount++;
    30.         }
    31.         if (clipCount > 0)
    32.         {
    33.             var plural = updateCount != 1 ? "s" : "";
    34.             if (!apply)
    35.             {
    36.                 if (updateCount == 0)
    37.                 {
    38.                     EditorUtility.DisplayDialog("Nothing to do.", "All selected clips are already set to loop.", "Ok");
    39.                 }
    40.                 else if (EditorUtility.DisplayDialog("Loop Clips?",
    41.                     $"Are you sure you want to set {updateCount} Animation Clip{plural} " +
    42.                     $"to Loop?", "Ok", "Cancel"))
    43.                 {
    44.                     LoopSelectedAnims(apply: true);
    45.                 }
    46.             }
    47.             else
    48.             {
    49.                 EditorUtility.DisplayDialog("Success", $"Updated {updateCount} Animation Clip{plural}", "Ok");
    50.             }
    51.         }
    52.     }
    53. }
    54.  
     
    Last edited: Jul 2, 2020
    ModLunar likes this.
unityunity