質問編集履歴

1

修正

2021/08/23 01:52

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- Unity ブロックをクリックしたらアニメーションをさせたいです
1
+ Unity ブロックをクリック
test CHANGED
@@ -3,543 +3,3 @@
3
3
  ---
4
4
 
5
5
  Spriteのブロックをクリックしたら落下アニメーションをさせたいです。
6
-
7
- 現状は、
8
-
9
- ・StageManager.csでBlockプレハブ(Sprite)を使って配列を作成して、ブロックをクリックするとStageManager / ClickedBlock関数のDebug.Log("ClickedBlock");が実行される状態なのですが、落下アニメーションをそこで実行しても何故か配列が作成されなくなってしまい、わからなくなってしまいました。エラー文は特に出ていません。
10
-
11
- ・BlocksController / ClearBlock()関数の部分でDestroy(this.gameObject)をアニメーションに変えてみましたがこちらも上記と同様の結果でした。
12
-
13
- どのようなスクリプトを書いたら良いでしょうか?
14
-
15
-
16
-
17
-
18
-
19
- ブロックを押したら...
20
-
21
- ![イメージ説明](2b2bb259e5dc50cb60b71d721279c0b9.jpeg)
22
-
23
- 落下しながら落ちて消える
24
-
25
- ![イメージ説明](76e599e1425030c407262c6f9c098c8b.jpeg)
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
- ↓ブロックに付けているスクリプト
34
-
35
- ```C#:BlocksController
36
-
37
- using System;
38
-
39
- using System.Collections;
40
-
41
- using System.Collections.Generic;
42
-
43
- using UnityEngine;
44
-
45
-
46
-
47
- public enum BlockType
48
-
49
- {
50
-
51
- DEATH,
52
-
53
- ALIVE,
54
-
55
- }
56
-
57
-
58
-
59
- public class BlocksController : MonoBehaviour
60
-
61
- {
62
-
63
- public BlockType type;
64
-
65
- public Sprite deathSprite;
66
-
67
- public Sprite aliveSprite;
68
-
69
-
70
-
71
- SpriteRenderer spriteRenderer;
72
-
73
-
74
-
75
- StageManager stageManager;
76
-
77
- Vector3Int intPosition;
78
-
79
-
80
-
81
- void Awake()
82
-
83
- {
84
-
85
- spriteRenderer = GetComponent<SpriteRenderer>();
86
-
87
- }
88
-
89
-
90
-
91
- public void Init(BlockType blockType, Vector3Int position, StageManager stageManager)
92
-
93
- {
94
-
95
- intPosition = position;
96
-
97
- this.stageManager = stageManager;
98
-
99
- SetType(type);
100
-
101
- }
102
-
103
-
104
-
105
- void SetType(BlockType blockType)
106
-
107
- {
108
-
109
- type = blockType;
110
-
111
- SetSprite(type);
112
-
113
- }
114
-
115
-
116
-
117
- void SetSprite(BlockType type)
118
-
119
- {
120
-
121
- if (type == BlockType.DEATH)
122
-
123
- {
124
-
125
- spriteRenderer.sprite = deathSprite;
126
-
127
- }
128
-
129
- else if (type == BlockType.ALIVE)
130
-
131
- {
132
-
133
- spriteRenderer.sprite = aliveSprite;
134
-
135
- }
136
-
137
- }
138
-
139
-
140
-
141
- //クリックしたら実行
142
-
143
- public void OnBlock()
144
-
145
- {
146
-
147
- ClearBlock();
148
-
149
- stageManager.ClickedBlock(intPosition);
150
-
151
- }
152
-
153
-
154
-
155
- public void ClearBlock()
156
-
157
- {
158
-
159
- if (type == BlockType.DEATH)
160
-
161
- {
162
-
163
- SetType(BlockType.ALIVE);
164
-
165
- }
166
-
167
- else if (type == BlockType.ALIVE)
168
-
169
- {
170
-
171
- SetType(BlockType.DEATH);
172
-
173
- Destroy(this.gameObject); //ブロックを消す
174
-
175
- }
176
-
177
- }
178
-
179
- }
180
-
181
- ```
182
-
183
-
184
-
185
-
186
-
187
- ↓ブロックの配列やクリック処理をしているスクリプト
188
-
189
- ```C#:StageManager
190
-
191
- using System.Collections;
192
-
193
- using System.Collections.Generic;
194
-
195
- using UnityEngine;
196
-
197
- using UnityEngine.SceneManagement;
198
-
199
-
200
-
201
- public class StageManager : MonoBehaviour
202
-
203
- {
204
-
205
- public Animator openAnimator;
206
-
207
- public Animator openStopAnimator;
208
-
209
-
210
-
211
- public GameObject clearImage;
212
-
213
- public GameObject stopImage;
214
-
215
- public GameObject okButton;
216
-
217
- public GameObject reloadButton;
218
-
219
- public GameObject handOpenSprite;
220
-
221
-
222
-
223
- public TextAsset stageFile1;
224
-
225
- public TextAsset stageFile2;
226
-
227
- BlockType[,] blockTable;
228
-
229
- BlockType[,] blockTable2;
230
-
231
-
232
-
233
- BlocksController[,] blockTableobj;
234
-
235
- BlocksController[,] blockTableobj2;
236
-
237
-
238
-
239
- public BlocksController blockPrefab;
240
-
241
-
242
-
243
- void Start()
244
-
245
- {
246
-
247
- LoadStageFromText();
248
-
249
- DebugTable();
250
-
251
- CreateStage();
252
-
253
- openAnimator = GetComponent<Animator>();
254
-
255
- }
256
-
257
-
258
-
259
- void CreateStage()
260
-
261
- {
262
-
263
- Vector3 halfSize = default;
264
-
265
- float blockSize = blockPrefab.GetComponent<SpriteRenderer>().bounds.size.x;
266
-
267
- halfSize.x = blockSize * (blockTable.GetLength(0) / 2);
268
-
269
- halfSize.y = blockSize * (blockTable.GetLength(1) / 2);
270
-
271
-
272
-
273
- for (int y = 0; y < blockTable.GetLength(1); y++)
274
-
275
- {
276
-
277
- for (int x = 0; x < blockTable.GetLength(0); x++)
278
-
279
- {
280
-
281
- Vector3Int position = new Vector3Int(x, y, 0);
282
-
283
- BlocksController block = Instantiate(blockPrefab);
284
-
285
- block.Init(blockTable[x,y], position, this);
286
-
287
- Vector3 setPosition = (Vector3)position * blockSize - halfSize;
288
-
289
- setPosition.y *= (float)-1.04;
290
-
291
- setPosition.x *= (float)-1.04;
292
-
293
- block.transform.position = setPosition;
294
-
295
- blockTableobj[x, y] = block;
296
-
297
- }
298
-
299
- }
300
-
301
- reloadButton.SetActive(true);
302
-
303
- }
304
-
305
-
306
-
307
- public void LoadStageFromText()
308
-
309
- {
310
-
311
- string[] lines = stageFile1.text.Split(new[] { '\n', '\r' }, System.StringSplitOptions.RemoveEmptyEntries);
312
-
313
- int columns = 10;
314
-
315
- int rows = 5;
316
-
317
-
318
-
319
- blockTable = new BlockType[rows, columns];
320
-
321
- blockTableobj = new BlocksController[rows, columns];
322
-
323
- for (int y = 0; y < columns; y++)
324
-
325
- {
326
-
327
- string[] values = lines[y].Split(new[] { ',' });
328
-
329
- for (int x = 0; x < rows; x++)
330
-
331
- {
332
-
333
- if (values[x] == "0")
334
-
335
- {
336
-
337
- blockTable[x, y] = BlockType.DEATH;
338
-
339
- }
340
-
341
- if (values[x] == "1")
342
-
343
- {
344
-
345
- blockTable[x, y] = BlockType.ALIVE;
346
-
347
- }
348
-
349
- }
350
-
351
- }
352
-
353
- }
354
-
355
-
356
-
357
- public void ClearStageText()
358
-
359
- {
360
-
361
- string[] lines = stageFile2.text.Split(new[] { '\n', '\r' }, System.StringSplitOptions.RemoveEmptyEntries);
362
-
363
- int columns = 10;
364
-
365
- int rows = 5;
366
-
367
-
368
-
369
- blockTable2 = new BlockType[rows, columns];
370
-
371
- blockTableobj2 = new BlocksController[rows, columns];
372
-
373
- for (int y = 0; y < columns; y++)
374
-
375
- {
376
-
377
- string[] values = lines[y].Split(new[] { ',' });
378
-
379
- for (int x = 0; x < rows; x++)
380
-
381
- {
382
-
383
- if (values[x] == "0")
384
-
385
- {
386
-
387
- blockTable2[x, y] = BlockType.DEATH;
388
-
389
- }
390
-
391
- if (values[x] == "1")
392
-
393
- {
394
-
395
- blockTable2[x, y] = BlockType.ALIVE;
396
-
397
- }
398
-
399
- }
400
-
401
- }
402
-
403
- }
404
-
405
-
406
-
407
- public void ClickedBlock(Vector3Int center)
408
-
409
- {
410
-
411
- Debug.Log("ClickedBlock");
412
-
413
- }
414
-
415
-
416
-
417
- public void IsClear()
418
-
419
- {
420
-
421
- ClearStageText();
422
-
423
- bool isSuccess = true;
424
-
425
- for (int y = 0; y < blockTable2.GetLength(1); y++)
426
-
427
- {
428
-
429
- for (int x = 0; x < blockTable2.GetLength(0); x++)
430
-
431
- {
432
-
433
- if (blockTableobj[x,y].type != blockTable2[x,y]) isSuccess = false;
434
-
435
- }
436
-
437
- }
438
-
439
-
440
-
441
- if (isSuccess)
442
-
443
- {
444
-
445
- var clones = GameObject.FindGameObjectsWithTag("Yellow");
446
-
447
- foreach (var clone in clones)
448
-
449
- {
450
-
451
- Destroy(clone);
452
-
453
- }
454
-
455
- clearImage.SetActive(true);
456
-
457
- stopImage.SetActive(true);
458
-
459
- openAnimator.SetTrigger("OpenAni");
460
-
461
- openStopAnimator.SetTrigger("OpenAni2");
462
-
463
- Destroy(okButton.gameObject);
464
-
465
- Invoke("HandOpenMove", 1.2f);
466
-
467
- Debug.Log("正解");
468
-
469
-
470
-
471
- }
472
-
473
- else
474
-
475
- {
476
-
477
- //CreateStage();
478
-
479
- //openAnimator.SetTrigger("Open");
480
-
481
- Debug.Log("不正解");
482
-
483
- }
484
-
485
- }
486
-
487
-
488
-
489
- public void HandOpenMove()
490
-
491
- {
492
-
493
- handOpenSprite.SetActive(true);
494
-
495
- }
496
-
497
-
498
-
499
- public void Reload()
500
-
501
- {
502
-
503
- SceneManager.LoadScene("Level1");
504
-
505
- }
506
-
507
-
508
-
509
- public void NextScene()
510
-
511
- {
512
-
513
- SceneManager.LoadScene("Level2");
514
-
515
- }
516
-
517
-
518
-
519
- void DebugTable()
520
-
521
- {
522
-
523
- for (int y = 0; y < 10; y++)
524
-
525
- {
526
-
527
- string debugText = "";
528
-
529
- for (int x = 0; x < 5; x++)
530
-
531
- {
532
-
533
- debugText += blockTable[x, y] + ",";
534
-
535
- }
536
-
537
- Debug.Log(debugText);
538
-
539
- }
540
-
541
- }
542
-
543
- }
544
-
545
- ```