每个Photon中的每个客户两次收到筹款事件

时间:2018-11-10 12:08:57

标签: unity3d photon

每个客户都两次收到我的“筹款活动”,尽管发送一次。我正在使用以下配置。我只发送给一个还是所有客户端都没关系。

 public void OnEnable()
   {
       PhotonNetwork.OnEventCall += OnEvent;
   }

 public void OnDisable()
   {
       PhotonNetwork.OnEventCall -= OnEvent;
   }


public void OnEvent(byte eventCode, object content, int senderId)
{
    Debug.Log("Event recieved");

  if (eventCode ==  genshieldcode )
    {

        object[] mydata = (object[])content;
        int clickedview = (int)mydata[0];
        string command = (string)mydata[1];
        int clickerview = (int)mydata[2];

        GameObject clicker = PhotonView.Find(clickerview).gameObject;
        if (clicker != null)
            if (LocalPlayerInstance != clicker)
                return;




        GameObject clicked = PhotonView.Find(clickedview).gameObject;

        if (command == "generate")
        {
            Debug.Log("Generating Shield");
            GameObject temp = Instantiate(shield);
            temp.transform.position = clicked.transform.position;
        }
        else if(command =="destroy")
        {
            Debug.Log("Destroying shield");
            GameObject temp = GameObject.FindGameObjectWithTag("shield");
            if (temp != null)
                Destroy(temp);

        }


    }

这是我发送活动的方式。该事件仅发送一次,因为我已经检查过了。

object[] myobject = new object[] { clicked.GetComponent<PhotonView>().viewID, "generate",clicker.GetComponent<PhotonView>().viewID };
                     int[] actors = new int[] { clicker.GetComponent<PhotonView>().ownerId };
                     RaiseEventOptions r = new RaiseEventOptions { TargetActors = actors };
                     PhotonNetwork.RaiseEvent(genshieldcode, myobject, true, r)

1 个答案:

答案 0 :(得分:0)

  please try using this  code, may be your event listener is being hooked twice somehow.


public void OnEnable()
   {
       PhotonNetwork.OnEventCall -= OnEvent;
       PhotonNetwork.OnEventCall += OnEvent;
   }

 public void OnDisable()
   {
       PhotonNetwork.OnEventCall -= OnEvent;
   }


public void OnEvent(byte eventCode, object content, int senderId)
{
    PhotonNetwork.OnEventCall -= OnEvent;  // comment this line if you need this function to be called more than once
    Debug.Log("Event recieved");

  if (eventCode ==  genshieldcode )
    {

        object[] mydata = (object[])content;
        int clickedview = (int)mydata[0];
        string command = (string)mydata[1];
        int clickerview = (int)mydata[2];

        GameObject clicker = PhotonView.Find(clickerview).gameObject;
        if (clicker != null)
            if (LocalPlayerInstance != clicker)
                return;




        GameObject clicked = PhotonView.Find(clickedview).gameObject;

        if (command == "generate")
        {
            Debug.Log("Generating Shield");
            GameObject temp = Instantiate(shield);
            temp.transform.position = clicked.transform.position;
        }
        else if(command =="destroy")
        {
            Debug.Log("Destroying shield");
            GameObject temp = GameObject.FindGameObjectWithTag("shield");
            if (temp != null)
                Destroy(temp);

        }


    }