ServerCleanInstantiateAndDestroy









How do I use Server Clean Instantiate And Destroy
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: NetworkingPeer.cs Copy
2493     protected internal void RemoveInstantiatedGO(GameObject go, bool localOnly)
2494     {
2495         if (go == null)
2496         {
2497             Debug.LogError("Failed to 'network-remove' GameObject because it's null.");
2498             return;
2499         }
2500
2501         // Don't remove the GO if it doesn't have any PhotonView
2502         PhotonView[] views = go.GetComponentsInChildren(true);
2503         if (views == null || views.Length <= 0)
2504         {
2505             Debug.LogError("Failed to 'network-remove' GameObject because has no PhotonView components: " + go);
2506             return;
2507         }
2508
2509         PhotonView viewZero = views[0];
2510         int creatorId = viewZero.CreatorActorNr; // creatorId of obj is needed to delete EvInstantiate (only if it's from that user)
2511         int instantiationId = viewZero.instantiationId; // actual, live InstantiationIds start with 1 and go up
2512
2513         // Don't remove GOs that are owned by others (unless this is the master and the remote player left)
2514         if (!localOnly)
2515         {
2516             if (!viewZero.isMine)
2517             {
2518                 Debug.LogError("Failed to 'network-remove' GameObject. Client is neither owner nor masterClient taking over for owner who left: " + viewZero);
2519                 return;
2520             }
2521
2522             // Don't remove the Instantiation from the server, if it doesn't have a proper ID
2523             if (instantiationId < 1)
2524             {
2525                 Debug.LogError("Failed to 'network-remove' GameObject because it is missing a valid InstantiationId on view: " + viewZero + ". Not Destroying GameObject or PhotonViews!");
2526                 return;
2527             }
2528         }
2529
2530
2531         // cleanup instantiation (event and local list)
2532         if (!localOnly)
2533         {
2534             this.ServerCleanInstantiateAndDestroy(instantiationId, creatorId, viewZero.isRuntimeInstantiated); // server cleaning
2535         }
2536
2537
2538         // cleanup PhotonViews and their RPCs events (if not localOnly)
2539         for (int j = views.Length - 1; j >= 0; j--)
2540         {
2541             PhotonView view = views[j];
2542             if (view == null)
2543             {
2544                 continue;
2545             }
2546
2547             // we only destroy/clean PhotonViews that were created by PhotonNetwork.Instantiate (and those have an instantiationId!)
2548             if (view.instantiationId >= 1)
2549             {
2550                 this.LocalCleanPhotonView(view);
2551             }
2552             if (!localOnly)
2553             {
2554                 this.OpCleanRpcBuffer(view);
2555             }
2556         }
2557
2558         if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
2559             Debug.Log("Network destroy Instantiated GO: " + go.name);
2560
2561         GameObject.Destroy(go);
2562     }
File name: NetworkingPeer.cs Copy
2592     private void ServerCleanInstantiateAndDestroy(int instantiateId, int creatorId, bool isRuntimeInstantiated)
2593     {
2594         Hashtable removeFilter = new Hashtable();
2595         removeFilter[(byte)7] = instantiateId;
2596
2597         RaiseEventOptions options = new RaiseEventOptions() { CachingOption = EventCaching.RemoveFromRoomCache, TargetActors = new int[] { creatorId } };
2598         this.OpRaiseEvent(PunEvent.Instantiation, removeFilter, true, options);
2599         //this.OpRaiseEvent(PunEvent.Instantiation, removeFilter, true, 0, new int[] { actorNr }, EventCaching.RemoveFromRoomCache);
2600
2601         Hashtable evData = new Hashtable();
2602         evData[(byte)0] = instantiateId;
2603         options = null;
2604         if (!isRuntimeInstantiated)
2605         {
2606             // if the view got loaded with the scene, the EvDestroy must be cached (there is no Instantiate-msg which we can remove)
2607             // reason: joining players will load the obj and have to destroy it (too)
2608             options = new RaiseEventOptions();
2609             options.CachingOption = EventCaching.AddToRoomCacheGlobal;
2610             Debug.Log("Destroying GO as global. ID: " + instantiateId);
2611         }
2612         this.OpRaiseEvent(PunEvent.Destroy, evData, true, options);
2613     }

ServerCleanInstantiateAndDestroy 170 lượt xem

Gõ tìm kiếm nhanh...