简体   繁体   中英

Wait for multiple animation finish

What I would like to do is

move each 3 panels by animation

Do something when every animations finish.

in my panelCtrl.cs, it moves panels.

public class PanelCtrl : MonoBehaviour {
void Update () {
    if (moving){
        distCovered = (Time.time - startTime) * moveSpeed * 4;

        fracJourney = distCovered / journeyLength;

        transform.position = Vector3.Lerp(startMarker, endMarker,fracJourney);

        if (fracJourney >= 1){
            moving = false;

        }
    }
}

//when moving
public float distCovered;
public Vector3 startMarker;
public Vector3 endMarker;
private float moveSpeed = 10.0F;
public float startTime;
public float journeyLength;
public float fracJourney;
public bool moving = false;

public void Move(Transform distination){
    endMarker = distination.position;
    startMarker = this.transform.position;
    startTime = Time.time;
    journeyLength = Vector3.Distance(startMarker,endMarker);
    moving = true;
}

in my Gamectrl.cs

PanelCtrl.Move (panel1.transform);
PanelCtrl.Move (panel2.transform);
PanelCtrl.Move (panel3.transform);
Debug.log('finished!');

For now, Debug.log('finished') is called before animation finished.

However, I want to do Debug.log('finished') after three move finished.

I have two ideas.

1) use coroutine?? But how?? make three coroutine and how can I check each animation finishes??

2)check move flag? I tried to check move flag,

So I try this in my GameCtrl.cs, but it hung up!

void Update () {
bool readyForSpawn = false; // Wait for all movement finishing
while (readyForSpawn == false) {
    readyForSpawn = true;
    foreach (GameObject obj in GameObject.FindObjectsOfType(typeof(GameObject))) {
        if (obj.transform.parent == null && obj.name == "Panel(Clone)") {
            PanelCtrl pan = obj.GetComponent ("PanelCtrl") as PanelCtrl;
            if (pan.moving == true){
                Debug.Log ("moving:true");
                readyForSpawn = false;
            }
        }
    }
}

Forget about doing it by code I would say. Create an Animation via Animator component.

The Animation window allows to use Animation event.

http://docs.unity3d.com/Manual/animeditor-AnimationEvents.html

If you really want to keep the code, then you would use an event system that uses a integer flag. Your panel controller would listen to the animation and when one is done it triggers the event. Panel listens and records it. Then when all three are done, it does what should be

public class PanelCtrl : MonoBehaviour {

    public static event Action OnCompletion;
    void Update () {
    if (moving){
        distCovered = (Time.time - startTime) * moveSpeed * 4;

        fracJourney = distCovered / journeyLength;

        transform.position = Vector3.Lerp(startMarker, endMarker,fracJourney);

        if (fracJourney >= 1){
            moving = false;
            OnCompletion();
        }
    }
}

Then you have a controller to listen:

public class Manager:MonoBehaviour {
    void Awake(){
        PanelCtrl.OnCompletion += HandleCompletion;
    }
}

private int flag = 3 
void HandleCompletion (){
     if(--flag == 0){ Debug.Log("All done"); }
}

I would clearly consider that way to be dirty but it should work and requires less modification from your existing code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM