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.

UNDERSTANDING ITWEEN CALLBACKS的更多相关文章

  1. Understanding Asynchronous IO With Python 3.4's Asyncio And Node.js

    [转自]http://sahandsaba.com/understanding-asyncio-node-js-python-3-4.html Introduction I spent this su ...

  2. [转] Understanding JavaScript’s async await

    PS:Promise的用处是异步调用,这个对象使用的时候,call then函数,传一个处理函数进去,处理异步调用后的结果 Promise<Action>这样的对象呢,异步调用后的结果是一 ...

  3. Understanding JavaScript Function Invocation and "this"

    Understanding JavaScript Function Invocation and "this" 11 Aug 2011 Over the years, I've s ...

  4. Async Performance: Understanding the Costs of Async and Await

    Stephen Toub Download the Code Sample Asynchronous programming has long been the realm of only the m ...

  5. jquery.Callbacks的实现

    前言 本人是一个热爱前端的菜鸟,一直喜欢学习js原生,对于jq这种js库,比较喜欢理解他的实现,虽然自己能力有限,水平很低,但是勉勉强强也算是能够懂一点吧,对于jq源码解读系列,博客园里有很多,推荐大 ...

  6. 解决ugui中Image使用iTween的ColorTo、ColorFrom等不生效

    查看iTween的源码找到ColorFrom函数,看该函数的注释“/// Changes a GameObject's color values instantly then returns them ...

  7. GOOD MEETINGS CREATE SHARED UNDERSTANDING, NOT BRDS!

      Deliverables and artifacts were a focal point of BA work during the early part of my career. If I ...

  8. jQuery.Callbacks之demo

    jQuery.Callbacks是jquery在1.7版本之后加入的,是从1.6版中的_Deferred对象中抽离的,主要用来进行函数队列的add.remove.fire.lock等操作,并提供onc ...

  9. jQuery 2.0.3 源码分析 回调对象 - Callbacks

    源码API:http://api.jquery.com/jQuery.Callbacks/ jQuery.Callbacks()是在版本1.7中新加入的.它是一个多用途的回调函数列表对象,提供了一种强 ...

随机推荐

  1. 【DL】梯度下降小结

    温故知新 https://www.cnblogs.com/pinard/p/5970503.html

  2. bootstrap-select 多选下拉框使用教程

    http://silviomoreto.github.io/bootstrap-select/ 一.使用bootstrap-select组件时,先引用下列文件 最后一个文件 defaults-zh_C ...

  3. [Python] 05 - Load data from Files

    文件读写 一.文件打开 传统方法 >>> f = open('data.txt', 'w') # Make a new file in output mode ('w' is wri ...

  4. linux erase

    map的erase windows和linux不同,而迭代器弄不好就失效 1 #include <iostream> 2 #include <map> 3 #include & ...

  5. 【Important】数据库索引原理

    为什么要给表加上主键? 为什么加索引后会使查询变快? 为什么加索引后会使写入.修改.删除变慢? 什么情况下要同时在两个字段上建索引? 想理解索引原理必须清楚一种数据结构(平衡树非二叉)也就是b tre ...

  6. day_5.22 py

    #!/usr/bin/env/python #-*-coding:utf-8-*- ''' 私有化 __相当于prevate 外部不能直接调用,只能通过set get方法用 property ''' ...

  7. js函数 eql,equal,equalp

    function eql(obj, other) { if(stringp(obj) && stringp(other) && obj === other) retur ...

  8. CentOS和Redhat单用户模式

    当系统无法启动时,可能是/etc/fstab挂载错误导致这时候可以进入单用户模式修改配置文件后重启 重启系统出现以下界面按e 选择第二栏按e健 在后面输入1回车回到上一个页面按b健启动 进入单用户模式 ...

  9. svn的安装方法

    SVN的安装很常用.但是我好像经常用了之后就忘记.这次把笔记放在这里,如果以后忘了,就可以随时查看了. 步骤: 一.下载SVN 这里常用的是site.zip. 之前是手头有现成的site.zip.今天 ...

  10. 洛谷P1403 约数研究【思维】

    题目:https://www.luogu.org/problemnew/show/P1403 题意: 定义$f(n)$为n的因子个数.给定一个数n,求$f(1)$到$f(n)$之和. 思路: 最直接的 ...