Unity3D: iTween oncomplete not working

本文关键字:not working oncomplete iTween Unity3D | 更新日期: 2023-09-27 18:12:56

我正在制作一款Unity2游戏,我想让Boom在iTween动画的最后消失。

下面是部分代码

void OnMouseDown() {    
    if (ChosePosition == false)
        ChosePosition = true;
    // make Star stop
    else if (ChosePosition == true && ChoseForce == false) {
        ChoseForce = true;
        //Compute Force power 
        PowerDegree = Mathf.Abs (Star.transform.position.y - StarPosition)/ Mathf.Abs(StarendPosition - StarPosition) * 100;
        //print (PowerDegree);
        Vector3 NewBoomPostion = new Vector3 (Luncher.transform.position.x, BoomPosition, 85);
        GameObject newBoom = Instantiate(Boom, NewBoomPostion , Quaternion.identity) as GameObject;
        iTween.MoveTo (newBoom, iTween.Hash 
            ("y",BoomPosition+((BoomendPosition-BoomPosition)/100*PowerDegree),
            "speed",Boomspeed,
            "EaseType",BoomeaseType,
            "LoopType",BoomloopType,
            "oncomplete","BoomComplete"
            ));

        CarrierReset = true;
        ChosePosition  = false;
        ChoseForce = false;
        //After Shot, Luncher will move
        iTween.Resume(Luncher);
        //After Shot, Luncher Carrier will display
        displayCarrierOrNot = true;
    }
}

void BoomComplete(){
    print("complete");
}

但是我在控制台看不到"完成"。

Unity3D: iTween oncomplete not working

这篇关于Unity Answers的文章描述了为什么你有这个问题:

默认情况下,iTween尝试调用你为它正在动画的对象提供的回调方法——在你的例子中是mainCamera.gameObject。由于"onCameraShakeComplete"并不驻留在该对象上,因此它永远不会被调用。你有两个选择:将该方法移动到mainCamera.gameObject上,或者简单地提供gameObject的"onCompleteTarget"来告诉iTween使用正在设置此iTween的GameObject。

在你的例子中,它动画的对象是newBoom,但是你在你自己的组件上有回调。为了让渐变使用当前的回调,你可以在Hash调用中添加一个参数:

iTween.MoveTo (newBoom, iTween.Hash(
    // ...
    "oncomplete", "BoomComplete",
    "oncompletetarget", gameObject
    ));