查看iTween的源碼找到ColorFrom函數,看該函數的注釋“/// Changes a GameObject's color values instantly then returns them to the provided properties over time with FULL customization options. If a GUIText or GUITexture component is attached, it will become the target of the animation.” 原來iTween支持“GUIText”和“GUITexture”組件,Image和RawImage要想使用iTween的相應顏色動畫函數,就要在相應函數中添加支持Image和RawImage的代碼。先看下效果:
創建了一個Image,將腳本Test.cs添加到該Image上。

1 public class Test : MonoBehaviour { 2 3 // Use this for initialization 4 void Start () { 5 iTween.ColorFrom (this.gameObject, Color.red, 2f); 6 } 7 }
在iTween中找到函數,
public static void ColorFrom(GameObject target, Hashtable args)
在函數中找到
//set tempColor and base fromColor: if(target.GetComponent<GUITexture>()){ tempColor=fromColor=target.guiTexture.color; }else if(target.GetComponent<GUIText>()){ tempColor=fromColor=target.guiText.material.color; }else if(target.renderer){ tempColor=fromColor=target.renderer.material.color; }else if(target.light){ tempColor=fromColor=target.light.color; }
在其后添加支持Image和RawImage即可,修改后為
1 //set tempColor and base fromColor: 2 if(target.GetComponent<GUITexture>()){ 3 tempColor=fromColor=target.guiTexture.color; 4 }else if(target.GetComponent<GUIText>()){ 5 tempColor=fromColor=target.guiText.material.color; 6 }else if(target.renderer){ 7 tempColor=fromColor=target.renderer.material.color; 8 }else if(target.light){ 9 tempColor=fromColor=target.light.color; 10 }else if(target.GetComponent<Image>()){ 11 tempColor = fromColor = target.GetComponent<Image> ().color; 12 }else if(target.GetComponent<RawImage>()){ 13 tempColor = fromColor = target.GetComponent<RawImage> ().color; 14 }
在其他相應的函數中有類似判斷的地方繼續添加相應的條件判斷即可。
總共要修改的函數:
1 public static void ColorFrom(GameObject target, Hashtable args) 2 public static void ColorUpdate(GameObject target, Hashtable args) 3 void GenerateColorToTargets() 4 void ApplyColorToTargets()
修改好的iTween文件:
鏈接: http://pan.baidu.com/s/1bn8QvmB 密碼: pbpr