SecondsBeforeRespawn









How do I use Seconds Before Respawn
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
74     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
75     {
76         // read the description in SecondsBeforeRespawn
77
78         if (stream.isWriting && SecondsBeforeRespawn <= 0)
79         {
80             stream.SendNext(this.gameObject.transform.position);
81         }
82         else
83         {
84             // this will directly apply the last received position for this PickupItem. No smoothing. Usually not needed though.
85             Vector3 lastIncomingPos = (Vector3)stream.ReceiveNext();
86             this.gameObject.transform.position = lastIncomingPos;
87         }
88     }
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: PickupItemSimple.cs Copy
40     public void PunPickupSimple(PhotonMessageInfo msgInfo)
41     {
42         // one of the messages might be ours
43         // note: you could check "active" first, if you're not interested in your own, failed pickup-attempts.
44         if (this.SentPickup && msgInfo.sender.isLocal)
45         {
46             if (this.gameObject.GetActive())
47             {
48                 // picked up! yay.
49             }
50             else
51             {
52                 // pickup failed. too late (compared to others)
53             }
54         }
55
56         this.SentPickup = false;
57
58         if (!this.gameObject.GetActive())
59         {
60             Debug.Log("Ignored PU RPC, cause item is inactive. " + this.gameObject);
61             return;
62         }
63
64
65         // how long it is until this item respanws, depends on the pickup time and the respawn time
66         double timeSinceRpcCall = (PhotonNetwork.time - msgInfo.timestamp);
67         float timeUntilRespawn = SecondsBeforeRespawn - (float)timeSinceRpcCall;
68         //Debug.Log("msg timestamp: " + msgInfo.timestamp + " time until respawn: " + timeUntilRespawn);
69
70         if (timeUntilRespawn > 0)
71         {
72             // this script simply disables the GO for a while until it respawns.
73             this.gameObject.SetActive(false);
74             Invoke("RespawnAfter", timeUntilRespawn);
75         }
76     }
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     }
File name: PickupItemSyncer.cs Copy
130     public void PickupItemInit(double timeBase, float[] inactivePickupsAndTimes)
131     {
132         this.IsWaitingForPickupInit = false;
133
134         // if there are no inactive pickups, the sender will send a list of 0 items. this is not a problem...
135         for (int i = 0; i < inactivePickupsAndTimes.Length / 2; i++)
136         {
137             int arrayIndex = i*2;
138             int viewIdOfPickup = (int)inactivePickupsAndTimes[arrayIndex];
139             float timeUntilRespawnBasedOnTimeBase = inactivePickupsAndTimes[arrayIndex + 1];
140
141
142             PhotonView view = PhotonView.Find(viewIdOfPickup);
143             PickupItem pi = view.GetComponent();
144
145             if (timeUntilRespawnBasedOnTimeBase <= 0)
146             {
147                 pi.PickedUp(0.0f);
148             }
149             else
150             {
151                 double timeOfRespawn = timeUntilRespawnBasedOnTimeBase + timeBase;
152
153                 Debug.Log(view.viewID + " respawn: " + timeOfRespawn + " timeUntilRespawnBasedOnTimeBase:" + timeUntilRespawnBasedOnTimeBase + " SecondsBeforeRespawn: " + pi.SecondsBeforeRespawn);
154                 double timeBeforeRespawn = timeOfRespawn - PhotonNetwork.time;
155                 if (timeUntilRespawnBasedOnTimeBase <= 0)
156                 {
157                     timeBeforeRespawn = 0.0f;
158                 }
159
160                 pi.PickedUp((float) timeBeforeRespawn);
161             }
162         }
163     }

SecondsBeforeRespawn 103 lượt xem

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