unity, itween 对不透明对象使用FadeTo需要先更换material
跟自己实现fade一样,使用itween对不透明对象FadeTo前也要先更换material为透明material。
设player的Hierarchy如下:
player
--aniRoot
----head
----body
其中head和body都挂有如下脚本:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Assertions.Must;
public class materialControl : MonoBehaviour {
public List<string> m_keyList;
public List<Material> m_matList;
private Dictionary<string,Material> m_matDic=new Dictionary<string, Material>();//dictionary will not show in inspeactor
void Awake(){
int keyCount = m_keyList.Count;
int matCount = m_matList.Count;
keyCount.MustBeEqual(matCount);
for (int i=0; i<keyCount; i++) {
string key=m_keyList[i];
Material mat=m_matList[i];
m_matDic.Add (key,mat);
}
}
public Material getMaterialByKey(string key){
Material mat=null;
bool isGot=m_matDic.TryGetValue (key,out mat);
if (isGot) {
return mat;
} else {
return null;
}
}
public void changeMaterialTo(string key){
Material mat=getMaterialByKey (key);
if (mat) {
GetComponent<MeshRenderer> ().material = mat;
} else {
Debug.Log("error: material not found!");
}
}
}
player挂有如下脚本:
using UnityEngine;
using System.Collections;
using UnityEngine.Assertions.Must;
public class playerControl : MonoBehaviour {
void fadeOut(){
Transform aniRoot = transform.FindChild ("aniRoot");
Transform[] allChildren = aniRoot.GetComponentsInChildren<Transform> ();
foreach (Transform child in allChildren) {
materialControl materialControlScript = child.GetComponent<materialControl> ();
if (materialControlScript) {
materialControlScript.changeMaterialTo ("fadeMat");
}
}
//ref: http://www.xuanyusong.com/archives/2052
Hashtable args = new Hashtable();
args.Add("alpha",0);
args.Add("time",0.2f);
args.Add ("oncomplete", "disActive");
args.Add("oncompleteparams", gameObject);
args.Add("oncompletetarget", gameObject);
iTween.FadeTo (aniRoot.gameObject, args);
}
void disActive(GameObject target){
target.SetActive (false);
}
......
}