質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

GitHub

GitHubは、Gitバージョン管理システムを利用したソフトウェア開発向けの共有ウェブサービスです。GitHub商用プランおよびオープンソースプロジェクト向けの無料アカウントを提供しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

1回答

2345閲覧

Unityの『Object2Terrain』が動きません。

U_Bonche

総合スコア19

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

GitHub

GitHubは、Gitバージョン管理システムを利用したソフトウェア開発向けの共有ウェブサービスです。GitHub商用プランおよびオープンソースプロジェクト向けの無料アカウントを提供しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/05/14 06:52

編集2020/05/17 02:21

前提・実現したいこと

ここに質問の内容を詳しく書いてください。
Unityで『Object2Terrain』のスクリプトを書いていたのですが、下記のようなメッセージが出た上、メニューアイテムにこのプログラムが追加されないので困っています…。プログラムはVisual studio 2019で書きました。

発生している問題・エラーメッセージ

No MonoBehaviour scripts in the file, or their names do not match fale name.

↳解消。

Assets\Editor\object2terrain.cs(1,1):error CS1056: Unexpected character"

該当のソースコード

using UnityEngine; using UnityEditor; public class Object2Terrain : EditorWindow { [MenuItem("Terrain/Object to Terrain", false, 2000)] static void OpenWindow() { EditorWindow.GetWindow<Object2Terrain>(true); } private int resolution = 512; private Vector3 addTerrain; int bottomTopRadioSelected = 0; static string[] bottomTopRadio = new string[] { "Bottom Up", "Top Down" }; private float shiftHeight = 0f; void OnGUI() { resolution = EditorGUILayout.IntField("Resolution", resolution); addTerrain = EditorGUILayout.Vector3Field("Add terrain", addTerrain); shiftHeight = EditorGUILayout.Slider("Shift height", shiftHeight, -1f, 1f); bottomTopRadioSelected = GUILayout.SelectionGrid(bottomTopRadioSelected, bottomTopRadio, bottomTopRadio.Length, EditorStyles.radioButton); if (GUILayout.Button("Create Terrain")) { if (Selection.activeGameObject == null) { EditorUtility.DisplayDialog("No object selected", "Please select an object.", "Ok"); return; } else { CreateTerrain(); } } } delegate void CleanUp(); void CreateTerrain() { //fire up the progress bar ShowProgressBar(1, 100); TerrainData terrain = new TerrainData(); terrain.heightmapResolution = resolution; terrain.SetDetailResolution(resolution, 100); GameObject terrainObject = Terrain.CreateTerrainGameObject(terrain); Undo.RegisterCreatedObjectUndo(terrainObject, "Object to Terrain"); MeshCollider collider = Selection.activeGameObject.GetComponent<MeshCollider>(); CleanUp cleanUp = null; //Add a collider to our source object if it does not exist. //Otherwise raycasting doesn't work. if (!collider) { collider = Selection.activeGameObject.AddComponent<MeshCollider>(); cleanUp = () => DestroyImmediate(collider); } Bounds bounds = collider.bounds; float sizeFactor = collider.bounds.size.y / (collider.bounds.size.y + addTerrain.y); terrain.size = collider.bounds.size + addTerrain; bounds.size = new Vector3(terrain.size.x, collider.bounds.size.y, terrain.size.z); // Do raycasting samples over the object to see what terrain heights should be float[,] heights = new float[terrain.heightmapWidth, terrain.heightmapHeight]; Ray ray = new Ray(new Vector3(bounds.min.x, bounds.max.y + bounds.size.y, bounds.min.z), -Vector3.up); RaycastHit hit = new RaycastHit(); float meshHeightInverse = 1 / bounds.size.y; Vector3 rayOrigin = ray.origin; int maxHeight = heights.GetLength(0); int maxLength = heights.GetLength(1); Vector2 stepXZ = new Vector2(bounds.size.x / maxLength, bounds.size.z / maxHeight); for (int zCount = 0; zCount < maxHeight; zCount++) { ShowProgressBar(zCount, maxHeight); for (int xCount = 0; xCount < maxLength; xCount++) { float height = 0.0f; if (collider.Raycast(ray, out hit, bounds.size.y * 3)) { height = (hit.point.y - bounds.min.y) * meshHeightInverse; height += shiftHeight; //bottom up if (bottomTopRadioSelected == 0) { height *= sizeFactor; } //clamp if (height < 0) { height = 0; } } heights[zCount, xCount] = height; rayOrigin.x += stepXZ[0]; ray.origin = rayOrigin; } rayOrigin.z += stepXZ[1]; rayOrigin.x = bounds.min.x; ray.origin = rayOrigin; } terrain.SetHeights(0, 0, heights); EditorUtility.ClearProgressBar(); if (cleanUp != null) { cleanUp(); } } void ShowProgressBar(float progress, float maxProgress) { float p = progress / maxProgress; EditorUtility.DisplayProgressBar("Creating Terrain...", Mathf.RoundToInt(p * 100f) + " %", p); } }

試したこと

・void がつく場所すべてに『Private』を付加。⇒上手くいかず。
・classがミスで違ってた箇所ありなので、修正。⇒?
大文字小文字のミスがないか確認。⇒ミスがあった箇所は修正。ただ、まだミスがあるかもしれません。

using System.Collections

を追加しました。そしたら、『No MonoBehaviour』が消えましたが、error CS1056:『Unexpected character』が出ました。

修正後のコードはこちらです。

using UnityEngine; using UnityEditor; using System.Collections; public class Object2Terrain : EditorWindow { [MenuItem("Terrain/Object to Terrain", false, 2000)] static void OpenWindow() => EditorWindow.GetWindow(typeof(Object2Terrain))(true); private int resolution = 512; private Vector3 addTerrain; int bottomTopRadioSelected = 0; static string[] bottomTopRadio = new string[] { "Bottom Up", "Top Down"}; private float shiftHeight = 0f; void OnGUI () { resolution = EditorGUILayout.IntField("Resolution", resolution); addTerrain = EditorGUILayout.Vector3Field("Add terrain", addTerrain); shiftHeight = EditorGUILayout.Slider("Shift height", shiftHeight, -1f, 1f); bottomTopRadioSelected = GUILayout.SelectionGrid(bottomTopRadioSelected, bottomTopRadio, bottomTopRadio.Length, EditorStyles.radioButton); if(GUILayout.Button("Create Terrain")){ if(Selection.activeGameObject == null){ EditorUtility.DisplayDialog("No object selected", "Please select an object.", "Ok"); return; } else{ CreateTerrain(); } } } delegate void CleanUp(); void CreateTerrain(){ //fire up the progress bar ShowProgressBar(1, 100); TerrainData terrain = new TerrainData(); terrain.heightmapResolution = resolution; GameObject terrainObject = Terrain.CreateTerrainGameObject(terrain); Undo.RegisterCreatedObjectUndo(terrainObject, "Object to Terrain"); MeshCollider collider = Selection.activeGameObject.GetComponent<MeshCollider>(); CleanUp cleanUp = null; //Add a collider to our source object if it does not exist. //Otherwise raycasting doesn't work. if(!collider){ collider = Selection.activeGameObject.AddComponent<MeshCollider>(); cleanUp = () => DestroyImmediate(collider); } Bounds bounds = collider.bounds; float sizeFactor = collider.bounds.size.y / (collider.bounds.size.y + addTerrain.y); terrain.size = collider.bounds.size + addTerrain; bounds.size = new Vector3(terrain.size.x, collider.bounds.size.y, terrain.size.z); // Do raycasting samples over the object to see what terrain heights should be float[,] heights = new float[terrain.heightmapWidth, terrain.heightmapHeight]; Ray ray = new Ray(new Vector3(bounds.min.x, bounds.max.y + bounds.size.y, bounds.min.z), -Vector3.up); RaycastHit hit = new RaycastHit(); float meshHeightInverse = 1 / bounds.size.y; Vector3 rayOrigin = ray.origin; int maxHeight = heights.GetLength(0); int maxLength = heights.GetLength(1); Vector2 stepXZ = new Vector2(bounds.size.x / maxLength, bounds.size.z / maxHeight); for(int zCount = 0; zCount < maxHeight; zCount++){ ShowProgressBar(zCount, maxHeight); for(int xCount = 0; xCount < maxLength; xCount++){ float height = 0.0f; if(collider.Raycast(ray, out hit, bounds.size.y * 3)){ height = (hit.point.y - bounds.min.y) * meshHeightInverse; height += shiftHeight; //bottom up if(bottomTopRadioSelected == 0){ height *= sizeFactor; } //clamp if(height < 0){ height = 0; } } heights[zCount, xCount] = height; rayOrigin.x += stepXZ[0]; ray.origin = rayOrigin; } rayOrigin.z += stepXZ[1]; rayOrigin.x = bounds.min.x; ray.origin = rayOrigin; } terrain.SetHeights(0, 0, heights); EditorUtility.ClearProgressBar(); if(cleanUp != null){ cleanUp(); } } void ShowProgressBar(float progress, float maxProgress){ float p = progress / maxProgress; EditorUtility.DisplayProgressBar("Creating Terrain...", Mathf.RoundToInt(p * 100f)+ " %", p); } }

あと、このようなマークが出てきまして、
Visual studio2017 Object2Terrain.cs 謎のマーク

『フィールドのカプセル化(およびプロパティを使用します)』と出ます。あと、もう一つ出ていまして『フィールドのカプセル化(ただしフィールドを継続して使用します)』と出ます。
どちらを適用したらいいのか、またはどちらも適用しなくてもいいのか?教えていただけると嬉しいですしとても役に立ちます。

補足情報(FW/ツールのバージョンなど)

Visual studioは2019を使っています。
Unityは2019.3.13f1を使っています。

Object2Terrain.csは以前はGitHubからダウンロードしていました。しかし、最近ダウンロードしようとしたらページが見つかりませんでした。エラーばかり出たのでリンクを探したところ、Object2Terrain.csからダウンロードしました。しかし、

using System.Collections

を挿入しないと

No MonoBehaviour

のエラーメッセージが出ます。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

frodo821

2020/05/23 17:23 編集

追記依頼です。 1. スクリプトの配置パスを記入してください(例: Assets/scripts/の下に配置、など) 2. スクリプトのファイル名を、大文字小文字正しく記入してください。 質問に関する質問です。オブジェクトにコンポーネントとして追加しようとしませんでしたか? > Assets\Editor\object2terrain.cs このパスは大文字小文字含めても正しいですか?
guest

回答1

0

すっごい迷走していると思います。

Assets\Editor\object2terrain.cs(1,1):error CS1056: Unexpected character"

これは object2terrain.cs というファイルの一行目の先頭に「期待しない文字」が入っていると言っています。( " はコピペミスで入り込んだと理解しています)
「期待しない文字」が「見えない文字、あるいは文字化けしている」かもしれません。なので、一行目を丸ごと Shift + ↓ で選んで削除し、その行だけ入力しなおして、ファイルを保存します。
そうしたら「そのエラー」は消えるはずです。
ただ、他のエラーが改めて出てくるかもしれませんので、出てきてしまったら一つ一つ対処してください。

投稿2020/07/21 12:29

bboydaisuke

総合スコア5277

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問