我有密码:
iTween.MoveTo(
gameObject,
iTween.Hash("x",_x,"z",_y, "time", 2.0f, "easetype",
iTween.EaseType.easeInExpo,
"oncomplete", "afterPlayerMove",
"oncompleteparams", iTween.Hash("value", _fieldIndex)
));但我不知道该怎么使用。在官方的手册中没有任何例子。
你是如何使用单完全参数的?
发布于 2017-05-18 14:48:37
这里是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"]);
}发布于 2017-09-17 21:39:06
此外,您可以直接使用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相比,您在这里将失去一些可读性,因为您只有可以使用的索引,但不涉及转换。
https://stackoverflow.com/questions/44049510
复制相似问题