質問編集履歴
1
修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
Unity ブロックをクリック
|
1
|
+
Unity ブロックをクリック
|
body
CHANGED
@@ -1,273 +1,3 @@
|
|
1
1
|
実装したいこと
|
2
2
|
---
|
3
|
-
Spriteのブロックをクリックしたら落下アニメーションをさせたいです。
|
3
|
+
Spriteのブロックをクリックしたら落下アニメーションをさせたいです。
|
4
|
-
現状は、
|
5
|
-
・StageManager.csでBlockプレハブ(Sprite)を使って配列を作成して、ブロックをクリックするとStageManager / ClickedBlock関数のDebug.Log("ClickedBlock");が実行される状態なのですが、落下アニメーションをそこで実行しても何故か配列が作成されなくなってしまい、わからなくなってしまいました。エラー文は特に出ていません。
|
6
|
-
・BlocksController / ClearBlock()関数の部分でDestroy(this.gameObject)をアニメーションに変えてみましたがこちらも上記と同様の結果でした。
|
7
|
-
どのようなスクリプトを書いたら良いでしょうか?
|
8
|
-
|
9
|
-
|
10
|
-
ブロックを押したら...
|
11
|
-

|
12
|
-
落下しながら落ちて消える
|
13
|
-

|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
↓ブロックに付けているスクリプト
|
18
|
-
```C#:BlocksController
|
19
|
-
using System;
|
20
|
-
using System.Collections;
|
21
|
-
using System.Collections.Generic;
|
22
|
-
using UnityEngine;
|
23
|
-
|
24
|
-
public enum BlockType
|
25
|
-
{
|
26
|
-
DEATH,
|
27
|
-
ALIVE,
|
28
|
-
}
|
29
|
-
|
30
|
-
public class BlocksController : MonoBehaviour
|
31
|
-
{
|
32
|
-
public BlockType type;
|
33
|
-
public Sprite deathSprite;
|
34
|
-
public Sprite aliveSprite;
|
35
|
-
|
36
|
-
SpriteRenderer spriteRenderer;
|
37
|
-
|
38
|
-
StageManager stageManager;
|
39
|
-
Vector3Int intPosition;
|
40
|
-
|
41
|
-
void Awake()
|
42
|
-
{
|
43
|
-
spriteRenderer = GetComponent<SpriteRenderer>();
|
44
|
-
}
|
45
|
-
|
46
|
-
public void Init(BlockType blockType, Vector3Int position, StageManager stageManager)
|
47
|
-
{
|
48
|
-
intPosition = position;
|
49
|
-
this.stageManager = stageManager;
|
50
|
-
SetType(type);
|
51
|
-
}
|
52
|
-
|
53
|
-
void SetType(BlockType blockType)
|
54
|
-
{
|
55
|
-
type = blockType;
|
56
|
-
SetSprite(type);
|
57
|
-
}
|
58
|
-
|
59
|
-
void SetSprite(BlockType type)
|
60
|
-
{
|
61
|
-
if (type == BlockType.DEATH)
|
62
|
-
{
|
63
|
-
spriteRenderer.sprite = deathSprite;
|
64
|
-
}
|
65
|
-
else if (type == BlockType.ALIVE)
|
66
|
-
{
|
67
|
-
spriteRenderer.sprite = aliveSprite;
|
68
|
-
}
|
69
|
-
}
|
70
|
-
|
71
|
-
//クリックしたら実行
|
72
|
-
public void OnBlock()
|
73
|
-
{
|
74
|
-
ClearBlock();
|
75
|
-
stageManager.ClickedBlock(intPosition);
|
76
|
-
}
|
77
|
-
|
78
|
-
public void ClearBlock()
|
79
|
-
{
|
80
|
-
if (type == BlockType.DEATH)
|
81
|
-
{
|
82
|
-
SetType(BlockType.ALIVE);
|
83
|
-
}
|
84
|
-
else if (type == BlockType.ALIVE)
|
85
|
-
{
|
86
|
-
SetType(BlockType.DEATH);
|
87
|
-
Destroy(this.gameObject); //ブロックを消す
|
88
|
-
}
|
89
|
-
}
|
90
|
-
}
|
91
|
-
```
|
92
|
-
|
93
|
-
|
94
|
-
↓ブロックの配列やクリック処理をしているスクリプト
|
95
|
-
```C#:StageManager
|
96
|
-
using System.Collections;
|
97
|
-
using System.Collections.Generic;
|
98
|
-
using UnityEngine;
|
99
|
-
using UnityEngine.SceneManagement;
|
100
|
-
|
101
|
-
public class StageManager : MonoBehaviour
|
102
|
-
{
|
103
|
-
public Animator openAnimator;
|
104
|
-
public Animator openStopAnimator;
|
105
|
-
|
106
|
-
public GameObject clearImage;
|
107
|
-
public GameObject stopImage;
|
108
|
-
public GameObject okButton;
|
109
|
-
public GameObject reloadButton;
|
110
|
-
public GameObject handOpenSprite;
|
111
|
-
|
112
|
-
public TextAsset stageFile1;
|
113
|
-
public TextAsset stageFile2;
|
114
|
-
BlockType[,] blockTable;
|
115
|
-
BlockType[,] blockTable2;
|
116
|
-
|
117
|
-
BlocksController[,] blockTableobj;
|
118
|
-
BlocksController[,] blockTableobj2;
|
119
|
-
|
120
|
-
public BlocksController blockPrefab;
|
121
|
-
|
122
|
-
void Start()
|
123
|
-
{
|
124
|
-
LoadStageFromText();
|
125
|
-
DebugTable();
|
126
|
-
CreateStage();
|
127
|
-
openAnimator = GetComponent<Animator>();
|
128
|
-
}
|
129
|
-
|
130
|
-
void CreateStage()
|
131
|
-
{
|
132
|
-
Vector3 halfSize = default;
|
133
|
-
float blockSize = blockPrefab.GetComponent<SpriteRenderer>().bounds.size.x;
|
134
|
-
halfSize.x = blockSize * (blockTable.GetLength(0) / 2);
|
135
|
-
halfSize.y = blockSize * (blockTable.GetLength(1) / 2);
|
136
|
-
|
137
|
-
for (int y = 0; y < blockTable.GetLength(1); y++)
|
138
|
-
{
|
139
|
-
for (int x = 0; x < blockTable.GetLength(0); x++)
|
140
|
-
{
|
141
|
-
Vector3Int position = new Vector3Int(x, y, 0);
|
142
|
-
BlocksController block = Instantiate(blockPrefab);
|
143
|
-
block.Init(blockTable[x,y], position, this);
|
144
|
-
Vector3 setPosition = (Vector3)position * blockSize - halfSize;
|
145
|
-
setPosition.y *= (float)-1.04;
|
146
|
-
setPosition.x *= (float)-1.04;
|
147
|
-
block.transform.position = setPosition;
|
148
|
-
blockTableobj[x, y] = block;
|
149
|
-
}
|
150
|
-
}
|
151
|
-
reloadButton.SetActive(true);
|
152
|
-
}
|
153
|
-
|
154
|
-
public void LoadStageFromText()
|
155
|
-
{
|
156
|
-
string[] lines = stageFile1.text.Split(new[] { '\n', '\r' }, System.StringSplitOptions.RemoveEmptyEntries);
|
157
|
-
int columns = 10;
|
158
|
-
int rows = 5;
|
159
|
-
|
160
|
-
blockTable = new BlockType[rows, columns];
|
161
|
-
blockTableobj = new BlocksController[rows, columns];
|
162
|
-
for (int y = 0; y < columns; y++)
|
163
|
-
{
|
164
|
-
string[] values = lines[y].Split(new[] { ',' });
|
165
|
-
for (int x = 0; x < rows; x++)
|
166
|
-
{
|
167
|
-
if (values[x] == "0")
|
168
|
-
{
|
169
|
-
blockTable[x, y] = BlockType.DEATH;
|
170
|
-
}
|
171
|
-
if (values[x] == "1")
|
172
|
-
{
|
173
|
-
blockTable[x, y] = BlockType.ALIVE;
|
174
|
-
}
|
175
|
-
}
|
176
|
-
}
|
177
|
-
}
|
178
|
-
|
179
|
-
public void ClearStageText()
|
180
|
-
{
|
181
|
-
string[] lines = stageFile2.text.Split(new[] { '\n', '\r' }, System.StringSplitOptions.RemoveEmptyEntries);
|
182
|
-
int columns = 10;
|
183
|
-
int rows = 5;
|
184
|
-
|
185
|
-
blockTable2 = new BlockType[rows, columns];
|
186
|
-
blockTableobj2 = new BlocksController[rows, columns];
|
187
|
-
for (int y = 0; y < columns; y++)
|
188
|
-
{
|
189
|
-
string[] values = lines[y].Split(new[] { ',' });
|
190
|
-
for (int x = 0; x < rows; x++)
|
191
|
-
{
|
192
|
-
if (values[x] == "0")
|
193
|
-
{
|
194
|
-
blockTable2[x, y] = BlockType.DEATH;
|
195
|
-
}
|
196
|
-
if (values[x] == "1")
|
197
|
-
{
|
198
|
-
blockTable2[x, y] = BlockType.ALIVE;
|
199
|
-
}
|
200
|
-
}
|
201
|
-
}
|
202
|
-
}
|
203
|
-
|
204
|
-
public void ClickedBlock(Vector3Int center)
|
205
|
-
{
|
206
|
-
Debug.Log("ClickedBlock");
|
207
|
-
}
|
208
|
-
|
209
|
-
public void IsClear()
|
210
|
-
{
|
211
|
-
ClearStageText();
|
212
|
-
bool isSuccess = true;
|
213
|
-
for (int y = 0; y < blockTable2.GetLength(1); y++)
|
214
|
-
{
|
215
|
-
for (int x = 0; x < blockTable2.GetLength(0); x++)
|
216
|
-
{
|
217
|
-
if (blockTableobj[x,y].type != blockTable2[x,y]) isSuccess = false;
|
218
|
-
}
|
219
|
-
}
|
220
|
-
|
221
|
-
if (isSuccess)
|
222
|
-
{
|
223
|
-
var clones = GameObject.FindGameObjectsWithTag("Yellow");
|
224
|
-
foreach (var clone in clones)
|
225
|
-
{
|
226
|
-
Destroy(clone);
|
227
|
-
}
|
228
|
-
clearImage.SetActive(true);
|
229
|
-
stopImage.SetActive(true);
|
230
|
-
openAnimator.SetTrigger("OpenAni");
|
231
|
-
openStopAnimator.SetTrigger("OpenAni2");
|
232
|
-
Destroy(okButton.gameObject);
|
233
|
-
Invoke("HandOpenMove", 1.2f);
|
234
|
-
Debug.Log("正解");
|
235
|
-
|
236
|
-
}
|
237
|
-
else
|
238
|
-
{
|
239
|
-
//CreateStage();
|
240
|
-
//openAnimator.SetTrigger("Open");
|
241
|
-
Debug.Log("不正解");
|
242
|
-
}
|
243
|
-
}
|
244
|
-
|
245
|
-
public void HandOpenMove()
|
246
|
-
{
|
247
|
-
handOpenSprite.SetActive(true);
|
248
|
-
}
|
249
|
-
|
250
|
-
public void Reload()
|
251
|
-
{
|
252
|
-
SceneManager.LoadScene("Level1");
|
253
|
-
}
|
254
|
-
|
255
|
-
public void NextScene()
|
256
|
-
{
|
257
|
-
SceneManager.LoadScene("Level2");
|
258
|
-
}
|
259
|
-
|
260
|
-
void DebugTable()
|
261
|
-
{
|
262
|
-
for (int y = 0; y < 10; y++)
|
263
|
-
{
|
264
|
-
string debugText = "";
|
265
|
-
for (int x = 0; x < 5; x++)
|
266
|
-
{
|
267
|
-
debugText += blockTable[x, y] + ",";
|
268
|
-
}
|
269
|
-
Debug.Log(debugText);
|
270
|
-
}
|
271
|
-
}
|
272
|
-
}
|
273
|
-
```
|