Destroying
How do I use Destroying
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
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 }
File name: NetworkingPeer.cs
Copy
2688 public void RegisterPhotonView(PhotonView netView)
2689 {
2690 if (!Application.isPlaying)
2691 {
2692 this.photonViewList = new Dictionary
2693 return;
2694 }
2695
2696 if (netView.viewID == 0)
2697 {
2698 // don't register views with ID 0 (not initialized). they register when a ID is assigned later on
2699 Debug.Log("PhotonView register is ignored, because viewID is 0. No id assigned yet to: " + netView);
2700 return;
2701 }
2702
2703 if (this.photonViewList.ContainsKey(netView.viewID))
2704 {
2705 // if some other view is in the list already, we got a problem. it might be undestructible. print out error
2706 if (netView != photonViewList[netView.viewID])
2707 {
2708 Debug.LogError(string.Format("PhotonView ID duplicate found: {0}. New: {1} old: {2}. Maybe one wasn't destroyed on scene load?! Check for 'DontDestroyOnLoad'. Destroying old entry, adding new.", netView.viewID, netView, photonViewList[netView.viewID]));
2709 }
2710
2711 //this.photonViewList.Remove(netView.viewID); // TODO check if we chould Destroy the GO of this view?!
2712 this.RemoveInstantiatedGO(photonViewList[netView.viewID].gameObject, true);
2713 }
2714
2715 // Debug.Log("adding view to known list: " + netView);
2716 this.photonViewList.Add(netView.viewID, netView);
2717 //Debug.LogError("view being added. " + netView); // Exit Games internal log
2718
2719 if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
2720 Debug.Log("Registered PhotonView: " + netView.viewID);
2721 }
Destroying 213 lượt xem
Gõ tìm kiếm nhanh...