Spawn
How do I use Spawn
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
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: OnJoinedInstantiate.cs
Copy
10 public void OnJoinedRoom()
11 {
12 if (this.PrefabsToInstantiate != null)
13 {
14 foreach (GameObject o in this.PrefabsToInstantiate)
15 {
16 Debug.Log("Instantiating: " + o.name);
17
18 Vector3 spawnPos = Vector3.up;
19 if (this.SpawnPosition != null)
20 {
21 spawnPos = this.SpawnPosition.position;
22 }
23
24 Vector3 random = Random.insideUnitSphere;
25 random.y = 0;
26 random = random.normalized;
27 Vector3 itempos = spawnPos + this.PositionOffset * random;
28
29 PhotonNetwork.Instantiate(o.name, itempos, Quaternion.identity, 0);
30 }
31 }
32 }
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
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
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
187 internal void PunRespawn(Vector3 pos)
188 {
189 Debug.Log("PunRespawn with Position.");
190 this.PunRespawn();
191 this.gameObject.transform.position = pos;
192 }
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: 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 }
Download file with original file name:Spawn
Spawn 141 lượt xem
Gõ tìm kiếm nhanh...