zoukankan      html  css  js  c++  java
  • UNDERSTANDING ITWEEN CALLBACKS

    One of the most frequent problems I see people have with iTween is with callbacks that don’t fire.

    At it’s core iTween leverages Unity’s SendMessage method for carrying out it’s 3 callbacks: “onStart”, “onUpdate” and “onComplete”. When you add an iTween to a GameObject, iTween will throw all callbacks against the GameObject that is being animated. Where most people have difficulties is when they assign iTweens to GameObjects that don’t physically contain the methods they are attempting to call.

    For example, the following code with never execute the “ShakeComplete” method because the “target” GameObject does not have a script attachted to it containing this method:

    using UnityEngine;
    using System.Collections;
    
    public class UnderstandingCallbacks : MonoBehaviour
    {
    	public GameObject target;
    	
    	void Start ()
    	{
    		iTween.ShakePosition(target, iTween.Hash("x", 1, "onComplete", ShakeComplete));
    	}
    	
    	void ShakeComplete(){
    		Debug.Log("The shake completed!");	
    	}
    }
    

    There’s 2 ways to correct the previous script. The long route: create another script, attach it to our actual “target” GameObject and transfer the “ShakeComplete” method to that new script. The easy route: utilize iTween’s callback “target modifiers”!

    Each of iTween’s 3 callbacks have an additional “target modifier” that can be used to tell iTween to look somewhere else for the callback method: “onStartTarget”, “onUpdateTarget” and “onCompleteTarget”.

    The following corrected script will now fire the “onComplete” callback since we are now using “onCompleteTarget” to tell iTween that the “ShakeComplete” method is located on the GameObject that actually set up the iTween:

    using UnityEngine;
    using System.Collections;
    
    public class UnderstandingCallbacks : MonoBehaviour
    {
    	public GameObject target;
    	
    	void Start ()
    	{
    		iTween.ShakePosition(target, iTween.Hash("x", 1, "onComplete", "ShakeComplete", "onCompleteTarget", gameObject));
    	}
    	
    	void ShakeComplete(){
    		Debug.Log("The shake completed!");	
    	}
    }
    

    I hope this helps clear up unsuccessful callbacks in iTween.

查看全文
  • 相关阅读:
    【后缀自动机例题】
    【BZOJ-1146】网络管理Network DFS序 + 带修主席树
    【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集
    【BZOJ-2653】middle 可持久化线段树 + 二分
    【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组
    【BZOJ-2938】病毒 Trie图 + 拓扑排序
    【BZOJ-4726】Sabota? 树形DP
    【BZOJ-3143】游走 高斯消元 + 概率期望
    【BZOJ-3270】博物馆 高斯消元 + 概率期望
    laravel框架中所用到的依赖注入
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10776195.html
  • Copyright © 2011-2022 走看看