<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12
      代碼改變世界

      Photon自定義加載Resource之外的資源

      2019-01-16 13:39  風(fēng)色年代  閱讀(715)  評論(0)    收藏  舉報

      PhotonNetwork.cs 結(jié)尾添加如下代碼:

          #region >>> Photon自定義異步加載GameObject
      
          public delegate void CustomLoader(string prefabName, Action<UnityEngine.Object> ac);
          public static CustomLoader _loader;
          public static void RegisterCustomLoaderToPhoton(CustomLoader loader)
          {
              _loader = loader;
          }
      
          public static bool InstantiateCustom(string prefabName, Vector3 position, Quaternion rotation, byte group, object[] data, Action<GameObject> callback)
          {
              if (!connected || (InstantiateInRoomOnly && !inRoom))
              {
                  Debug.LogError("Failed to Instantiate prefab: " + prefabName + ". Client should be in a room. Current connectionStateDetailed: " + PhotonNetwork.connectionStateDetailed);
                  return false;
              }
      
              GameObject prefabGo;
              if (!UsePrefabCache || !PrefabCache.TryGetValue(prefabName, out prefabGo))
              {
                  _loader(prefabName, (ab) =>
                  {
                      prefabGo = (GameObject)(ab);
      
                      if (UsePrefabCache)
                      {
                          PrefabCache.Add(prefabName, prefabGo);
                      }
      
                      if (prefabGo == null)
                      {
                          Debug.LogError("Failed to Instantiate prefab: " + prefabName + ". Verify the Prefab is in a Resources folder (and not in a subfolder)");
                          callback.Invoke(null);
                      }
      
                      // a scene object instantiated with network visibility has to contain a PhotonView
                      if (prefabGo.GetComponent<PhotonView>() == null)
                      {
                          Debug.LogError("Failed to Instantiate prefab:" + prefabName + ". Prefab must have a PhotonView component.");
                          callback.Invoke(null);
                      }
      
                      Component[] views = (Component[])prefabGo.GetPhotonViewsInChildren();
                      int[] viewIDs = new int[views.Length];
                      for (int i = 0; i < viewIDs.Length; i++)
                      {
                          //Debug.Log("Instantiate prefabName: " + prefabName + " player.ID: " + player.ID);
                          viewIDs[i] = AllocateViewID(player.ID);
                      }
      
                      // Send to others, create info
                      Hashtable instantiateEvent = networkingPeer.SendInstantiate(prefabName, position, rotation, group, viewIDs, data, false);
      
                      // Instantiate the GO locally (but the same way as if it was done via event). This will also cache the instantiationId
                      callback.Invoke(networkingPeer.DoInstantiate(instantiateEvent, networkingPeer.LocalPlayer, prefabGo));
                  });
                  return true;
              }
              if (prefabGo != null)
              {
                  // a scene object instantiated with network visibility has to contain a PhotonView
                  if (prefabGo.GetComponent<PhotonView>() == null)
                  {
                      Debug.LogError("Failed to Instantiate prefab:" + prefabName + ". Prefab must have a PhotonView component.");
                      callback.Invoke(null);
                  }
      
                  Component[] views = (Component[])prefabGo.GetPhotonViewsInChildren();
                  int[] viewIDs = new int[views.Length];
                  for (int i = 0; i < viewIDs.Length; i++)
                  {
                      //Debug.Log("Instantiate prefabName: " + prefabName + " player.ID: " + player.ID);
                      viewIDs[i] = AllocateViewID(player.ID);
                  }
      
                  // Send to others, create info
                  Hashtable instantiateEvent = networkingPeer.SendInstantiate(prefabName, position, rotation, group, viewIDs, data, false);
                  callback.Invoke(networkingPeer.DoInstantiate(instantiateEvent, networkingPeer.LocalPlayer, prefabGo));
                  return true;
              }
              return false;
          }
      
          #endregion
      

        

      NetworkingPeer.cs 結(jié)尾添加如下代碼:

          #region >>> Photon自定義異步加載GameObject
      
          public delegate void CustomInstantiatedHandler(PhotonPlayer photonPlayer, GameObject go);
          public static event CustomInstantiatedHandler OnCustomInstantiated;
      
          internal void DoInstantiateCustom(Hashtable evData, PhotonPlayer photonPlayer, GameObject resourceGameObject, Action<GameObject> callback)
          {
              // some values always present:
              string prefabName = (string)evData[(byte)0];
              int serverTime = (int)evData[(byte)6];
              int instantiationId = (int)evData[(byte)7];
      
              Vector3 position;
              if (evData.ContainsKey((byte)1))
              {
                  position = (Vector3)evData[(byte)1];
              }
              else
              {
                  position = Vector3.zero;
              }
      
              Quaternion rotation = Quaternion.identity;
              if (evData.ContainsKey((byte)2))
              {
                  rotation = (Quaternion)evData[(byte)2];
              }
      
              byte group = 0;
              if (evData.ContainsKey((byte)3))
              {
                  group = (byte)evData[(byte)3];
              }
      
              short objLevelPrefix = 0;
              if (evData.ContainsKey((byte)8))
              {
                  objLevelPrefix = (short)evData[(byte)8];
              }
      
              int[] viewsIDs;
              if (evData.ContainsKey((byte)4))
              {
                  viewsIDs = (int[])evData[(byte)4];
              }
              else
              {
                  viewsIDs = new int[1] { instantiationId };
              }
      
              object[] incomingInstantiationData;
              if (evData.ContainsKey((byte)5))
              {
                  incomingInstantiationData = (object[])evData[(byte)5];
              }
              else
              {
                  incomingInstantiationData = null;
              }
      
              // SetReceiving filtering
              if (group != 0 && !this.allowedReceivingGroups.Contains(group))
              {
                  if (callback != null)
                  {
                      callback.Invoke(null);
                  }
                  return; // Ignore group
              }
      
              if (ObjectPool != null) // 使用對象池的情況
              {
                  GameObject go = ObjectPool.Instantiate(prefabName, position, rotation);
      
                  PhotonView[] photonViews = go.GetPhotonViewsInChildren();
                  if (photonViews.Length != viewsIDs.Length)
                  {
                      throw new Exception("Error in Instantiation! The resource's PhotonView count is not the same as in incoming data.");
                  }
                  for (int i = 0; i < photonViews.Length; i++)
                  {
                      photonViews[i].didAwake = false;
                      photonViews[i].viewID = 0;
      
                      photonViews[i].prefix = objLevelPrefix;
                      photonViews[i].instantiationId = instantiationId;
                      photonViews[i].isRuntimeInstantiated = true;
                      photonViews[i].instantiationDataField = incomingInstantiationData;
      
                      photonViews[i].didAwake = true;
                      photonViews[i].viewID = viewsIDs[i];    // with didAwake true and viewID == 0, this will also register the view
                  }
      
                  // Send OnPhotonInstantiate callback to newly created GO.
                  // GO will be enabled when instantiated from Prefab and it does not matter if the script is enabled or disabled.
                  go.SendMessage(OnPhotonInstantiateString, new PhotonMessageInfo(photonPlayer, serverTime, null), SendMessageOptions.DontRequireReceiver);
                  if (callback != null)
                  {
                      callback.Invoke(go);
                      if (OnCustomInstantiated != null)
                      {
                          OnCustomInstantiated.Invoke(photonPlayer, go);
                      }
                  }
                  return;
              }
              else
              {
                  // load prefab, if it wasn't loaded before (calling methods might do this)
                  if (resourceGameObject == null)
                  {
                      if (!NetworkingPeer.UsePrefabCache || !NetworkingPeer.PrefabCache.TryGetValue(prefabName, out resourceGameObject))
                      {
                          //resourceGameObject = (GameObject)Resources.Load(prefabName, typeof(GameObject));
                          PhotonNetwork._loader(prefabName, (ab) =>
                          {
                              resourceGameObject = (GameObject)ab;
                              if (NetworkingPeer.UsePrefabCache)
                              {
                                  NetworkingPeer.PrefabCache.Add(prefabName, resourceGameObject);
                              }
                              if (resourceGameObject == null)
                              {
                                  Debug.LogError("PhotonNetwork error: Could not find the bundle [" + prefabName + "]. Please verify you have this gameobject in a StreamingAssets/Res folder.");
                                  if (callback != null)
                                  {
                                      callback.Invoke(null);
                                  }
                                  return;
                              }
      
                              // now modify the loaded "blueprint" object before it becomes a part of the scene (by instantiating it)
                              PhotonView[] resourcePVs = resourceGameObject.GetPhotonViewsInChildren();
                              if (resourcePVs.Length != viewsIDs.Length)
                              {
                                  throw new Exception("Error in Instantiation! The resource's PhotonView count is not the same as in incoming data.");
                              }
      
                              for (int i = 0; i < viewsIDs.Length; i++)
                              {
                                  // NOTE instantiating the loaded resource will keep the viewID but would not copy instantiation data, so it's set below
                                  // so we only set the viewID and instantiationId now. the instantiationData can be fetched
                                  resourcePVs[i].viewID = viewsIDs[i];
                                  resourcePVs[i].prefix = objLevelPrefix;
                                  resourcePVs[i].instantiationId = instantiationId;
                                  resourcePVs[i].isRuntimeInstantiated = true;
                              }
      
                              this.StoreInstantiationData(instantiationId, incomingInstantiationData);
      
                              // load the resource and set it's values before instantiating it:
                              GameObject go = (GameObject)GameObject.Instantiate(resourceGameObject, position, rotation);
      
                              for (int i = 0; i < viewsIDs.Length; i++)
                              {
                                  // NOTE instantiating the loaded resource will keep the viewID but would not copy instantiation data, so it's set below
                                  // so we only set the viewID and instantiationId now. the instantiationData can be fetched
                                  resourcePVs[i].viewID = 0;
                                  resourcePVs[i].prefix = -1;
                                  resourcePVs[i].prefixBackup = -1;
                                  resourcePVs[i].instantiationId = -1;
                                  resourcePVs[i].isRuntimeInstantiated = false;
                              }
      
                              this.RemoveInstantiationData(instantiationId);
      
                              // Send OnPhotonInstantiate callback to newly created GO.
                              // GO will be enabled when instantiated from Prefab and it does not matter if the script is enabled or disabled.
                              go.SendMessage(OnPhotonInstantiateString, new PhotonMessageInfo(photonPlayer, serverTime, null), SendMessageOptions.DontRequireReceiver);
                              if (callback != null)
                              {
                                  callback.Invoke(go);
                                  if (OnCustomInstantiated != null)
                                  {
                                      OnCustomInstantiated.Invoke(photonPlayer, go);
                                  }
                              }
                              return;
      
                          });
                      }
                      else
                      {
                          // now modify the loaded "blueprint" object before it becomes a part of the scene (by instantiating it)
                          PhotonView[] resourcePVs = resourceGameObject.GetPhotonViewsInChildren();
                          if (resourcePVs.Length != viewsIDs.Length)
                          {
                              throw new Exception("Error in Instantiation! The resource's PhotonView count is not the same as in incoming data.");
                          }
      
                          for (int i = 0; i < viewsIDs.Length; i++)
                          {
                              // NOTE instantiating the loaded resource will keep the viewID but would not copy instantiation data, so it's set below
                              // so we only set the viewID and instantiationId now. the instantiationData can be fetched
                              resourcePVs[i].viewID = viewsIDs[i];
                              resourcePVs[i].prefix = objLevelPrefix;
                              resourcePVs[i].instantiationId = instantiationId;
                              resourcePVs[i].isRuntimeInstantiated = true;
                          }
      
                          this.StoreInstantiationData(instantiationId, incomingInstantiationData);
      
                          // load the resource and set it's values before instantiating it:
                          GameObject go = (GameObject)GameObject.Instantiate(resourceGameObject, position, rotation);
      
                          for (int i = 0; i < viewsIDs.Length; i++)
                          {
                              // NOTE instantiating the loaded resource will keep the viewID but would not copy instantiation data, so it's set below
                              // so we only set the viewID and instantiationId now. the instantiationData can be fetched
                              resourcePVs[i].viewID = 0;
                              resourcePVs[i].prefix = -1;
                              resourcePVs[i].prefixBackup = -1;
                              resourcePVs[i].instantiationId = -1;
                              resourcePVs[i].isRuntimeInstantiated = false;
                          }
      
                          this.RemoveInstantiationData(instantiationId);
      
                          // Send OnPhotonInstantiate callback to newly created GO.
                          // GO will be enabled when instantiated from Prefab and it does not matter if the script is enabled or disabled.
                          go.SendMessage(OnPhotonInstantiateString, new PhotonMessageInfo(photonPlayer, serverTime, null), SendMessageOptions.DontRequireReceiver);
                          if (callback != null)
                          {
                              callback.Invoke(go);
                              if (OnCustomInstantiated != null)
                              {
                                  OnCustomInstantiated.Invoke(photonPlayer, go);
                              }
                          }
                          return;
                      }
                  }
              }
          }
      
          #endregion
      

        

      NetworkingPeer.cs 第2598行:

      屏蔽掉原有的 DoInstantiate 調(diào)用,改為 DoInstantiateCustom 調(diào)用

                      //this.DoInstantiate((Hashtable)photonEvent[ParameterCode.Data], originatingPlayer, null);
                      this.DoInstantiateCustom((Hashtable)photonEvent[ParameterCode.Data], originatingPlayer, null, (go) => {
                          SendMonoMessage(PhotonNetworkingMessage.OnPhotonInstantiate, new PhotonMessageInfo(originatingPlayer, 0, go.GetComponent<PhotonView>()));
                      });
      

      回調(diào)的意義在于你可以在初始Photon.MonoBehaviour中捕獲全局的OnPhotonInstantiate網(wǎng)絡(luò)物體初始回調(diào),同樣的你也可以通過OnCustomInstantiated事件來自定義回調(diào)事件行為

       

      之后記得調(diào)用 PhotonNetwork.RegisterCustomLoaderToPhoton 注冊一個自定義的默認(rèn)異步資源加載方法給 Photon 即可

       

      主站蜘蛛池模板: 亚洲中文久久久精品无码| 久久精品波多野结衣| 亚洲av永久无码精品天堂久久| 蜜桃AV抽搐高潮一区二区| 黄男女激情一区二区三区| 人妻系列无码专区免费| 国产玖玖视频| 亚洲欧美成人综合久久久| 久9视频这里只有精品试看| 日本亚洲一区二区精品| 亚洲成av一区二区三区| 国产69精品久久久久777| 中文字幕一区二区人妻电影| 国产精品久久久久久久网| 亚洲精品成人福利网站| 日韩一区二区三区亚洲一| 久久久无码精品午夜| 暖暖 免费 高清 日本 在线观看5 色老头亚洲成人免费影院 | 家居| 国语精品国内自产视频| 真实国产乱啪福利露脸| 成人亚洲欧美一区二区三区| 国产成人a在线观看视频免费| 免费吃奶摸下激烈视频| 亚洲中文字幕久久精品码| 一本大道久久东京热AV| 国产精品免费看久久久| 日韩精品毛片一区到三区| 国产成人精品亚洲午夜| 黑人异族巨大巨大巨粗| 国产精品一区二区性色av| 亚洲色一区二区三区四区| 亚洲国产成人精品区综合| 精品免费看国产一区二区| 午夜成人性爽爽免费视频| 国产网友愉拍精品视频手机| 亚洲2022国产成人精品无码区| 亚洲码亚洲码天堂码三区| 欧洲精品色在线观看| 中国china体内裑精亚洲日本 | 国产愉拍91九色国产愉拍|