1

我有代码:

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?

4

2 回答 2

5

是该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-05-18T14:48:37.177 回答
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

于 2017-09-17T21:39:06.397 回答