如何理解谁想要一个方法的结果

时间:2016-01-20 07:23:46

标签: c# unity3d

我有两个方法alphaClicked()betaClicked(),为了让它们工作,我们需要连接到网络中的服务,所以我有另一个名为connect()的方法,所以在这两个类中我们需要首先连接,以便检查我们是否已连接,然后如果不是,那么我们调用connect()方法,然后我们在ActionConnectionResultReceived中获得连接的结果,所以如果我们连接成功,我想调用alphaClicked ()如果alpha是请求connect()的人,如果betaClicked()是请求betaClicked()方法的人connect(),那么调用alpha最好的方法是什么?

总结这是问题所在:

betaconnect需要connect,但ActionConnectionResultReceived结果不会立即返回,因此我们通过alpha获取结果,以便如何获取谁称之为连接是betaprivate void alphaClicked() { if(GooglePlayConnection.State == GPConnectionState.STATE_CONNECTED) { //do something } else { Connect(); } } private void betaClicked() { if(GooglePlayConnection.State == GPConnectionState.STATE_CONNECTED) { //do something else } else { Connect(); } } private void ActionConnectionResultReceived(GooglePlayConnectionResult result) { if (result.IsSuccess) { Debug.Log("Connected!"); //if alpha requested call alpha if beta requested call beta //how to do this? } else { Debug.Log("Cnnection failed with code: " + result.code.ToString()); } } private void Connect() { GooglePlayConnection.Instance.Connect(); }

@Override
public boolean onTouchEvent(MotionEvent ev) {
    boolean handle = ev.getActionMasked() == MotionEvent.ACTION_MOVE;
    return handle ? handle : super.onTouchEvent(ev);
}

1 个答案:

答案 0 :(得分:2)

如果没有Asyncevent handler,并且每次只调用一个connect,则可以引入一个存储最后一个调用者的变量:

string gCaller = "";
private void Connect(string caller){
    gCaller = caller;
    //something else
}

然后像这样使用它:

private void alphaClicked()
{
   if(GooglePlayConnection.State == GPConnectionState.STATE_CONNECTED) {
        //do something
    }
   else
   {
       Connect("alpha");
   }
}

private void betaClicked()
{
   if(GooglePlayConnection.State == GPConnectionState.STATE_CONNECTED) {
        //do something else
    }
   else
   {
       Connect("beta");
   }
}

然后,当您需要最后一个创建连接的人时,使用gCaller中的信息。

private void ActionConnectionResultReceived(GooglePlayConnectionResult result)
{
    if (result.IsSuccess)
    {
        Debug.Log("Connected!");
        switch (gCaller){
            case "alpha":
              alphaClicked();
              break;
            case "beta":
              betaClicked();
              break;
            default:
              break;
        }
    }
    else
    {
        Debug.Log("Cnnection failed with code: " + result.code.ToString());
    }
}

但最好的方法是为async事件创建Connected或事件处理程序。看一下这个MSDN article