PickupItems









How do I use Pickup Items
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: PickupDemoGui.cs Copy
15     public void OnGUI()
16     {
17         if (!PhotonNetwork.inRoom)
18         {
19             return;
20         }
21
22
23         if (this.ShowScores)
24         {
25             GUILayout.Label("Your Score: " + PhotonNetwork.player.GetScore());
26         }
27
28
29         if (this.ShowDropButton)
30         {
31             foreach (PickupItem item in PickupItem.DisabledPickupItems)
32             {
33                 if (item.PickupIsMine && item.SecondsBeforeRespawn <= 0)
34                 {
35                     if (GUILayout.Button("Drop " + item.name))
36                     {
37                         item.Drop(); // drops the item at the place where it originates
38                     }
39
40                     GameObject playerCharGo = PhotonNetwork.player.TagObject as GameObject;
41                     if (playerCharGo != null && GUILayout.Button("Drop here " + item.name))
42                     {
43                         // drop the item at some other place. next to the user's character would be fine...
44                         Vector3 random = Random.insideUnitSphere;
45                         random.y = 0;
46                         random = random.normalized;
47                         Vector3 itempos = playerCharGo.transform.position + this.DropOffset * random;
48
49                         item.Drop(itempos);
50                     }
51                 }
52             }
53         }
54
55
56         if (this.ShowTeams)
57         {
58             foreach (var teamName in PunTeams.PlayersPerTeam.Keys)
59             {
60                 GUILayout.Label("Team: " + teamName.ToString());
61                 List teamPlayers = PunTeams.PlayersPerTeam[teamName];
62                 foreach (PhotonPlayer player in teamPlayers)
63                 {
64                     GUILayout.Label(" " + player.ToStringFull() + " Score: " + player.GetScore());
65                 }
66             }
67
68             if (GUILayout.Button("to red"))
69             {
70                 PhotonNetwork.player.SetTeam(PunTeams.Team.red);
71             }
72             if (GUILayout.Button("to blue"))
73             {
74                 PhotonNetwork.player.SetTeam(PunTeams.Team.blue);
75             }
76         }
77     }
File name: PickupItem.cs Copy
171     internal void PickedUp(float timeUntilRespawn)
172     {
173         // this script simply disables the GO for a while until it respawns.
174         this.gameObject.SetActive(false);
175         PickupItem.DisabledPickupItems.Add(this);
176         this.TimeOfRespawn = 0;
177
178         if (timeUntilRespawn > 0)
179         {
180             this.TimeOfRespawn = PhotonNetwork.time + timeUntilRespawn;
181             Invoke("PunRespawn", timeUntilRespawn);
182         }
183     }
File name: PickupItem.cs Copy
195     internal void PunRespawn()
196     {
197         #if DEBUG
198         // debugging: in some cases, the respawn is "late". it's unclear why! just be aware of this.
199         double timeDiffToRespawnTime = PhotonNetwork.time - this.TimeOfRespawn;
200         if (timeDiffToRespawnTime > 0.1f) Debug.LogWarning("Spawn time is wrong by: " + timeDiffToRespawnTime + " (this is not an error. you just need to be aware of this.)");
201         #endif
202
203
204         // if this is called from another thread, we might want to do this in OnEnable() instead of here (depends on Invoke's implementation)
205         PickupItem.DisabledPickupItems.Remove(this);
206         this.TimeOfRespawn = 0;
207         this.PickupIsMine = false;
208
209         if (this.gameObject != null)
210         {
211             this.gameObject.SetActive(true);
212         }
213     }
File name: PickupItemSyncer.cs Copy
24     public void OnJoinedRoom()
25     {
26         Debug.Log("Joined Room. isMasterClient: " + PhotonNetwork.isMasterClient + " id: " + PhotonNetwork.player.ID);
27         // this client joined the room. let's see if there are players and if someone has to inform us about pickups
28         this.IsWaitingForPickupInit = !PhotonNetwork.isMasterClient;
29
30         if (PhotonNetwork.playerList.Length >= 2)
31         {
32             this.Invoke("AskForPickupItemSpawnTimes", 2.0f);
33         }
34     }
File name: PickupItemSyncer.cs Copy
37     public void AskForPickupItemSpawnTimes()
38     {
39         if (this.IsWaitingForPickupInit)
40         {
41             if (PhotonNetwork.playerList.Length < 2)
42             {
43                 Debug.Log("Cant ask anyone else for PickupItem spawn times.");
44                 this.IsWaitingForPickupInit = false;
45                 return;
46             }
47
48
49             // find a another player (than the master, who likely is gone) to ask for the PickupItem spawn times
50             PhotonPlayer nextPlayer = PhotonNetwork.masterClient.GetNext();
51             if (nextPlayer == null || nextPlayer.Equals(PhotonNetwork.player))
52             {
53                 nextPlayer = PhotonNetwork.player.GetNext();
54                 //Debug.Log("This player is the Master's next. Asking this client's 'next' player: " + ((nextPlayer != null) ? nextPlayer.ToStringFull() : ""));
55             }
56
57             if (nextPlayer != null && !nextPlayer.Equals(PhotonNetwork.player))
58             {
59                 this.photonView.RPC("RequestForPickupTimes", nextPlayer);
60
61                 // you could restart this invoke and try to find another player after 4 seconds. but after a while it doesnt make a difference anymore
62                 //this.Invoke("AskForPickupItemSpawnTimes", 2.0f);
63             }
64             else
65             {
66                 Debug.Log("No player left to ask");
67                 this.IsWaitingForPickupInit = false;
68             }
69         }
70     }
File name: PickupItemSyncer.cs Copy
73     public void RequestForPickupTimes(PhotonMessageInfo msgInfo)
74     {
75         if (msgInfo.sender == null)
76         {
77             Debug.LogError("Unknown player asked for PickupItems");
78             return;
79         }
80
81         SendPickedUpItems(msgInfo.sender);
82     }
File name: PickupItemSyncer.cs Copy
87     private void SendPickedUpItems(PhotonPlayer targtePlayer)
88     {
89         if (targtePlayer == null)
90         {
91             Debug.LogWarning("Cant send PickupItem spawn times to unknown targetPlayer.");
92             return;
93         }
94
95         double now = PhotonNetwork.time;
96         double soon = now + TimeDeltaToIgnore;
97
98
99         PickupItem[] items = new PickupItem[PickupItem.DisabledPickupItems.Count];
100         PickupItem.DisabledPickupItems.CopyTo(items);
101
102         List valuesToSend = new List(items.Length * 2);
103         for (int i = 0; i < items.Length; i++)
104         {
105             PickupItem pi = items[i];
106             if (pi.SecondsBeforeRespawn <= 0)
107             {
108                 valuesToSend.Add(pi.ViewID);
109                 valuesToSend.Add((float)0.0f);
110             }
111             else
112             {
113                 double timeUntilRespawn = pi.TimeOfRespawn - PhotonNetwork.time;
114                 if (pi.TimeOfRespawn > soon)
115                 {
116                     // the respawn of this item is not "immediately", so we include it in the message "these items are not active" for the new player
117                     Debug.Log(pi.ViewID + " respawn: " + pi.TimeOfRespawn + " timeUntilRespawn: " + timeUntilRespawn + " (now: " + PhotonNetwork.time + ")");
118                     valuesToSend.Add(pi.ViewID);
119                     valuesToSend.Add((float)timeUntilRespawn);
120                 }
121             }
122         }
123
124         Debug.Log("Sent count: " + valuesToSend.Count + " now: " + now);
125         this.photonView.RPC("PickupItemInit", targtePlayer, PhotonNetwork.time, valuesToSend.ToArray());
126     }

Download file with original file name:PickupItems

PickupItems 108 lượt xem

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