PickupIsMine









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

Featured Snippets


File name: OnPickedUpScript.cs Copy
6  public void OnPickedUp(PickupItem item)
7  {
8      if (item.PickupIsMine)
9      {
10          Debug.Log("I picked up something. That's a score!");
11          PhotonNetwork.player.AddScore(1);
12      }
13      else
14      {
15             Debug.Log("Someone else picked up something. Lucky!");
16      }
17  }
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
105     public void Drop()
106     {
107         if (this.PickupIsMine)
108         {
109             this.photonView.RPC("PunRespawn", PhotonTargets.AllViaServer);
110         }
111     }
File name: PickupItem.cs Copy
114     public void Drop(Vector3 newPosition)
115     {
116         if (this.PickupIsMine)
117         {
118             this.photonView.RPC("PunRespawn", PhotonTargets.AllViaServer, newPosition);
119         }
120     }
File name: PickupItem.cs Copy
124     public void PunPickup(PhotonMessageInfo msgInfo)
125     {
126         // when this client's RPC gets executed, this client no longer waits for a sent pickup and can try again
127         if (msgInfo.sender.isLocal) this.SentPickup = false;
128
129
130         // In this solution, picked up items are disabled. They can't be picked up again this way, etc.
131         // You could check "active" first, if you're not interested in failed pickup-attempts.
132         if (!this.gameObject.GetActive())
133         {
134             // optional logging:
135             Debug.Log("Ignored PU RPC, cause item is inactive. " + this.gameObject + " SecondsBeforeRespawn: " + SecondsBeforeRespawn + " TimeOfRespawn: " + this.TimeOfRespawn + " respawn in future: " + (TimeOfRespawn > PhotonNetwork.time));
136             return; // makes this RPC being ignored
137         }
138
139
140         // if the RPC isn't ignored by now, this is a successful pickup. this might be "my" pickup and we should do a callback
141         this.PickupIsMine = msgInfo.sender.isLocal;
142
143         // call the method OnPickedUp(PickupItem item) if a GameObject was defined as callback target
144         if (this.OnPickedUpCall != null)
145         {
146             // you could also skip callbacks for items that are not picked up by this client by using: if (this.PickupIsMine)
147             this.OnPickedUpCall.SendMessage("OnPickedUp", this);
148         }
149
150
151         // setup a respawn (or none, if the item has to be dropped)
152         if (SecondsBeforeRespawn <= 0)
153         {
154             this.PickedUp(0.0f); // item doesn't auto-respawn. must be dropped
155         }
156         else
157         {
158             // how long it is until this item respanws, depends on the pickup time and the respawn time
159             double timeSinceRpcCall = (PhotonNetwork.time - msgInfo.timestamp);
160             double timeUntilRespawn = SecondsBeforeRespawn - timeSinceRpcCall;
161
162             //Debug.Log("msg timestamp: " + msgInfo.timestamp + " time until respawn: " + timeUntilRespawn);
163
164             if (timeUntilRespawn > 0)
165             {
166                 this.PickedUp((float)timeUntilRespawn);
167             }
168         }
169     }
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     }

PickupIsMine 111 lượt xem

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