So this is a weird one,
I have 5 different scenes, comprised of a number of Cinema4D animations. Naturally, the animations have many sub-objects. The animations themselves are stored in the root, and pretty much all other import settings are set to the default. I also have a Pause/Resume function which is applied to the animation. We’re using the Vuforia (the artist formerly known as Qualcomm AR) library. Whenever the tracker is lost, the animations are paused, and when it is re-found they are resumed. If you’ve used this library before, you know that trackers are lost/found quite often.
I’ve found that one of these animations, once paused/resumed (while inactive, and not started) won’t actually resume. If it is paused/resumed once it has been started, it resumes no problem. Here are my pause/resume functions:
Dictionary<AnimationState, float> animationPauseTimes = new Dictionary<AnimationState, float>();
Dictionary<AudioSource, float> audioPauseTimes = new Dictionary<AudioSource, float>();
//Pause/Resume should now be call on every object. It will handle activation/deactivation
void Pause(Animation anim) {
foreach(AudioSource source in anim.gameObject.GetComponents<AudioSource>()) {
if(source.isPlaying) {
audioPauseTimes[source] = source.time;
source.Pause();
}
}
if (anim.gameObject.active == true) {
foreach (AnimationState state in anim) {
if (anim.IsPlaying(state.clip.name)) {
animationPauseTimes[state] = state.time;
//state.speed = 0; //Shouldn't be neccesary anymore, was causing problems
}
}
anim.gameObject.SetActiveRecursively(false);
}
}
void Resume(Animation anim) {
foreach(AudioSource source in anim.gameObject.GetComponents<AudioSource>()) {
if(audioPauseTimes.ContainsKey(source)){
source.Play();
source.time = audioPauseTimes[source];
audioPauseTimes.Remove(source);
}
}
foreach (AnimationState state in anim) {
if (animationPauseTimes.ContainsKey(state)) {
anim.gameObject.SetActiveRecursively(true);
anim.Play(state.clip.name);
state.time = animationPauseTimes[state];
animationPauseTimes.Remove(state);
//state.speed = 1;
}
}
}
Here’s the code that starts the animation after it is resumed.
if(!bg.active) {
bg.SetActiveRecursively(true);
bg.animation.clip = bg.animation.GetClip("in");
bg.animation.clip.AddEvent(new AnimationEvent { functionName = "OnAnimationEnd", stringParameter = ingredient.name, time = bg.animation.clip.length });
bg.animation.Play("in", PlayMode.StopAll);
bg.animation.Rewind("in");
Debug.Log(bg.animation["in"].speed);
}
Speed is 1, time is 0, but Play() still don’t do nothin! Any ideas?