我有代码:
iTween.MoveTo(
gameObject,
iTween.Hash("x",_x,"z",_y, "time", 2.0f, "easetype",
iTween.EaseType.easeInExpo,
"oncomplete", "afterPlayerMove",
"oncompleteparams", iTween.Hash("value", _fieldIndex)
));
但我不知道如何使用 oncompleteparams 。 官方manual中没有例子。
你如何使用oncompleteparams?
答案 0 :(得分:1)
Here是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
回调函数中,我们将其强制转换为整数以获得结果。
在您的示例中,您使用iTween.Hash
作为oncompleteparams
,因此您必须转换为Hashtable
,因为iTween.Hash返回Hashtable
。之后,要获取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"]);
}
答案 1 :(得分:0)
此外,您可以直接使用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
相比,你在这里会失去一些可读性,因为你只有索引可以使用,但是没有涉及任何转换。