如何在Unity iTween插件中使用oncompleteparams? [英] 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希望将 Object
作为参数.这意味着几乎任何数据类型都可以传递给它.例如, string
, bool
, int
, float
, double
和对象实例
是可以传递给它的数据类型之一.
在回调方面,使回调函数将Object作为参数.在回调函数中,将 Object
参数转换为传递给它的数据类型.
示例:
"oncomplete","afterPlayerMove","oncompleteparams",5)
回调:
afterPlayerMove(对象cmpParams)之后的公共无效{Debug.Log("Result" +(int)cmpParams);}
如您所见,我们将5传递给 oncompleteparams
函数,而5是整数.在 afterPlayerMove
回调函数中,我们将其强制转换回整数以获取结果.
在您的示例中,您为 oncompleteparams
使用了 iTween.Hash
,因此您必须将其强制转换为 Hashtable
,因为iTween.Hash返回了哈希表
.之后,要获取哈希表中的值,您也必须强制转换为该类型.
"oncomplete","afterPlayerMove","oncompleteparams",iTween.Hash("value",_ fieldIndex)
回调:
假设 _fieldIndex
是 int
.
afterPlayerMove(对象cmpParams)之后的公共无效{Hashtable hstbl =(Hashtable)cmpParams;Debug.Log(您的值" +(int)hstbl [值"]);}
最后,您的代码不可读.简化此代码,以使其他人下次可以更轻松地为您提供帮助.
完整的简化示例:
int _x,_y = 6;//范围int _fieldIndex = 4;float floatVal = 2;字符串stringVal =你好";bool boolVal = false;GameObject gObjVal = null;无效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");//创建oncompleteparams哈希表Hashtable paramHashtable = new Hashtable();paramHashtable.Add("value1",_fieldIndex);paramHashtable.Add("value2",floatVal);paramHashtable.Add("value3",stringVal);paramHashtable.Add("value4",boolVal);paramHashtable.Add("value5",gObjVal);//将oncompleteparams参数包含到哈希表中hashtable.Add("oncompleteparams",paramHashtable);iTween.MoveTo(gameObject,hashtable);}public void afterPlayerMove(object cmpParams){Hashtable hstbl =(Hashtable)cmpParams;Debug.Log(您的int值" +(int)hstbl ["value1"]);Debug.Log(您的浮点值" +(float)hstbl ["value2"]);Debug.Log(您的字符串值" +(string)hstbl ["value3"]);Debug.Log(您的bool值" +(bool)hstbl ["value4"]);Debug.Log(您的GameObject值" +(GameObject)hstbl ["value5"]);}
I have code:
iTween.MoveTo(
gameObject,
iTween.Hash("x",_x,"z",_y, "time", 2.0f, "easetype",
iTween.EaseType.easeInExpo,
"oncomplete", "afterPlayerMove",
"oncompleteparams", iTween.Hash("value", _fieldIndex)
));
but I don't know how to use oncompleteparams. There is no example in the official manual.
How do you use oncompleteparams?
Here is the direct doc for the Itween.MoveTo
function.
oncompleteparams expects Object
as argument. This means that almost any datatype can be passed in to it. For example, string
, bool
, int
, float
, double
, and object instance
are one of datatypes that can be passed to it.
On the callback side, you make the callback function take Object as parameter. Inside the callback function, you cast the Object
parameter to the type of datatype you passed to it.
Example:
"oncomplete", "afterPlayerMove",
"oncompleteparams", 5)
Callback:
public void afterPlayerMove(object cmpParams)
{
Debug.Log("Result" + (int)cmpParams);
}
As you can see, we are passing 5 to the oncompleteparams
function and 5 is an integer. In the afterPlayerMove
callback function, we cast it back to integer to get the result.
In your example, you used iTween.Hash
for the oncompleteparams
so you must cast to Hashtable
since iTween.Hash returns Hashtable
. After that, to get the values in the Hashtable, you have to cast to that type too.
"oncomplete", "afterPlayerMove",
"oncompleteparams", iTween.Hash("value", _fieldIndex)
Callback:
Assuming that _fieldIndex
is an int
.
public void afterPlayerMove(object cmpParams)
{
Hashtable hstbl = (Hashtable)cmpParams;
Debug.Log("Your value " + (int)hstbl["value"]);
}
Finally, your code is unreadable. Simplify this code to make it easier for others to help you next time.
Complete simplified Example:
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"]);
}
这篇关于如何在Unity iTween插件中使用oncompleteparams?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!