質問編集履歴
2
スクリプトファイルの更新
test
CHANGED
File without changes
|
test
CHANGED
@@ -36,6 +36,8 @@
|
|
36
36
|
|
37
37
|
### スクリプト
|
38
38
|
|
39
|
+
文字制限があるため、怪しいファイルのみ転載してます。
|
40
|
+
|
39
41
|
|
40
42
|
|
41
43
|
GameManager
|
@@ -477,3 +479,185 @@
|
|
477
479
|
}
|
478
480
|
|
479
481
|
```
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
Wall
|
486
|
+
|
487
|
+
```C#
|
488
|
+
|
489
|
+
using System.Collections;
|
490
|
+
|
491
|
+
using System.Collections.Generic;
|
492
|
+
|
493
|
+
using UnityEngine;
|
494
|
+
|
495
|
+
public class Wall : MonoBehaviour
|
496
|
+
|
497
|
+
{
|
498
|
+
|
499
|
+
[SerializeField]
|
500
|
+
|
501
|
+
float speed = 10.0f;
|
502
|
+
|
503
|
+
[SerializeField]
|
504
|
+
|
505
|
+
float minScaleX = 4.0f;
|
506
|
+
|
507
|
+
[SerializeField]
|
508
|
+
|
509
|
+
float minScaleY = 3.0f;
|
510
|
+
|
511
|
+
[SerializeField]
|
512
|
+
|
513
|
+
float maxScaleX = 6.0f;
|
514
|
+
|
515
|
+
[SerializeField]
|
516
|
+
|
517
|
+
float maxScaleY = 7.0f;
|
518
|
+
|
519
|
+
bool inCamera = false;
|
520
|
+
|
521
|
+
Rigidbody2D rigidBody2D;
|
522
|
+
|
523
|
+
//初期化
|
524
|
+
|
525
|
+
void Start()
|
526
|
+
|
527
|
+
{
|
528
|
+
|
529
|
+
//コンポーネントを取得
|
530
|
+
|
531
|
+
rigidBody2D = GetComponent<Rigidbody2D>();
|
532
|
+
|
533
|
+
//壁の長さをランダムに変更
|
534
|
+
|
535
|
+
float scaleX = Random.Range(minScaleX, maxScaleX);
|
536
|
+
|
537
|
+
float scaleY = Random.Range(minScaleY, maxScaleY);
|
538
|
+
|
539
|
+
transform.localScale = new Vector3(scaleX, scaleY, transform.localScale.z);
|
540
|
+
|
541
|
+
}
|
542
|
+
|
543
|
+
//物理演算
|
544
|
+
|
545
|
+
void FixedUpdate()
|
546
|
+
|
547
|
+
{
|
548
|
+
|
549
|
+
rigidBody2D.velocity = Vector2.left * speed;
|
550
|
+
|
551
|
+
}
|
552
|
+
|
553
|
+
//消去処理
|
554
|
+
|
555
|
+
public void DestroyBlock()
|
556
|
+
|
557
|
+
{
|
558
|
+
|
559
|
+
Destroy(gameObject);
|
560
|
+
|
561
|
+
}
|
562
|
+
|
563
|
+
//カメラ内外の判定処理
|
564
|
+
|
565
|
+
void OnBecameVisible()
|
566
|
+
|
567
|
+
{
|
568
|
+
|
569
|
+
inCamera = true;
|
570
|
+
|
571
|
+
}
|
572
|
+
|
573
|
+
void OnBecameInvisible()
|
574
|
+
|
575
|
+
{
|
576
|
+
|
577
|
+
//一度画面内に入って、また出たときに消去する
|
578
|
+
|
579
|
+
if (inCamera)
|
580
|
+
|
581
|
+
{
|
582
|
+
|
583
|
+
DestroyBlock();
|
584
|
+
|
585
|
+
}
|
586
|
+
|
587
|
+
}
|
588
|
+
|
589
|
+
}
|
590
|
+
|
591
|
+
```
|
592
|
+
|
593
|
+
|
594
|
+
|
595
|
+
Coin
|
596
|
+
|
597
|
+
```C#
|
598
|
+
|
599
|
+
using System.Collections;
|
600
|
+
|
601
|
+
using System.Collections.Generic;
|
602
|
+
|
603
|
+
using UnityEngine;
|
604
|
+
|
605
|
+
public class Coin : MonoBehaviour
|
606
|
+
|
607
|
+
{
|
608
|
+
|
609
|
+
[SerializeField]
|
610
|
+
|
611
|
+
float speed = -10.0f;
|
612
|
+
|
613
|
+
Rigidbody2D rigidBody2D;
|
614
|
+
|
615
|
+
bool inCamera = false;
|
616
|
+
|
617
|
+
//初期化
|
618
|
+
|
619
|
+
void Start()
|
620
|
+
|
621
|
+
{
|
622
|
+
|
623
|
+
rigidBody2D = GetComponent<Rigidbody2D>();
|
624
|
+
|
625
|
+
}
|
626
|
+
|
627
|
+
//物理演算
|
628
|
+
|
629
|
+
void FixedUpdate()
|
630
|
+
|
631
|
+
{
|
632
|
+
|
633
|
+
rigidBody2D.velocity = new Vector2(speed, rigidBody2D.velocity.y); ;
|
634
|
+
|
635
|
+
}
|
636
|
+
|
637
|
+
//カメラ内外の判定処理
|
638
|
+
|
639
|
+
void OnBecameVisible()
|
640
|
+
|
641
|
+
{
|
642
|
+
|
643
|
+
inCamera = true;
|
644
|
+
|
645
|
+
}
|
646
|
+
|
647
|
+
void OnBecameInvisible()
|
648
|
+
|
649
|
+
{
|
650
|
+
|
651
|
+
if (inCamera)
|
652
|
+
|
653
|
+
{
|
654
|
+
|
655
|
+
Destroy(gameObject, 1.0f);
|
656
|
+
|
657
|
+
}
|
658
|
+
|
659
|
+
}
|
660
|
+
|
661
|
+
}
|
662
|
+
|
663
|
+
```
|
1
スクリプトファイル内容変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -38,7 +38,7 @@
|
|
38
38
|
|
39
39
|
|
40
40
|
|
41
|
-
|
41
|
+
GameManager
|
42
42
|
|
43
43
|
```C#
|
44
44
|
|
@@ -48,640 +48,432 @@
|
|
48
48
|
|
49
49
|
using UnityEngine;
|
50
50
|
|
51
|
+
using UnityEngine.UI;
|
52
|
+
|
51
|
-
public class
|
53
|
+
public class GameManager : MonoBehaviour
|
52
54
|
|
53
55
|
{
|
54
56
|
|
55
|
-
//インスペクタに表示
|
56
|
-
|
57
|
-
[Serialize
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
[Serialize
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
[Serialize
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
i
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
57
|
+
//インスペクタに表示しないpublic変数
|
58
|
+
|
59
|
+
[System.NonSerialized]
|
60
|
+
|
61
|
+
public int level = 1;
|
62
|
+
|
63
|
+
[System.NonSerialized]
|
64
|
+
|
65
|
+
public int score = 0;
|
66
|
+
|
67
|
+
[System.NonSerialized]
|
68
|
+
|
69
|
+
public bool playing = false;
|
70
|
+
|
71
|
+
//インスペクタに表示するprivate変数
|
72
|
+
|
73
|
+
[SerializeField]
|
74
|
+
|
75
|
+
GameObject player;
|
76
|
+
|
77
|
+
[SerializeField]
|
78
|
+
|
79
|
+
int levelUpTime = 6; //レベルアップするまでの時間
|
80
|
+
|
81
|
+
[SerializeField]
|
82
|
+
|
83
|
+
int maxLevel = 10;
|
84
|
+
|
85
|
+
[SerializeField]
|
86
|
+
|
87
|
+
float addScoreInterval = 6; //スコア加算間隔
|
88
|
+
|
89
|
+
[SerializeField]
|
90
|
+
|
91
|
+
int addScoreCofficient = 1; //スコア加算係数
|
92
|
+
|
93
|
+
[SerializeField]
|
94
|
+
|
95
|
+
float playerSpawnX = -7.0f;
|
96
|
+
|
97
|
+
[SerializeField]
|
98
|
+
|
99
|
+
float playerSpawnY = 3.0f;
|
100
|
+
|
101
|
+
//インスペクタに表示しない変数
|
102
|
+
|
103
|
+
int highScore = 0;
|
104
|
+
|
105
|
+
string highScoreKey = "highScore";
|
106
|
+
|
107
|
+
//UI取得用
|
108
|
+
|
109
|
+
[SerializeField]
|
110
|
+
|
111
|
+
Image titleLogoImg;
|
112
|
+
|
113
|
+
[SerializeField]
|
114
|
+
|
115
|
+
Text clickStartTxt;
|
116
|
+
|
117
|
+
[SerializeField]
|
118
|
+
|
119
|
+
Text gameOverTxt;
|
120
|
+
|
121
|
+
[SerializeField]
|
122
|
+
|
123
|
+
Text tryAgainTxt;
|
124
|
+
|
125
|
+
[SerializeField]
|
126
|
+
|
127
|
+
Text scoreLabelTxt;
|
128
|
+
|
129
|
+
[SerializeField]
|
130
|
+
|
131
|
+
Text scoreNumTxt;
|
132
|
+
|
133
|
+
[SerializeField]
|
134
|
+
|
135
|
+
Text highLabelTxt;
|
136
|
+
|
137
|
+
[SerializeField]
|
138
|
+
|
139
|
+
Text highNumTxt;
|
140
|
+
|
141
|
+
//初期化
|
142
|
+
|
143
|
+
void Start()
|
144
|
+
|
145
|
+
{
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
//毎フレームの処理
|
154
|
+
|
155
|
+
void Update()
|
156
|
+
|
157
|
+
{
|
158
|
+
|
159
|
+
//ゲーム中の処理
|
160
|
+
|
161
|
+
if (playing)
|
162
|
+
|
163
|
+
{
|
164
|
+
|
165
|
+
if (highScore < score)
|
166
|
+
|
167
|
+
{
|
168
|
+
|
169
|
+
highScore = score;
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
//スコアを表示
|
174
|
+
|
175
|
+
scoreNumTxt.text = score.ToString("d6");
|
176
|
+
|
177
|
+
highNumTxt.text = highScore.ToString("d6");
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
//タイトルまたはゲームオーバー
|
182
|
+
|
183
|
+
else
|
184
|
+
|
185
|
+
{
|
186
|
+
|
187
|
+
//クリックされたらゲーム開始
|
188
|
+
|
189
|
+
if (Input.GetMouseButtonDown(0))
|
190
|
+
|
191
|
+
{
|
192
|
+
|
193
|
+
StartGame();
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
//ゲーム開始処理
|
202
|
+
|
203
|
+
void StartGame()
|
204
|
+
|
205
|
+
{
|
206
|
+
|
207
|
+
//ガベージコレクション実行
|
208
|
+
|
209
|
+
System.GC.Collect();
|
210
|
+
|
211
|
+
//初期化
|
212
|
+
|
213
|
+
score = 0;
|
214
|
+
|
215
|
+
level = 1;
|
216
|
+
|
217
|
+
//ハイスコア読み込み
|
218
|
+
|
219
|
+
highScore = PlayerPrefs.GetInt(highScoreKey, 0);
|
220
|
+
|
221
|
+
//タイトルまたはゲームオーバー画面非表示
|
222
|
+
|
223
|
+
titleLogoImg.enabled = false;
|
224
|
+
|
225
|
+
clickStartTxt.enabled = false;
|
226
|
+
|
227
|
+
gameOverTxt.enabled = false;
|
228
|
+
|
229
|
+
tryAgainTxt.enabled = false;
|
230
|
+
|
231
|
+
//スコアを表示
|
232
|
+
|
233
|
+
scoreLabelTxt.enabled = true;
|
234
|
+
|
235
|
+
scoreNumTxt.enabled = true;
|
236
|
+
|
237
|
+
highLabelTxt.enabled = true;
|
238
|
+
|
239
|
+
highNumTxt.enabled = true;
|
240
|
+
|
241
|
+
playing = true;
|
242
|
+
|
243
|
+
//もし地形やコインが残ってたら消去
|
244
|
+
|
245
|
+
DeleteObjects("Wall");
|
246
|
+
|
247
|
+
DeleteObjects("Item");
|
248
|
+
|
249
|
+
//プレイヤー生成
|
250
|
+
|
251
|
+
Instantiate(player, new Vector3(playerSpawnX, playerSpawnY, 0.0f), Quaternion.identity);
|
252
|
+
|
253
|
+
//ゲーム開始後、一定時間ごとの処理
|
254
|
+
|
255
|
+
InvokeRepeating("LevelUp", levelUpTime, levelUpTime);
|
256
|
+
|
257
|
+
InvokeRepeating("AddScoreByTime", addScoreInterval, addScoreInterval);
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
//ゲームオーバー処理
|
262
|
+
|
263
|
+
public void GameOver()
|
264
|
+
|
265
|
+
{
|
266
|
+
|
267
|
+
CancelInvoke();
|
268
|
+
|
269
|
+
playing = false;
|
270
|
+
|
271
|
+
// ハイスコアを保存
|
272
|
+
|
273
|
+
PlayerPrefs.SetInt(highScoreKey, highScore);
|
274
|
+
|
275
|
+
PlayerPrefs.Save();
|
276
|
+
|
277
|
+
//ゲームオーバー画面表示
|
278
|
+
|
279
|
+
gameOverTxt.enabled = true;
|
280
|
+
|
281
|
+
tryAgainTxt.enabled = true;
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
//スコア加算
|
286
|
+
|
287
|
+
void AddScoreByTime()
|
288
|
+
|
289
|
+
{
|
290
|
+
|
291
|
+
score += addScoreCofficient * (10 + level - 1);
|
292
|
+
|
293
|
+
Debug.Log(score);
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
//レベルアップ
|
298
|
+
|
299
|
+
void LevelUp()
|
300
|
+
|
301
|
+
{
|
302
|
+
|
303
|
+
if (level < maxLevel)
|
304
|
+
|
305
|
+
{
|
306
|
+
|
307
|
+
level++;
|
308
|
+
|
309
|
+
Debug.Log(level);
|
310
|
+
|
311
|
+
}
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
//ゲームオブジェクトの一括削除
|
316
|
+
|
317
|
+
void DeleteObjects(string tag)
|
318
|
+
|
319
|
+
{
|
320
|
+
|
321
|
+
GameObject[] deleteObjects = GameObject.FindGameObjectsWithTag(tag);
|
322
|
+
|
323
|
+
if (deleteObjects.Length > 0)
|
324
|
+
|
325
|
+
{
|
326
|
+
|
327
|
+
foreach (GameObject obj in deleteObjects)
|
328
|
+
|
329
|
+
{
|
330
|
+
|
331
|
+
Destroy(obj);
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
```
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
Emitter
|
346
|
+
|
347
|
+
```C#
|
348
|
+
|
349
|
+
using System.Collections;
|
350
|
+
|
351
|
+
using System.Collections.Generic;
|
352
|
+
|
353
|
+
using UnityEngine;
|
354
|
+
|
355
|
+
public class Emitter : MonoBehaviour
|
356
|
+
|
357
|
+
{
|
358
|
+
|
359
|
+
[SerializeField]
|
360
|
+
|
361
|
+
float emitWaitTimeMin = 0.6f;
|
362
|
+
|
363
|
+
[SerializeField]
|
364
|
+
|
365
|
+
float emitWaitTimeMax = 1.0f;
|
366
|
+
|
367
|
+
[SerializeField]
|
368
|
+
|
369
|
+
float intervalCofficient = 0.98f; //レベルアップしたとき、生成間隔を何倍するかの係数
|
370
|
+
|
371
|
+
[SerializeField]
|
372
|
+
|
373
|
+
float wallSpawnX = 13.0f;
|
374
|
+
|
375
|
+
[SerializeField]
|
376
|
+
|
377
|
+
float wallSpawnY = -2.5f;
|
378
|
+
|
379
|
+
[SerializeField]
|
380
|
+
|
381
|
+
float coinSpawnX = 11.0f;
|
382
|
+
|
383
|
+
[SerializeField]
|
384
|
+
|
385
|
+
float coinSpawnY = 5.0f;
|
386
|
+
|
387
|
+
[SerializeField]
|
388
|
+
|
389
|
+
GameObject wall;
|
390
|
+
|
391
|
+
[SerializeField]
|
392
|
+
|
393
|
+
GameObject coin;
|
394
|
+
|
395
|
+
[SerializeField]
|
86
396
|
|
87
397
|
GameManager gameManager;
|
88
398
|
|
399
|
+
bool emitterEnabled = true;
|
400
|
+
|
89
|
-
|
401
|
+
int cullentLevel;
|
90
|
-
|
91
|
-
|
402
|
+
|
92
|
-
|
93
|
-
{
|
94
|
-
|
95
|
-
//コンポーネントを取得
|
96
|
-
|
97
|
-
|
403
|
+
float emitWaitTimeMinNow;
|
98
|
-
|
404
|
+
|
99
|
-
|
405
|
+
float emitWaitTimeMaxNow;
|
100
|
-
|
101
|
-
|
406
|
+
|
102
|
-
|
103
|
-
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
|
104
|
-
|
105
|
-
}
|
106
|
-
|
107
|
-
//毎フレームの処理
|
407
|
+
//毎フレームの処理
|
108
408
|
|
109
409
|
void Update()
|
110
410
|
|
111
411
|
{
|
112
412
|
|
113
|
-
//接地チェック
|
114
|
-
|
115
|
-
//GroundCheckオブジェクトに床などが重なってるならtrue
|
116
|
-
|
117
|
-
grounded = (Physics2D.OverlapPoint(groundCheck.position) != null) ? true : false;
|
118
|
-
|
119
|
-
//
|
413
|
+
//タイトルまたはゲームオーバー画面
|
120
|
-
|
414
|
+
|
121
|
-
if (gr
|
415
|
+
if (!gameManager.playing)
|
122
|
-
|
416
|
+
|
123
|
-
{
|
417
|
+
{
|
418
|
+
|
124
|
-
|
419
|
+
//初期化
|
420
|
+
|
421
|
+
cullentLevel = gameManager.level;
|
422
|
+
|
423
|
+
emitWaitTimeMinNow = emitWaitTimeMin;
|
424
|
+
|
425
|
+
emitWaitTimeMaxNow = emitWaitTimeMax;
|
426
|
+
|
427
|
+
}
|
428
|
+
|
429
|
+
//プレイ中の処理
|
430
|
+
|
431
|
+
if (emitterEnabled && gameManager.playing)
|
432
|
+
|
433
|
+
{
|
434
|
+
|
435
|
+
//生成処理
|
436
|
+
|
437
|
+
Instantiate(wall, new Vector3(wallSpawnX, wallSpawnY, transform.position.z), Quaternion.identity);
|
438
|
+
|
439
|
+
Instantiate(coin, new Vector3(coinSpawnX, coinSpawnY, transform.position.z), Quaternion.identity);
|
440
|
+
|
441
|
+
StartCoroutine(EmitWait());
|
442
|
+
|
125
|
-
//
|
443
|
+
//レベルアップ時に生成速度Up
|
126
|
-
|
444
|
+
|
127
|
-
if (
|
445
|
+
if (gameManager.level != cullentLevel)
|
128
446
|
|
129
447
|
{
|
130
448
|
|
449
|
+
emitWaitTimeMinNow *= intervalCofficient;
|
450
|
+
|
451
|
+
emitWaitTimeMaxNow *= intervalCofficient;
|
452
|
+
|
131
|
-
|
453
|
+
cullentLevel = gameManager.level;
|
132
454
|
|
133
455
|
}
|
134
456
|
|
135
457
|
}
|
136
458
|
|
459
|
+
}
|
460
|
+
|
137
|
-
|
461
|
+
//生成を待つ処理
|
138
|
-
|
462
|
+
|
139
|
-
|
463
|
+
IEnumerator EmitWait()
|
140
|
-
|
464
|
+
|
141
|
-
|
465
|
+
{
|
142
|
-
|
143
|
-
|
466
|
+
|
144
|
-
|
145
|
-
//ジャンプ終了(前フレームで接地してなくて、今のフレームで接地したとき)
|
146
|
-
|
147
|
-
if (!groundedPrev & grounded)
|
148
|
-
|
149
|
-
{
|
150
|
-
|
151
|
-
|
467
|
+
emitterEnabled = false;
|
152
|
-
|
153
|
-
|
468
|
+
|
154
|
-
|
155
|
-
}
|
156
|
-
|
157
|
-
else
|
158
|
-
|
159
|
-
{
|
160
|
-
|
161
|
-
animator.SetTrigger("Dash");
|
162
|
-
|
163
|
-
}
|
164
|
-
|
165
|
-
//このフレームの接地状態を保存
|
166
|
-
|
167
|
-
groundedPrev = grounded ? true : false;
|
168
|
-
|
169
|
-
}
|
170
|
-
|
171
|
-
//毎フレームの処理(物理演算)
|
172
|
-
|
173
|
-
void FixedUpdate()
|
174
|
-
|
175
|
-
{
|
176
|
-
|
177
|
-
if (grounded)
|
178
|
-
|
179
|
-
{
|
180
|
-
|
181
|
-
|
469
|
+
float emitWaitTime = Random.Range(emitWaitTimeMinNow, emitWaitTimeMaxNow);
|
182
|
-
|
183
|
-
|
470
|
+
|
184
|
-
|
185
|
-
}
|
186
|
-
|
187
|
-
//ジャンプ
|
188
|
-
|
189
|
-
void Jump()
|
190
|
-
|
191
|
-
{
|
192
|
-
|
193
|
-
jumped = true;
|
194
|
-
|
195
|
-
|
471
|
+
yield return new WaitForSeconds(emitWaitTime);
|
196
|
-
|
197
|
-
|
472
|
+
|
198
|
-
|
199
|
-
//死亡処理
|
200
|
-
|
201
|
-
public void Dead()
|
202
|
-
|
203
|
-
{
|
204
|
-
|
205
|
-
gameManager.GameOver();
|
206
|
-
|
207
|
-
rigidBody2D.Sleep();
|
208
|
-
|
209
|
-
Destroy(gameObject);
|
210
|
-
|
211
|
-
}
|
212
|
-
|
213
|
-
//アイテムと衝突した時の処理
|
214
|
-
|
215
|
-
void OnCollisionEnter2D(Collision2D other)
|
216
|
-
|
217
|
-
{
|
218
|
-
|
219
|
-
if (other.gameObject.tag == "Item")
|
220
|
-
|
221
|
-
{
|
222
|
-
|
223
|
-
//スコア加算
|
224
|
-
|
225
|
-
gameManager.score += itemScore;
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
//取ったアイテムを消す
|
230
|
-
|
231
|
-
Destroy(other.gameObject);
|
232
|
-
|
233
|
-
}
|
234
|
-
|
235
|
-
}
|
236
|
-
|
237
|
-
//カメラ内外の判定処理
|
238
|
-
|
239
|
-
void OnBecameVisible()
|
240
|
-
|
241
|
-
{
|
242
|
-
|
243
|
-
in
|
473
|
+
emitterEnabled = true;
|
244
|
-
|
245
|
-
}
|
246
|
-
|
247
|
-
void OnBecameInvisible()
|
248
|
-
|
249
|
-
{
|
250
|
-
|
251
|
-
if (inCamera)
|
252
|
-
|
253
|
-
{
|
254
|
-
|
255
|
-
Dead();
|
256
|
-
|
257
|
-
}
|
258
474
|
|
259
475
|
}
|
260
476
|
|
261
477
|
}
|
262
478
|
|
263
479
|
```
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
GameManager
|
268
|
-
|
269
|
-
```C#
|
270
|
-
|
271
|
-
using System.Collections;
|
272
|
-
|
273
|
-
using System.Collections.Generic;
|
274
|
-
|
275
|
-
using UnityEngine;
|
276
|
-
|
277
|
-
using UnityEngine.UI;
|
278
|
-
|
279
|
-
public class GameManager : MonoBehaviour
|
280
|
-
|
281
|
-
{
|
282
|
-
|
283
|
-
//インスペクタに表示しないpublic変数
|
284
|
-
|
285
|
-
[System.NonSerialized]
|
286
|
-
|
287
|
-
public int level = 1;
|
288
|
-
|
289
|
-
[System.NonSerialized]
|
290
|
-
|
291
|
-
public int score = 0;
|
292
|
-
|
293
|
-
[System.NonSerialized]
|
294
|
-
|
295
|
-
public bool playing = false;
|
296
|
-
|
297
|
-
//インスペクタに表示するprivate変数
|
298
|
-
|
299
|
-
[SerializeField]
|
300
|
-
|
301
|
-
GameObject player;
|
302
|
-
|
303
|
-
[SerializeField]
|
304
|
-
|
305
|
-
int levelUpTime = 6; //レベルアップするまでの時間
|
306
|
-
|
307
|
-
[SerializeField]
|
308
|
-
|
309
|
-
int maxLevel = 10;
|
310
|
-
|
311
|
-
[SerializeField]
|
312
|
-
|
313
|
-
float addScoreInterval = 6; //スコア加算間隔
|
314
|
-
|
315
|
-
[SerializeField]
|
316
|
-
|
317
|
-
int addScoreCofficient = 1; //スコア加算係数
|
318
|
-
|
319
|
-
[SerializeField]
|
320
|
-
|
321
|
-
float playerSpawnX = -7.0f;
|
322
|
-
|
323
|
-
[SerializeField]
|
324
|
-
|
325
|
-
float playerSpawnY = 3.0f;
|
326
|
-
|
327
|
-
//インスペクタに表示しない変数
|
328
|
-
|
329
|
-
int highScore = 0;
|
330
|
-
|
331
|
-
string highScoreKey = "highScore";
|
332
|
-
|
333
|
-
//UI取得用
|
334
|
-
|
335
|
-
[SerializeField]
|
336
|
-
|
337
|
-
Image titleLogoImg;
|
338
|
-
|
339
|
-
[SerializeField]
|
340
|
-
|
341
|
-
Text clickStartTxt;
|
342
|
-
|
343
|
-
[SerializeField]
|
344
|
-
|
345
|
-
Text gameOverTxt;
|
346
|
-
|
347
|
-
[SerializeField]
|
348
|
-
|
349
|
-
Text tryAgainTxt;
|
350
|
-
|
351
|
-
[SerializeField]
|
352
|
-
|
353
|
-
Text scoreLabelTxt;
|
354
|
-
|
355
|
-
[SerializeField]
|
356
|
-
|
357
|
-
Text scoreNumTxt;
|
358
|
-
|
359
|
-
[SerializeField]
|
360
|
-
|
361
|
-
Text highLabelTxt;
|
362
|
-
|
363
|
-
[SerializeField]
|
364
|
-
|
365
|
-
Text highNumTxt;
|
366
|
-
|
367
|
-
//初期化
|
368
|
-
|
369
|
-
void Start()
|
370
|
-
|
371
|
-
{
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
}
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
//毎フレームの処理
|
380
|
-
|
381
|
-
void Update()
|
382
|
-
|
383
|
-
{
|
384
|
-
|
385
|
-
//ゲーム中の処理
|
386
|
-
|
387
|
-
if (playing)
|
388
|
-
|
389
|
-
{
|
390
|
-
|
391
|
-
if (highScore < score)
|
392
|
-
|
393
|
-
{
|
394
|
-
|
395
|
-
highScore = score;
|
396
|
-
|
397
|
-
}
|
398
|
-
|
399
|
-
//スコアを表示
|
400
|
-
|
401
|
-
scoreNumTxt.text = score.ToString("d6");
|
402
|
-
|
403
|
-
highNumTxt.text = highScore.ToString("d6");
|
404
|
-
|
405
|
-
}
|
406
|
-
|
407
|
-
//タイトルまたはゲームオーバー
|
408
|
-
|
409
|
-
else
|
410
|
-
|
411
|
-
{
|
412
|
-
|
413
|
-
//クリックされたらゲーム開始
|
414
|
-
|
415
|
-
if (Input.GetMouseButtonDown(0))
|
416
|
-
|
417
|
-
{
|
418
|
-
|
419
|
-
StartGame();
|
420
|
-
|
421
|
-
}
|
422
|
-
|
423
|
-
}
|
424
|
-
|
425
|
-
}
|
426
|
-
|
427
|
-
//ゲーム開始処理
|
428
|
-
|
429
|
-
void StartGame()
|
430
|
-
|
431
|
-
{
|
432
|
-
|
433
|
-
//ガベージコレクション実行
|
434
|
-
|
435
|
-
System.GC.Collect();
|
436
|
-
|
437
|
-
//初期化
|
438
|
-
|
439
|
-
score = 0;
|
440
|
-
|
441
|
-
level = 1;
|
442
|
-
|
443
|
-
//ハイスコア読み込み
|
444
|
-
|
445
|
-
highScore = PlayerPrefs.GetInt(highScoreKey, 0);
|
446
|
-
|
447
|
-
//タイトルまたはゲームオーバー画面非表示
|
448
|
-
|
449
|
-
titleLogoImg.enabled = false;
|
450
|
-
|
451
|
-
clickStartTxt.enabled = false;
|
452
|
-
|
453
|
-
gameOverTxt.enabled = false;
|
454
|
-
|
455
|
-
tryAgainTxt.enabled = false;
|
456
|
-
|
457
|
-
//スコアを表示
|
458
|
-
|
459
|
-
scoreLabelTxt.enabled = true;
|
460
|
-
|
461
|
-
scoreNumTxt.enabled = true;
|
462
|
-
|
463
|
-
highLabelTxt.enabled = true;
|
464
|
-
|
465
|
-
highNumTxt.enabled = true;
|
466
|
-
|
467
|
-
playing = true;
|
468
|
-
|
469
|
-
//もし地形やコインが残ってたら消去
|
470
|
-
|
471
|
-
DeleteObjects("Wall");
|
472
|
-
|
473
|
-
DeleteObjects("Item");
|
474
|
-
|
475
|
-
//プレイヤー生成
|
476
|
-
|
477
|
-
Instantiate(player, new Vector3(playerSpawnX, playerSpawnY, 0.0f), Quaternion.identity);
|
478
|
-
|
479
|
-
//ゲーム開始後、一定時間ごとの処理
|
480
|
-
|
481
|
-
InvokeRepeating("LevelUp", levelUpTime, levelUpTime);
|
482
|
-
|
483
|
-
InvokeRepeating("AddScoreByTime", addScoreInterval, addScoreInterval);
|
484
|
-
|
485
|
-
}
|
486
|
-
|
487
|
-
//ゲームオーバー処理
|
488
|
-
|
489
|
-
public void GameOver()
|
490
|
-
|
491
|
-
{
|
492
|
-
|
493
|
-
CancelInvoke();
|
494
|
-
|
495
|
-
playing = false;
|
496
|
-
|
497
|
-
// ハイスコアを保存
|
498
|
-
|
499
|
-
PlayerPrefs.SetInt(highScoreKey, highScore);
|
500
|
-
|
501
|
-
PlayerPrefs.Save();
|
502
|
-
|
503
|
-
//ゲームオーバー画面表示
|
504
|
-
|
505
|
-
gameOverTxt.enabled = true;
|
506
|
-
|
507
|
-
tryAgainTxt.enabled = true;
|
508
|
-
|
509
|
-
}
|
510
|
-
|
511
|
-
//スコア加算
|
512
|
-
|
513
|
-
void AddScoreByTime()
|
514
|
-
|
515
|
-
{
|
516
|
-
|
517
|
-
score += addScoreCofficient * (10 + level - 1);
|
518
|
-
|
519
|
-
Debug.Log(score);
|
520
|
-
|
521
|
-
}
|
522
|
-
|
523
|
-
//レベルアップ
|
524
|
-
|
525
|
-
void LevelUp()
|
526
|
-
|
527
|
-
{
|
528
|
-
|
529
|
-
if (level < maxLevel)
|
530
|
-
|
531
|
-
{
|
532
|
-
|
533
|
-
level++;
|
534
|
-
|
535
|
-
Debug.Log(level);
|
536
|
-
|
537
|
-
}
|
538
|
-
|
539
|
-
}
|
540
|
-
|
541
|
-
//ゲームオブジェクトの一括削除
|
542
|
-
|
543
|
-
void DeleteObjects(string tag)
|
544
|
-
|
545
|
-
{
|
546
|
-
|
547
|
-
GameObject[] deleteObjects = GameObject.FindGameObjectsWithTag(tag);
|
548
|
-
|
549
|
-
if (deleteObjects.Length > 0)
|
550
|
-
|
551
|
-
{
|
552
|
-
|
553
|
-
foreach (GameObject obj in deleteObjects)
|
554
|
-
|
555
|
-
{
|
556
|
-
|
557
|
-
Destroy(obj);
|
558
|
-
|
559
|
-
}
|
560
|
-
|
561
|
-
}
|
562
|
-
|
563
|
-
}
|
564
|
-
|
565
|
-
}
|
566
|
-
|
567
|
-
```
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
Wall
|
572
|
-
|
573
|
-
```C#
|
574
|
-
|
575
|
-
using System.Collections;
|
576
|
-
|
577
|
-
using System.Collections.Generic;
|
578
|
-
|
579
|
-
using UnityEngine;
|
580
|
-
|
581
|
-
public class Wall : MonoBehaviour
|
582
|
-
|
583
|
-
{
|
584
|
-
|
585
|
-
[SerializeField]
|
586
|
-
|
587
|
-
float speed = 10.0f;
|
588
|
-
|
589
|
-
[SerializeField]
|
590
|
-
|
591
|
-
float minScaleX = 4.0f;
|
592
|
-
|
593
|
-
[SerializeField]
|
594
|
-
|
595
|
-
float minScaleY = 3.0f;
|
596
|
-
|
597
|
-
[SerializeField]
|
598
|
-
|
599
|
-
float maxScaleX = 6.0f;
|
600
|
-
|
601
|
-
[SerializeField]
|
602
|
-
|
603
|
-
float maxScaleY = 7.0f;
|
604
|
-
|
605
|
-
bool inCamera = false;
|
606
|
-
|
607
|
-
Rigidbody2D rigidBody2D;
|
608
|
-
|
609
|
-
//初期化
|
610
|
-
|
611
|
-
void Start()
|
612
|
-
|
613
|
-
{
|
614
|
-
|
615
|
-
//コンポーネントを取得
|
616
|
-
|
617
|
-
rigidBody2D = GetComponent<Rigidbody2D>();
|
618
|
-
|
619
|
-
//壁の長さをランダムに変更
|
620
|
-
|
621
|
-
float scaleX = Random.Range(minScaleX, maxScaleX);
|
622
|
-
|
623
|
-
float scaleY = Random.Range(minScaleY, maxScaleY);
|
624
|
-
|
625
|
-
transform.localScale = new Vector3(scaleX, scaleY, transform.localScale.z);
|
626
|
-
|
627
|
-
}
|
628
|
-
|
629
|
-
//物理演算
|
630
|
-
|
631
|
-
void FixedUpdate()
|
632
|
-
|
633
|
-
{
|
634
|
-
|
635
|
-
rigidBody2D.velocity = Vector2.left * speed;
|
636
|
-
|
637
|
-
}
|
638
|
-
|
639
|
-
//消去処理
|
640
|
-
|
641
|
-
public void DestroyBlock()
|
642
|
-
|
643
|
-
{
|
644
|
-
|
645
|
-
Destroy(gameObject);
|
646
|
-
|
647
|
-
}
|
648
|
-
|
649
|
-
//カメラ内外の判定処理
|
650
|
-
|
651
|
-
void OnBecameVisible()
|
652
|
-
|
653
|
-
{
|
654
|
-
|
655
|
-
inCamera = true;
|
656
|
-
|
657
|
-
}
|
658
|
-
|
659
|
-
void OnBecameInvisible()
|
660
|
-
|
661
|
-
{
|
662
|
-
|
663
|
-
//一度画面内に入って、また出たときに消去する
|
664
|
-
|
665
|
-
if (inCamera)
|
666
|
-
|
667
|
-
{
|
668
|
-
|
669
|
-
DestroyBlock();
|
670
|
-
|
671
|
-
}
|
672
|
-
|
673
|
-
}
|
674
|
-
|
675
|
-
}
|
676
|
-
|
677
|
-
```
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
Emitter
|
682
|
-
|
683
|
-
```C
|
684
|
-
|
685
|
-
コード
|
686
|
-
|
687
|
-
```
|