質問編集履歴
1
詳細やスクリプトを記述
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,6 +6,422 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
+
ちなみに精査記しているゲームは3Dオンライン対戦ゲームで、戦車を操作して弾を発射して戦うゲームです
|
10
|
+
|
11
|
+
|
12
|
+
|
9
13
|
|
10
14
|
|
11
15
|
いろいろと試しましたがどうしても相手の名前を取得できなかったので質問させていただきました。
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
### 試したこと
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
CNameInputField.csでPhotonNetwork.NickNameを設定
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
PlayerID.csでキャラクターをルームにInstantiateし、そのInstantiateしたキャラクターに自分のPhotonNetwork.NickNameを入れる(PhotonNetwork.NickNameはキャラクターにアタッチしているNickName.csに格納する)
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
ゲーム終了後にSyouhai.csでシーン上に残っているキャラクターをFindGameObjectWithTagで検索し、GetComponent<NickName>();でプレイヤーのNickNameをパネルに表示
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
という流れで試してみましたが相手が勝利者の場合相手の名前が表示されませんでした。
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
※流れを分かりやすく
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
CNameInputField.csでプレイヤーの名前(PhotonNetwork.NickName)を設定
|
44
|
+
|
45
|
+
↓
|
46
|
+
|
47
|
+
PlayerID.csでキャラクターを生成、そのキャラクターのNickName.csに名前(PhotonNetwork.NickName)を入れる
|
48
|
+
|
49
|
+
↓
|
50
|
+
|
51
|
+
対戦終了後Syouhai.csが作動し、シーンに残っているキャラクターの名前をTextに入れる
|
52
|
+
|
53
|
+
そのTextを全員に表示する
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
### CNameInputField.cs
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
```C#
|
66
|
+
|
67
|
+
using UnityEngine;
|
68
|
+
|
69
|
+
using UnityEngine.UI;
|
70
|
+
|
71
|
+
using System.Collections;
|
72
|
+
|
73
|
+
using Photon.Pun;
|
74
|
+
|
75
|
+
using Photon.Realtime;
|
76
|
+
|
77
|
+
public class CNameInputField : MonoBehaviourPunCallbacks
|
78
|
+
|
79
|
+
{
|
80
|
+
|
81
|
+
#region Private変数定義
|
82
|
+
|
83
|
+
static string playerNamePrefKey = "PlayerName";
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
InputField _inputField;
|
88
|
+
|
89
|
+
#endregion
|
90
|
+
|
91
|
+
#region MonoBehaviourコールバック
|
92
|
+
|
93
|
+
void Start()
|
94
|
+
|
95
|
+
{
|
96
|
+
|
97
|
+
string defaultName = "";
|
98
|
+
|
99
|
+
_inputField = this.GetComponent<InputField>();
|
100
|
+
|
101
|
+
//前回プレイ開始時に入力した名前をロードして表示
|
102
|
+
|
103
|
+
if (_inputField != null)
|
104
|
+
|
105
|
+
{
|
106
|
+
|
107
|
+
if (PlayerPrefs.HasKey(playerNamePrefKey))
|
108
|
+
|
109
|
+
{
|
110
|
+
|
111
|
+
defaultName = PlayerPrefs.GetString(playerNamePrefKey);
|
112
|
+
|
113
|
+
_inputField.text = defaultName;
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
#endregion
|
122
|
+
|
123
|
+
#region Public Method
|
124
|
+
|
125
|
+
public void SetPlayerName()
|
126
|
+
|
127
|
+
{
|
128
|
+
|
129
|
+
PhotonNetwork.NickName = _inputField.text + " ";//今回ゲームで利用するプレイヤーの名前を設定
|
130
|
+
|
131
|
+
PlayerPrefs.SetString(playerNamePrefKey, _inputField.text);//今回の名前をセーブ
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
Debug.Log(PhotonNetwork.NickName);
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
#endregion
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
```
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
### PlayerID.cs
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
```C#
|
152
|
+
|
153
|
+
using System;
|
154
|
+
|
155
|
+
using System.Collections;
|
156
|
+
|
157
|
+
using UnityEngine;
|
158
|
+
|
159
|
+
using UnityEngine.SceneManagement;
|
160
|
+
|
161
|
+
using Photon.Pun;
|
162
|
+
|
163
|
+
using Photon.Realtime;
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
public class PlayerID : MonoBehaviourPunCallbacks
|
168
|
+
|
169
|
+
{
|
170
|
+
|
171
|
+
public FixedJoystick joystick;
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
public GameObject gameplayer1;
|
176
|
+
|
177
|
+
public GameObject gameplayer2;
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
int id;
|
182
|
+
|
183
|
+
NickName script;
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
void Awake()
|
188
|
+
|
189
|
+
{
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
//PhotonNetwork.IsMessageQueueRunning = true;
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
PhotonNetwork.AutomaticallySyncScene = false;
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
id = PhotonNetwork.LocalPlayer.ActorNumber;
|
204
|
+
|
205
|
+
var v = new Vector3(-5.5f, 0.5f, -3.5f);
|
206
|
+
|
207
|
+
var v2 = new Vector3(5.5f, 0.5f, 3.5f);
|
208
|
+
|
209
|
+
//var v3 = new Vector3(-5.5f, 0.5f, 3.5f);
|
210
|
+
|
211
|
+
//var v4 = new Vector3(5.5f, 0.5f, -3.5f);
|
212
|
+
|
213
|
+
if (!PhotonNetwork.IsConnected) //Phootnに接続されていなければ
|
214
|
+
|
215
|
+
{
|
216
|
+
|
217
|
+
SceneManager.LoadScene("MultiMode"); //ログイン画面に戻る
|
218
|
+
|
219
|
+
return;
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
if (id == 1)
|
224
|
+
|
225
|
+
{
|
226
|
+
|
227
|
+
Debug.Log("ID = " + id);
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
GameObject Player1 = PhotonNetwork.Instantiate(this.gameplayer1.name, v, Quaternion.identity);
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
script = Player1.GetComponent<NickName>();
|
236
|
+
|
237
|
+
script.PhotonName = PhotonNetwork.NickName;
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
Debug.Log("script.PhotonName" + script.PhotonName);
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
else if (id == 2)
|
252
|
+
|
253
|
+
{
|
254
|
+
|
255
|
+
Debug.Log("ID = " + id);
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
GameObject Player2 = PhotonNetwork.Instantiate(this.gameplayer2.name, v2, Quaternion.identity);
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
script = Player2.GetComponent<NickName>();
|
264
|
+
|
265
|
+
script.PhotonName = PhotonNetwork.NickName;
|
266
|
+
|
267
|
+
Debug.Log("script.PhotonName" + script.PhotonName);
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
```
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
### Syouhai.cs
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
```C#
|
296
|
+
|
297
|
+
using System.Collections;
|
298
|
+
|
299
|
+
using System.Collections.Generic;
|
300
|
+
|
301
|
+
using UnityEngine;
|
302
|
+
|
303
|
+
using Photon.Pun;
|
304
|
+
|
305
|
+
using UnityEngine.UI;
|
306
|
+
|
307
|
+
using Photon.Realtime;
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
public class Syouhai : MonoBehaviourPunCallbacks
|
312
|
+
|
313
|
+
{
|
314
|
+
|
315
|
+
public GameObject winPanel;
|
316
|
+
|
317
|
+
public Text winText;
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
GameObject wintank;
|
322
|
+
|
323
|
+
public string winname;
|
324
|
+
|
325
|
+
NickName script;
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
void Start()
|
330
|
+
|
331
|
+
{
|
332
|
+
|
333
|
+
winPanel.SetActive(false);
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
public void ActivePanel()
|
340
|
+
|
341
|
+
{
|
342
|
+
|
343
|
+
wintank = GameObject.FindGameObjectWithTag("tank");
|
344
|
+
|
345
|
+
Debug.Log("wintank = " + wintank);
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
script = wintank.GetComponent<NickName>();
|
350
|
+
|
351
|
+
Debug.Log("script =" + script);
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
winname = script.PhotonName;
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
Debug.Log(winname + "のかち");
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
winText.text = winname + "のかち";
|
364
|
+
|
365
|
+
winPanel.SetActive(true);
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
}
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
```
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
### NickName.cs
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
```C#
|
386
|
+
|
387
|
+
using System.Collections;
|
388
|
+
|
389
|
+
using System.Collections.Generic;
|
390
|
+
|
391
|
+
using UnityEngine;
|
392
|
+
|
393
|
+
using Photon.Pun;
|
394
|
+
|
395
|
+
using Photon.Realtime;
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
public class NickName : MonoBehaviourPunCallbacks
|
400
|
+
|
401
|
+
{
|
402
|
+
|
403
|
+
public string PhotonName;
|
404
|
+
|
405
|
+
}
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
```
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
### 発生している問題
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
勝利者が相手の場合、相手の名前が表示されない
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
### 補足情報(FW/ツールのバージョンなど)
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
Unity 2019 4.11f1
|