質問編集履歴
3
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,245 +1,3 @@
|
|
1
1
|
3Dパズルゲームを作成中です。
|
2
2
|
Textデータ.Stage0の状態からクリックでブロックを消していき、Textデータ.Stage1の形の時にOKボタンを押すとクリアとしたいです。
|
3
|
-
自分の推測として、OKボタンを押した際にStage1情報を取得し、現在の配列情報と合致していたらクリアという考え方ではないかと推測していますがどのようなスクリプトを書いたら良いでしょうか?
|
4
|
-
|
5
|
-
|
6
|
-
```C#:BlocksController
|
7
|
-
using System.Collections;
|
8
|
-
using System.Collections.Generic;
|
9
|
-
using UnityEngine;
|
10
|
-
|
11
|
-
public enum BlockType
|
12
|
-
{
|
13
|
-
DEATH,
|
14
|
-
ALIVE,
|
15
|
-
}
|
16
|
-
|
17
|
-
public class BlocksController : MonoBehaviour
|
18
|
-
{
|
19
|
-
public BlockType type;
|
20
|
-
public Sprite deathSprite;
|
21
|
-
public Sprite aliveSprite;
|
22
|
-
|
23
|
-
SpriteRenderer spriteRenderer;
|
24
|
-
|
25
|
-
StageManager stageManager;
|
26
|
-
Vector3Int intPosition;
|
27
|
-
|
28
|
-
void Awake()
|
29
|
-
{
|
30
|
-
spriteRenderer = GetComponent<SpriteRenderer>();
|
31
|
-
}
|
32
|
-
|
33
|
-
public void Init(BlockType blockType, Vector3Int position, StageManager stageManager)
|
34
|
-
{
|
35
|
-
intPosition = position;
|
36
|
-
this.stageManager = stageManager;
|
37
|
-
SetType(type);
|
38
|
-
}
|
39
|
-
|
40
|
-
void SetType(BlockType blockType)
|
41
|
-
{
|
42
|
-
type = blockType;
|
43
|
-
SetSprite(type);
|
44
|
-
}
|
45
|
-
|
46
|
-
void SetSprite(BlockType type)
|
47
|
-
{
|
48
|
-
if (type == BlockType.DEATH)
|
49
|
-
{
|
50
|
-
spriteRenderer.sprite = deathSprite;
|
51
|
-
}
|
52
|
-
else if (type == BlockType.ALIVE)
|
53
|
-
{
|
54
|
-
spriteRenderer.sprite = aliveSprite;
|
55
|
-
}
|
56
|
-
}
|
57
|
-
|
58
|
-
//クリックしたら実行
|
59
|
-
public void OnBlock()
|
60
|
-
{
|
61
|
-
ClearBlock();
|
62
|
-
stageManager.ClickedBlock(intPosition);
|
63
|
-
}
|
64
|
-
|
65
|
-
public void ClearBlock()
|
66
|
-
{
|
67
|
-
if (type == BlockType.DEATH)
|
68
|
-
{
|
69
|
-
SetType(BlockType.ALIVE);
|
70
|
-
}
|
71
|
-
else if (type == BlockType.ALIVE)
|
72
|
-
{
|
73
|
-
SetType(BlockType.DEATH);
|
74
|
-
//Destroy(this.gameObject); //ブロックを消す
|
75
|
-
}
|
76
|
-
}
|
77
|
-
}
|
78
|
-
```
|
79
|
-
|
80
|
-
```C#:CheckButton
|
81
|
-
using System.Collections;
|
82
|
-
using System.Collections.Generic;
|
83
|
-
using UnityEngine;
|
84
|
-
|
85
|
-
public class CheckButton : MonoBehaviour
|
86
|
-
{
|
87
|
-
|
88
|
-
void Start()
|
89
|
-
{
|
90
|
-
|
91
|
-
}
|
92
|
-
|
93
|
-
void Update()
|
94
|
-
{
|
95
|
-
|
96
|
-
}
|
97
|
-
|
98
|
-
|
99
|
-
}
|
100
|
-
```
|
101
|
-
|
102
|
-
|
103
|
-
追記1
|
104
|
-
---
|
105
|
-
|
106
|
-
StageManagerにこのように記述しました。
|
107
|
-
|
108
|
-
```C#:StageManager
|
109
|
-
using System.Collections;
|
110
|
-
using System.Collections.Generic;
|
111
|
-
using UnityEngine;
|
112
|
-
|
113
|
-
public class StageManager : MonoBehaviour
|
114
|
-
{
|
115
|
-
public TextAsset stageFile1;
|
116
|
-
public TextAsset stageFile2;
|
117
|
-
BlockType[,] blockTable;
|
118
|
-
BlocksController[,] blockTableobj;
|
119
|
-
|
120
|
-
public BlocksController blockPrefab;
|
121
|
-
|
122
|
-
void Start()
|
123
|
-
{
|
124
|
-
LoadStageFromText();
|
125
|
-
DebugTable();
|
126
|
-
CreateStage();
|
127
|
-
|
128
|
-
// ステージのデータ
|
129
|
-
var blockTable = new BlockType[,] {
|
130
|
-
{ BlockType.DEATH, BlockType.DEATH, BlockType.ALIVE , BlockType.ALIVE, BlockType.ALIVE},
|
131
|
-
{ BlockType.DEATH, BlockType.ALIVE, BlockType.ALIVE , BlockType.ALIVE, BlockType.ALIVE},
|
132
|
-
{ BlockType.ALIVE, BlockType.ALIVE, BlockType.ALIVE , BlockType.ALIVE, BlockType.ALIVE},
|
133
|
-
{ BlockType.ALIVE, BlockType.ALIVE, BlockType.DEATH , BlockType.DEATH, BlockType.ALIVE},
|
134
|
-
{ BlockType.ALIVE, BlockType.ALIVE, BlockType.DEATH , BlockType.DEATH, BlockType.ALIVE},
|
135
|
-
{ BlockType.ALIVE, BlockType.ALIVE, BlockType.ALIVE , BlockType.ALIVE, BlockType.ALIVE},
|
136
|
-
{ BlockType.ALIVE, BlockType.ALIVE, BlockType.DEATH , BlockType.DEATH, BlockType.DEATH},
|
137
|
-
{ BlockType.ALIVE, BlockType.ALIVE, BlockType.ALIVE , BlockType.DEATH, BlockType.DEATH},
|
138
|
-
{ BlockType.DEATH, BlockType.ALIVE, BlockType.ALIVE , BlockType.ALIVE, BlockType.ALIVE},
|
139
|
-
{ BlockType.DEATH, BlockType.DEATH, BlockType.ALIVE , BlockType.ALIVE, BlockType.ALIVE},
|
140
|
-
};
|
141
|
-
|
142
|
-
// 操作されるゲームオブジェクトが持つデータ
|
143
|
-
var BlocksController = new BlocksController[,] {
|
144
|
-
{ new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE } },
|
145
|
-
{ new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE } },
|
146
|
-
{ new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE } },
|
147
|
-
{ new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.ALIVE } },
|
148
|
-
{ new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.ALIVE } },
|
149
|
-
{ new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE } },
|
150
|
-
{ new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.DEATH } },
|
151
|
-
{ new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.DEATH } },
|
152
|
-
{ new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE } },
|
153
|
-
{ new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE } },
|
154
|
-
};
|
155
|
-
|
156
|
-
}
|
157
|
-
|
158
|
-
void CreateStage()
|
159
|
-
{
|
160
|
-
Vector3 halfSize = default;
|
161
|
-
float blockSize = blockPrefab.GetComponent<SpriteRenderer>().bounds.size.x;
|
162
|
-
halfSize.x = blockSize * (blockTable.GetLength(0) / 2);
|
163
|
-
halfSize.y = blockSize * (blockTable.GetLength(1) / 2);
|
164
|
-
|
165
|
-
for (int y = 0; y < blockTable.GetLength(1); y++)
|
166
|
-
{
|
167
|
-
for (int x = 0; x < blockTable.GetLength(0); x++)
|
168
|
-
{
|
169
|
-
Vector3Int position = new Vector3Int(x, y, 0);
|
170
|
-
BlocksController block = Instantiate(blockPrefab);
|
171
|
-
block.Init(blockTable[x,y], position, this);
|
172
|
-
Vector3 setPosition = (Vector3)position * blockSize - halfSize;
|
173
|
-
setPosition.y *= (float)-1.04;
|
174
|
-
setPosition.x *= (float)-1.04;
|
175
|
-
block.transform.position = setPosition;
|
176
|
-
blockTableobj[x, y] = block;
|
177
|
-
}
|
178
|
-
}
|
179
|
-
}
|
180
|
-
|
181
|
-
void LoadStageFromText()
|
182
|
-
{
|
183
|
-
string[] lines = stageFile1.text.Split(new[] { '\n', '\r' }, System.StringSplitOptions.RemoveEmptyEntries);
|
184
|
-
int columns = 10;
|
185
|
-
int rows = 5;
|
186
|
-
|
187
|
-
blockTable = new BlockType[rows, columns];
|
188
|
-
blockTableobj = new BlocksController[rows, columns];
|
189
|
-
for (int y = 0; y < columns; y++)
|
190
|
-
{
|
191
|
-
string[] values = lines[y].Split(new[] { ',' });
|
192
|
-
for (int x = 0; x < rows; x++)
|
193
|
-
{
|
194
|
-
if (values[x] == "0")
|
195
|
-
{
|
196
|
-
blockTable[x, y] = BlockType.DEATH;
|
197
|
-
}
|
198
|
-
if (values[x] == "1")
|
199
|
-
{
|
200
|
-
blockTable[x, y] = BlockType.ALIVE;
|
201
|
-
}
|
202
|
-
}
|
203
|
-
}
|
204
|
-
}
|
205
|
-
|
206
|
-
public void ClickedBlock(Vector3Int center)
|
207
|
-
{
|
208
|
-
Debug.Log("ClickedBlock");
|
209
|
-
}
|
210
|
-
|
211
|
-
public void IsClear()
|
212
|
-
{
|
213
|
-
bool isSuccess = true;
|
214
|
-
for (int y = 0; y < blockTable.GetLength(1); y++)
|
215
|
-
{
|
216
|
-
for (int x = 0; x < blockTable.GetLength(0); x++)
|
217
|
-
{
|
218
|
-
if (blockTableobj[x, y].type != blockTable[x, y]) isSuccess = false;
|
219
|
-
}
|
220
|
-
}
|
221
|
-
|
222
|
-
if (isSuccess)
|
223
|
-
{
|
224
|
-
Debug.Log("正解");
|
225
|
-
}
|
226
|
-
else
|
227
|
-
{
|
228
|
-
Debug.Log("不正解");
|
229
|
-
}
|
230
|
-
}
|
231
|
-
|
232
|
-
void DebugTable()
|
233
|
-
{
|
234
|
-
for (int y = 0; y < 10; y++)
|
235
|
-
{
|
236
|
-
string debugText = "";
|
237
|
-
for (int x = 0; x < 5; x++)
|
238
|
-
{
|
239
|
-
debugText += blockTable[x, y] + ",";
|
240
|
-
}
|
241
|
-
Debug.Log(debugText);
|
242
|
-
}
|
243
|
-
}
|
244
|
-
}
|
245
|
-
```
|
3
|
+
自分の推測として、OKボタンを押した際にStage1情報を取得し、現在の配列情報と合致していたらクリアという考え方ではないかと推測していますがどのようなスクリプトを書いたら良いでしょうか?
|
2
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -242,13 +242,4 @@
|
|
242
242
|
}
|
243
243
|
}
|
244
244
|
}
|
245
|
-
```
|
245
|
+
```
|
246
|
-
|
247
|
-
ButtonにStageManagerをアタッチしてIsClear関数を呼び出しました
|
248
|
-

|
249
|
-
|
250
|
-
blockTableのデータとblockTableobj[x, y].typeのデータは合っている?と思われるのですがこの状態でOKを押すと正解が出てしまいます
|
251
|
-

|
252
|
-
|
253
|
-
この形になった際に正解にしたいのですが不正解になってしまいます
|
254
|
-

|
1
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -77,7 +77,34 @@
|
|
77
77
|
}
|
78
78
|
```
|
79
79
|
|
80
|
+
```C#:CheckButton
|
81
|
+
using System.Collections;
|
82
|
+
using System.Collections.Generic;
|
83
|
+
using UnityEngine;
|
80
84
|
|
85
|
+
public class CheckButton : MonoBehaviour
|
86
|
+
{
|
87
|
+
|
88
|
+
void Start()
|
89
|
+
{
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
void Update()
|
94
|
+
{
|
95
|
+
|
96
|
+
}
|
97
|
+
|
98
|
+
|
99
|
+
}
|
100
|
+
```
|
101
|
+
|
102
|
+
|
103
|
+
追記1
|
104
|
+
---
|
105
|
+
|
106
|
+
StageManagerにこのように記述しました。
|
107
|
+
|
81
108
|
```C#:StageManager
|
82
109
|
using System.Collections;
|
83
110
|
using System.Collections.Generic;
|
@@ -97,6 +124,35 @@
|
|
97
124
|
LoadStageFromText();
|
98
125
|
DebugTable();
|
99
126
|
CreateStage();
|
127
|
+
|
128
|
+
// ステージのデータ
|
129
|
+
var blockTable = new BlockType[,] {
|
130
|
+
{ BlockType.DEATH, BlockType.DEATH, BlockType.ALIVE , BlockType.ALIVE, BlockType.ALIVE},
|
131
|
+
{ BlockType.DEATH, BlockType.ALIVE, BlockType.ALIVE , BlockType.ALIVE, BlockType.ALIVE},
|
132
|
+
{ BlockType.ALIVE, BlockType.ALIVE, BlockType.ALIVE , BlockType.ALIVE, BlockType.ALIVE},
|
133
|
+
{ BlockType.ALIVE, BlockType.ALIVE, BlockType.DEATH , BlockType.DEATH, BlockType.ALIVE},
|
134
|
+
{ BlockType.ALIVE, BlockType.ALIVE, BlockType.DEATH , BlockType.DEATH, BlockType.ALIVE},
|
135
|
+
{ BlockType.ALIVE, BlockType.ALIVE, BlockType.ALIVE , BlockType.ALIVE, BlockType.ALIVE},
|
136
|
+
{ BlockType.ALIVE, BlockType.ALIVE, BlockType.DEATH , BlockType.DEATH, BlockType.DEATH},
|
137
|
+
{ BlockType.ALIVE, BlockType.ALIVE, BlockType.ALIVE , BlockType.DEATH, BlockType.DEATH},
|
138
|
+
{ BlockType.DEATH, BlockType.ALIVE, BlockType.ALIVE , BlockType.ALIVE, BlockType.ALIVE},
|
139
|
+
{ BlockType.DEATH, BlockType.DEATH, BlockType.ALIVE , BlockType.ALIVE, BlockType.ALIVE},
|
140
|
+
};
|
141
|
+
|
142
|
+
// 操作されるゲームオブジェクトが持つデータ
|
143
|
+
var BlocksController = new BlocksController[,] {
|
144
|
+
{ new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE } },
|
145
|
+
{ new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE } },
|
146
|
+
{ new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE } },
|
147
|
+
{ new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.ALIVE } },
|
148
|
+
{ new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.ALIVE } },
|
149
|
+
{ new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE } },
|
150
|
+
{ new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.DEATH } },
|
151
|
+
{ new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.DEATH } },
|
152
|
+
{ new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE } },
|
153
|
+
{ new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.DEATH }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE }, new BlocksController {type = BlockType.ALIVE } },
|
154
|
+
};
|
155
|
+
|
100
156
|
}
|
101
157
|
|
102
158
|
void CreateStage()
|
@@ -114,7 +170,8 @@
|
|
114
170
|
BlocksController block = Instantiate(blockPrefab);
|
115
171
|
block.Init(blockTable[x,y], position, this);
|
116
172
|
Vector3 setPosition = (Vector3)position * blockSize - halfSize;
|
117
|
-
setPosition.y *= -1;
|
173
|
+
setPosition.y *= (float)-1.04;
|
174
|
+
setPosition.x *= (float)-1.04;
|
118
175
|
block.transform.position = setPosition;
|
119
176
|
blockTableobj[x, y] = block;
|
120
177
|
}
|
@@ -149,12 +206,27 @@
|
|
149
206
|
public void ClickedBlock(Vector3Int center)
|
150
207
|
{
|
151
208
|
Debug.Log("ClickedBlock");
|
152
|
-
ClearBlocks(center);
|
153
209
|
}
|
154
210
|
|
155
|
-
void
|
211
|
+
public void IsClear()
|
156
212
|
{
|
213
|
+
bool isSuccess = true;
|
214
|
+
for (int y = 0; y < blockTable.GetLength(1); y++)
|
215
|
+
{
|
216
|
+
for (int x = 0; x < blockTable.GetLength(0); x++)
|
217
|
+
{
|
218
|
+
if (blockTableobj[x, y].type != blockTable[x, y]) isSuccess = false;
|
219
|
+
}
|
220
|
+
}
|
157
221
|
|
222
|
+
if (isSuccess)
|
223
|
+
{
|
224
|
+
Debug.Log("正解");
|
225
|
+
}
|
226
|
+
else
|
227
|
+
{
|
228
|
+
Debug.Log("不正解");
|
229
|
+
}
|
158
230
|
}
|
159
231
|
|
160
232
|
void DebugTable()
|
@@ -172,53 +244,11 @@
|
|
172
244
|
}
|
173
245
|
```
|
174
246
|
|
247
|
+
ButtonにStageManagerをアタッチしてIsClear関数を呼び出しました
|
248
|
+

|
175
249
|
|
176
|
-
```C#:CheckButton
|
177
|
-
|
250
|
+
blockTableのデータとblockTableobj[x, y].typeのデータは合っている?と思われるのですがこの状態でOKを押すと正解が出てしまいます
|
178
|
-
|
251
|
+

|
179
|
-
using UnityEngine;
|
180
252
|
|
253
|
+
この形になった際に正解にしたいのですが不正解になってしまいます
|
181
|
-
|
254
|
+

|
182
|
-
{
|
183
|
-
|
184
|
-
void Start()
|
185
|
-
{
|
186
|
-
|
187
|
-
}
|
188
|
-
|
189
|
-
void Update()
|
190
|
-
{
|
191
|
-
|
192
|
-
}
|
193
|
-
|
194
|
-
|
195
|
-
}
|
196
|
-
```
|
197
|
-
|
198
|
-
|
199
|
-
```C#:Stage0
|
200
|
-
1,1,1,1,1
|
201
|
-
1,1,1,1,1
|
202
|
-
1,1,1,1,1
|
203
|
-
1,1,1,1,1
|
204
|
-
1,1,1,1,1
|
205
|
-
1,1,1,1,1
|
206
|
-
1,1,1,1,1
|
207
|
-
1,1,1,1,1
|
208
|
-
1,1,1,1,1
|
209
|
-
1,1,1,1,1
|
210
|
-
```
|
211
|
-
|
212
|
-
|
213
|
-
```C#:Stage1
|
214
|
-
0,0,1,1,1
|
215
|
-
0,1,1,1,1
|
216
|
-
1,1,1,1,1
|
217
|
-
1,1,0,0,1
|
218
|
-
1,1,0,0,1
|
219
|
-
1,1,1,1,1
|
220
|
-
1,1,0,0,0
|
221
|
-
1,1,1,0,0
|
222
|
-
0,1,1,1,1
|
223
|
-
0,0,1,1,1
|
224
|
-
```
|