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

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

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

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

Unity3D

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

Unity

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

Q&A

0回答

1219閲覧

UnityのスタンダードアセットのVehicleのWaypointCircuit内のメソッドを呼び出すことができない。

monchi_3

総合スコア0

C#

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

Unity3D

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

Unity

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

0グッド

0クリップ

投稿2020/06/28 17:05

前提・実現したいこと

今、学校の課題で自動運転のシミュレータを作ろうと試行錯誤しています。
そこでUnityStandardAssetsの中のVehicleを用いて目的地まで自動で走行するプログラムを制作しようとしました。
その際、CarWaypointBasedというプレハブを用いて実現しようとプログラムを考えました。

###プログラムの構想
走行する都市の全体にルートを示すオブジェクトを置いています。
そのオブジェクトを走行するルートに合わせ、WaypointCircuitスクリプトをアタッチしたオブジェクトの子オブジェクトにし、
その後、WaypointCircuitスクリプトでオブジェクトをたどるルートを作成。
そのルートを車が走行してたどるというプログラムをまず組もうと思ったのですが
WaypointCircuitスクリプトとは別のスクリプトから子オブジェクトをたどるルートを作成するメソッドを呼び出す方法がわかりません。
スクリプト上でなければ”Assign using All child object”ボタンをインスペクタの中で押すだけでできたのですがボタンを押した時に呼び出されるメソッドを呼び出すことができません。
どなたか方法を知っているかたがいらっしゃれば教えていただけると幸いです。

teratailでの質問ははじめてなので拙い部分がたくさんあると思いますが指摘していただければ改善しますので最後まで目を通していただけるとありがたいです。

該当のソースコード

C#

1namespace UnityStandardAssets.Utility.Inspector 2{ 3#if UNITY_EDITOR 4 [CustomPropertyDrawer(typeof (WaypointCircuit.WaypointList))] 5 public class WaypointListDrawer : PropertyDrawer 6 { 7 private float lineHeight = 18; 8 private float spacing = 4; 9 public int push = 0; 10 11 public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) 12 { 13 EditorGUI.BeginProperty(position, label, property); 14 15 float x = position.x; 16 float y = position.y; 17 float inspectorWidth = position.width; 18 19 20 21 22 // Draw label 23 24 25 // Don't make child fields be indented 26 var indent = EditorGUI.indentLevel; 27 EditorGUI.indentLevel = 0; 28 29 var items = property.FindPropertyRelative("items"); 30 var titles = new string[] {"Transform", "", "", ""}; 31 var props = new string[] {"transform", "^", "v", "-"}; 32 var widths = new float[] {.7f, .1f, .1f, .1f}; 33 float lineHeight = 18; 34 bool changedLength = false; 35 if (items.arraySize > 0) 36 { 37 for (int i = -1; i < items.arraySize; ++i) 38 { 39 var item = items.GetArrayElementAtIndex(i); 40 41 float rowX = x; 42 for (int n = 0; n < props.Length; ++n) 43 { 44 float w = widths[n]*inspectorWidth; 45 46 // Calculate rects 47 Rect rect = new Rect(rowX, y, w, lineHeight); 48 rowX += w; 49 50 if (i == -1) 51 { 52 EditorGUI.LabelField(rect, titles[n]); 53 } 54 else 55 { 56 if (n == 0) 57 { 58 EditorGUI.ObjectField(rect, item.objectReferenceValue, typeof (Transform), true); 59 } 60 else 61 { 62 if (GUI.Button(rect, props[n])) 63 { 64 switch (props[n]) 65 { 66 case "-": 67 items.DeleteArrayElementAtIndex(i); 68 items.DeleteArrayElementAtIndex(i); 69 changedLength = true; 70 break; 71 case "v": 72 if (i > 0) 73 { 74 items.MoveArrayElement(i, i + 1); 75 } 76 break; 77 case "^": 78 if (i < items.arraySize - 1) 79 { 80 items.MoveArrayElement(i, i - 1); 81 } 82 break; 83 } 84 } 85 } 86 } 87 } 88 89 y += lineHeight + spacing; 90 if (changedLength) 91 { 92 break; 93 } 94 } 95 } 96 else 97 { 98 // add button 99 var addButtonRect = new Rect((x + position.width) - widths[widths.Length - 1]*inspectorWidth, y, 100 widths[widths.Length - 1]*inspectorWidth, lineHeight); 101 if (GUI.Button(addButtonRect, "+")) 102 { 103 items.InsertArrayElementAtIndex(items.arraySize); 104 } 105 106 y += lineHeight + spacing; 107 } 108 109 // add all button 110 var addAllButtonRect = new Rect(x, y, inspectorWidth, lineHeight); 111 if (GUI.Button(addAllButtonRect, "Assign using all child objects")) 112 { 113 114 var circuit = property.FindPropertyRelative("circuit").objectReferenceValue as WaypointCircuit; 115 var children = new Transform[circuit.transform.childCount]; 116 int n = 0; 117 foreach (Transform child in circuit.transform) 118 { 119 children[n++] = child; 120 } 121 Array.Sort(children, new TransformNameComparer()); 122 circuit.waypointList.items = new Transform[children.Length]; 123 for (n = 0; n < children.Length; ++n) 124 { 125 circuit.waypointList.items[n] = children[n]; 126 } 127 } 128 y += lineHeight + spacing; 129 130 // rename all button 131 var renameButtonRect = new Rect(x, y, inspectorWidth, lineHeight); 132 if (GUI.Button(renameButtonRect, "Auto Rename numerically from this order")) 133 { 134 var circuit = property.FindPropertyRelative("circuit").objectReferenceValue as WaypointCircuit; 135 int n = 0; 136 foreach (Transform child in circuit.waypointList.items) 137 { 138 child.name = "Waypoint " + (n++).ToString("000"); 139 } 140 } 141 y += lineHeight + spacing; 142 143 144 // Set indent back to what it was 145 EditorGUI.indentLevel = indent; 146 EditorGUI.EndProperty(); 147 } 148 149 150 public override float GetPropertyHeight(SerializedProperty property, GUIContent label) 151 { 152 SerializedProperty items = property.FindPropertyRelative("items"); 153 float lineAndSpace = lineHeight + spacing; 154 return 40 + (items.arraySize*lineAndSpace) + lineAndSpace; 155 } 156 157 158 // comparer for check distances in ray cast hits 159 public class TransformNameComparer : IComparer 160 { 161 public int Compare(object x, object y) 162 { 163 return ((Transform) x).name.CompareTo(((Transform) y).name); 164 } 165 } 166 167 168 169 } 170

試したこと

WaypointCircuitの中の”Assign using All child object”ボタンを押したときに実行されるメソッドのif文の条件の中にbool型の変数を追加しその変数を別スクリプトから操作し実行しようとしてみましたが変数を操作できませんでした。

WaypointCircuitの子オブジェクトのtransformを格納する構造体の配列の中に直接子オブジェクトの情報を別スクリプトから入れようとしてみましたがそれも情報を代入できずダメでした。

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

Unity2018.4.23f1(64bit)

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

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

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

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

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

bboydaisuke

2020/07/04 21:29 編集

> スクリプト上でなければ”Assign using All child object”ボタンをインスペクタの中で押すだけでできたのですがボタンを押した時に呼び出されるメソッドを呼び出すことができません。 ”Assign using All child object” ボタンが押された時の処理、というものはありますが、貼り付けているコードは「ボタンを押した時に呼び出されるメソッド」でもボタンを押した時に実行される処理でもないですね。 また、貼り付けてあるコードはエディタ拡張と言ってシーン編集時のみ動きます。実行中には動かないものです。 unity エディタ拡張でググったりしてエディタ拡張について知るとどうしたらいいかわかると思います。
bboydaisuke

2020/07/04 21:25

目的地まで自動走行させたいだけなら Navmesh について調べて Navmesh Agent を車っぽく設定する方が楽だと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問