質問編集履歴
3
問題の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,7 +8,11 @@
|
|
8
8
|
```
|
9
9
|
No MonoBehaviour scripts in the file, or their names do not match fale name.
|
10
10
|
```
|
11
|
+
↳解消。
|
11
12
|
|
13
|
+
```
|
14
|
+
Assets\Editor\object2terrain.cs(1,1):error CS1056: Unexpected character"
|
15
|
+
```
|
12
16
|
### 該当のソースコード
|
13
17
|
|
14
18
|
```ここに言語名を入力
|
2
リンクの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -321,4 +321,14 @@
|
|
321
321
|
Visual studioは2019を使っています。
|
322
322
|
Unityは2019.3.13f1を使っています。
|
323
323
|
|
324
|
-
Object2Terrain.csは以前はGitHubからダウンロードしていました。しかし、最近ダウンロードしようとしたらページが見つかりませんでした。
|
324
|
+
Object2Terrain.csは以前はGitHubからダウンロードしていました。しかし、最近ダウンロードしようとしたらページが見つかりませんでした。エラーばかり出たのでリンクを探したところ、[Object2Terrain.cs](https://www.mediafire.com/file/3oyvdjcjix2d3iu/Object2Terrain.cs/file)からダウンロードしました。しかし、
|
325
|
+
|
326
|
+
```
|
327
|
+
using System.Collections
|
328
|
+
```
|
329
|
+
を挿入しないと
|
330
|
+
|
331
|
+
```
|
332
|
+
No MonoBehaviour
|
333
|
+
```
|
334
|
+
のエラーメッセージが出ます。
|
1
前記のエラーの解消→新たなエラーの発生、内容の大幅追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -166,8 +166,156 @@
|
|
166
166
|
・void がつく場所すべてに『Private』を付加。⇒上手くいかず。
|
167
167
|
・classがミスで違ってた箇所ありなので、修正。⇒?
|
168
168
|
大文字小文字のミスがないか確認。⇒ミスがあった箇所は修正。ただ、まだミスがあるかもしれません。
|
169
|
+
-------------
|
169
170
|
|
171
|
+
```
|
172
|
+
using System.Collections
|
173
|
+
```
|
174
|
+
を追加しました。そしたら、『No MonoBehaviour』が消えましたが、error CS1056:『Unexpected character』が出ました。
|
170
175
|
|
176
|
+
修正後のコードはこちらです。
|
177
|
+
|
178
|
+
```
|
179
|
+
using UnityEngine;
|
180
|
+
using UnityEditor;
|
181
|
+
using System.Collections;
|
182
|
+
|
183
|
+
public class Object2Terrain : EditorWindow {
|
184
|
+
|
185
|
+
[MenuItem("Terrain/Object to Terrain", false, 2000)]
|
186
|
+
static void OpenWindow() => EditorWindow.GetWindow(typeof(Object2Terrain))(true);
|
187
|
+
|
188
|
+
private int resolution = 512;
|
189
|
+
private Vector3 addTerrain;
|
190
|
+
int bottomTopRadioSelected = 0;
|
191
|
+
static string[] bottomTopRadio = new string[] { "Bottom Up", "Top Down"};
|
192
|
+
private float shiftHeight = 0f;
|
193
|
+
|
194
|
+
void OnGUI () {
|
195
|
+
|
196
|
+
resolution = EditorGUILayout.IntField("Resolution", resolution);
|
197
|
+
addTerrain = EditorGUILayout.Vector3Field("Add terrain", addTerrain);
|
198
|
+
shiftHeight = EditorGUILayout.Slider("Shift height", shiftHeight, -1f, 1f);
|
199
|
+
bottomTopRadioSelected = GUILayout.SelectionGrid(bottomTopRadioSelected, bottomTopRadio, bottomTopRadio.Length, EditorStyles.radioButton);
|
200
|
+
|
201
|
+
if(GUILayout.Button("Create Terrain")){
|
202
|
+
|
203
|
+
if(Selection.activeGameObject == null){
|
204
|
+
|
205
|
+
EditorUtility.DisplayDialog("No object selected", "Please select an object.", "Ok");
|
206
|
+
return;
|
207
|
+
}
|
208
|
+
|
209
|
+
else{
|
210
|
+
|
211
|
+
CreateTerrain();
|
212
|
+
}
|
213
|
+
}
|
214
|
+
}
|
215
|
+
|
216
|
+
delegate void CleanUp();
|
217
|
+
|
218
|
+
void CreateTerrain(){
|
219
|
+
|
220
|
+
//fire up the progress bar
|
221
|
+
ShowProgressBar(1, 100);
|
222
|
+
|
223
|
+
TerrainData terrain = new TerrainData();
|
224
|
+
terrain.heightmapResolution = resolution;
|
225
|
+
GameObject terrainObject = Terrain.CreateTerrainGameObject(terrain);
|
226
|
+
|
227
|
+
Undo.RegisterCreatedObjectUndo(terrainObject, "Object to Terrain");
|
228
|
+
|
229
|
+
MeshCollider collider = Selection.activeGameObject.GetComponent<MeshCollider>();
|
230
|
+
CleanUp cleanUp = null;
|
231
|
+
|
232
|
+
//Add a collider to our source object if it does not exist.
|
233
|
+
//Otherwise raycasting doesn't work.
|
234
|
+
if(!collider){
|
235
|
+
|
236
|
+
collider = Selection.activeGameObject.AddComponent<MeshCollider>();
|
237
|
+
cleanUp = () => DestroyImmediate(collider);
|
238
|
+
}
|
239
|
+
|
240
|
+
Bounds bounds = collider.bounds;
|
241
|
+
float sizeFactor = collider.bounds.size.y / (collider.bounds.size.y + addTerrain.y);
|
242
|
+
terrain.size = collider.bounds.size + addTerrain;
|
243
|
+
bounds.size = new Vector3(terrain.size.x, collider.bounds.size.y, terrain.size.z);
|
244
|
+
|
245
|
+
// Do raycasting samples over the object to see what terrain heights should be
|
246
|
+
float[,] heights = new float[terrain.heightmapWidth, terrain.heightmapHeight];
|
247
|
+
Ray ray = new Ray(new Vector3(bounds.min.x, bounds.max.y + bounds.size.y, bounds.min.z), -Vector3.up);
|
248
|
+
RaycastHit hit = new RaycastHit();
|
249
|
+
float meshHeightInverse = 1 / bounds.size.y;
|
250
|
+
Vector3 rayOrigin = ray.origin;
|
251
|
+
|
252
|
+
int maxHeight = heights.GetLength(0);
|
253
|
+
int maxLength = heights.GetLength(1);
|
254
|
+
|
255
|
+
Vector2 stepXZ = new Vector2(bounds.size.x / maxLength, bounds.size.z / maxHeight);
|
256
|
+
|
257
|
+
for(int zCount = 0; zCount < maxHeight; zCount++){
|
258
|
+
|
259
|
+
ShowProgressBar(zCount, maxHeight);
|
260
|
+
|
261
|
+
for(int xCount = 0; xCount < maxLength; xCount++){
|
262
|
+
|
263
|
+
float height = 0.0f;
|
264
|
+
|
265
|
+
if(collider.Raycast(ray, out hit, bounds.size.y * 3)){
|
266
|
+
|
267
|
+
height = (hit.point.y - bounds.min.y) * meshHeightInverse;
|
268
|
+
height += shiftHeight;
|
269
|
+
|
270
|
+
//bottom up
|
271
|
+
if(bottomTopRadioSelected == 0){
|
272
|
+
|
273
|
+
height *= sizeFactor;
|
274
|
+
}
|
275
|
+
|
276
|
+
//clamp
|
277
|
+
if(height < 0){
|
278
|
+
|
279
|
+
height = 0;
|
280
|
+
}
|
281
|
+
}
|
282
|
+
|
283
|
+
heights[zCount, xCount] = height;
|
284
|
+
rayOrigin.x += stepXZ[0];
|
285
|
+
ray.origin = rayOrigin;
|
286
|
+
}
|
287
|
+
|
288
|
+
rayOrigin.z += stepXZ[1];
|
289
|
+
rayOrigin.x = bounds.min.x;
|
290
|
+
ray.origin = rayOrigin;
|
291
|
+
}
|
292
|
+
|
293
|
+
terrain.SetHeights(0, 0, heights);
|
294
|
+
|
295
|
+
EditorUtility.ClearProgressBar();
|
296
|
+
|
297
|
+
if(cleanUp != null){
|
298
|
+
|
299
|
+
cleanUp();
|
300
|
+
}
|
301
|
+
}
|
302
|
+
|
303
|
+
void ShowProgressBar(float progress, float maxProgress){
|
304
|
+
|
305
|
+
float p = progress / maxProgress;
|
306
|
+
EditorUtility.DisplayProgressBar("Creating Terrain...", Mathf.RoundToInt(p * 100f)+ " %", p);
|
307
|
+
}
|
308
|
+
}
|
309
|
+
```
|
310
|
+
---------------
|
311
|
+
|
312
|
+
あと、このようなマークが出てきまして、
|
313
|
+

|
314
|
+
|
315
|
+
『フィールドのカプセル化(およびプロパティを使用します)』と出ます。あと、もう一つ出ていまして『フィールドのカプセル化(ただしフィールドを継続して使用します)』と出ます。
|
316
|
+
どちらを適用したらいいのか、またはどちらも適用しなくてもいいのか?教えていただけると嬉しいですしとても役に立ちます。
|
317
|
+
--------------
|
318
|
+
|
171
319
|
### 補足情報(FW/ツールのバージョンなど)
|
172
320
|
|
173
321
|
Visual studioは2019を使っています。
|