[英]How to use oncompleteparams in Unity iTween plugin?
我有代碼:
iTween.MoveTo(
gameObject,
iTween.Hash("x",_x,"z",_y, "time", 2.0f, "easetype",
iTween.EaseType.easeInExpo,
"oncomplete", "afterPlayerMove",
"oncompleteparams", iTween.Hash("value", _fieldIndex)
));
但我不知道如何使用oncompleteparams 。 官方手冊中沒有示例。
您如何使用oncompleteparams?
這是Itween.MoveTo
函數的直接文檔。
oncompleteparams期望Object
作為參數。 這意味着幾乎任何數據類型都可以傳遞給它。 例如, string
, bool
, int
, float
, double
和object instance
是可以傳遞給它的數據類型之一。
在回調方面,使回調函數將Object作為參數。 在回調函數內部,將Object
參數轉換為傳遞給它的數據類型。
例:
"oncomplete", "afterPlayerMove",
"oncompleteparams", 5)
打回來:
public void afterPlayerMove(object cmpParams)
{
Debug.Log("Result" + (int)cmpParams);
}
如您所見,我們將5傳遞給oncompleteparams
函數,而5是整數。 在afterPlayerMove
回調函數中,我們將其afterPlayerMove
回整數以獲取結果。
在您的示例中,您將iTween.Hash
用作oncompleteparams
因此您必須將其強制轉換為Hashtable
因為iTween.Hash返回Hashtable
。 之后,要獲取哈希表中的值,您也必須強制轉換為該類型。
"oncomplete", "afterPlayerMove",
"oncompleteparams", iTween.Hash("value", _fieldIndex)
打回來:
假設_fieldIndex
是一個int
。
public void afterPlayerMove(object cmpParams)
{
Hashtable hstbl = (Hashtable)cmpParams;
Debug.Log("Your value " + (int)hstbl["value"]);
}
最后,您的代碼不可讀。 簡化此代碼,以使其他人下次可以更輕松地為您提供幫助。
完成簡化示例:
int _x, _y = 6;
//Parameter
int _fieldIndex = 4;
float floatVal = 2;
string stringVal = "Hello";
bool boolVal = false;
GameObject gObjVal = null;
void Start()
{
Hashtable hashtable = new Hashtable();
hashtable.Add("x", _x);
hashtable.Add("z", _y);
hashtable.Add("time", 2.0f);
hashtable.Add("easetype", iTween.EaseType.easeInExpo);
hashtable.Add("oncomplete", "afterPlayerMove");
//Create oncompleteparams hashtable
Hashtable paramHashtable = new Hashtable();
paramHashtable.Add("value1", _fieldIndex);
paramHashtable.Add("value2", floatVal);
paramHashtable.Add("value3", stringVal);
paramHashtable.Add("value4", boolVal);
paramHashtable.Add("value5", gObjVal);
//Include the oncompleteparams parameter to the hashtable
hashtable.Add("oncompleteparams", paramHashtable);
iTween.MoveTo(gameObject, hashtable);
}
public void afterPlayerMove(object cmpParams)
{
Hashtable hstbl = (Hashtable)cmpParams;
Debug.Log("Your int value " + (int)hstbl["value1"]);
Debug.Log("Your float value " + (float)hstbl["value2"]);
Debug.Log("Your string value " + (string)hstbl["value3"]);
Debug.Log("Your bool value " + (bool)hstbl["value4"]);
Debug.Log("Your GameObject value " + (GameObject)hstbl["value5"]);
}
此外,您可以直接使用Arrays
。 例如:
"oncomplete", "SomeMethod",
"oncompleteparams", new int[]{value1, 40, value2}
連同方法
void SomeMethod(int[] values) {
Debug.Log(string.Format("Value1: {0}; Number: {1}; Value2: {2}",
values[0], values[1], values[2]));
}
當然,與HashTable
相比,您在這里會失去一些可讀性,因為您只有要使用的索引但不涉及任何強制轉換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.