【问题标题】:Raise Event being received twice by each client in Photon在 Photon 中,每个客户端都收到两次引发事件
【发布时间】:2018-11-10 12:08:56
【问题描述】:

我的提升事件被每个客户接收两次,尽管它只被发送一次。我正在使用以下配置。我将它发送给一个或所有客户都没有关系。

 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)

【问题讨论】:

  • 确保订阅PhotonNetwork.OnEventCall 事件的组件不会在场景中出现两次。还要确保您拨打PhotonNetwork.RaiseEvent 一次而不是两次。

标签: unity3d photon


【解决方案1】:
  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);

        }


    }

【讨论】:

    猜你喜欢
    • 2014-11-25
    • 2015-05-10
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 2019-02-01
    • 2021-02-04
    相关资源
    最近更新 更多