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

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

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

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

Unity

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

Q&A

解決済

2回答

1196閲覧

Unity Editor拡張でスクリプトに書いてある関数を指定したい

退会済みユーザー

退会済みユーザー

総合スコア0

C#

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

Unity

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

0グッド

0クリップ

投稿2020/03/03 14:05

敵のステータスや攻撃パターンを簡単に設定できるツールを製作しています。
UnityEventを使うと関数を指定できますよね?このように指定できるものを作りたいのですが、スクリプト内の複数の関数を取ってきてList変数に保存しておく方法(関数を変数のように扱う)がわからないので知りたいです。

/// 渦巻き状 /// </summary> /// <returns></returns> private IEnumerator RollingShot() { while (true) { for(float i = 1; i <= 75; i+=0.5f) { for (float j = i; j < 360+i; j +=i) { var bullet1 = E_bullet.GetE_bullet(); bullet1.transform.rotation = Quaternion.Euler(bullet1.transform.rotation.x, bullet1.transform.rotation.y,j); bullet1.transform.position = this.transform.position; bullet1.SetActive(true); yield return new WaitForSeconds(0.01f); } } } } /// <summary> /// ランダム /// </summary> /// <returns></returns> private IEnumerator RandomShot() { while (true) { if (!Target.activeSelf) { StopShot(); } var bullet1 = E_bullet.GetE_bullet(); bullet1.transform.rotation = Quaternion.Euler(bullet1.transform.rotation.x, bullet1.transform.rotation.y, Random.Range(0,360)); bullet1.transform.position = this.transform.position; bullet1.SetActive(true); yield return null; } } /// <summary> /// ホーミング /// </summary> /// <returns></returns> private IEnumerator HomingShot() { Target = GameObject.FindWithTag("player"); while (true) { if (!Target.activeSelf) { StopShot(); } for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { var bullet1 = E_bullet.GetE_bullet(); PosX = this.transform.position.x - Target.transform.position.x; PosY = this.transform.position.y - Target.transform.position.y; bullet1.transform.rotation = Quaternion.Euler(bullet1.transform.rotation.x, bullet1.transform.rotation.y, -Mathf.Atan2(PosX, PosY) * Mathf.Rad2Deg); bullet1.transform.position = this.transform.position; bullet1.SetActive(true); yield return new WaitForSeconds(0.025f); } yield return new WaitForSeconds(0.05f); } yield return new WaitForSeconds(0.1f); } } /// <summary> /// つむじ /// </summary> /// <returns></returns> private IEnumerator SpecialShot() { while (true) { if (!Target.activeSelf) { StopShot(); } for (int i = 0; i < 360; i += 40) { for(int j = 0; j < 360; j += 90) { var bullet1 = E_bullet.GetE_bullet(); bullet1.transform.rotation = Quaternion.Euler(bullet1.transform.rotation.x, bullet1.transform.rotation.y, j+i); bullet1.transform.position = this.transform.position; bullet1.SetActive(true); yield return null; } yield return new WaitForSeconds(0.05f); } } } /// <summary> /// 円形 /// </summary> /// <returns></returns> private IEnumerator RoundShot() { while (true) { if (!Target.activeSelf) { StopShot(); } for (int i = 2; i < 5; i++) { for (int j = i; j < 360+i; j += 4) { var bullet1 = E_bullet.GetE_bullet(); bullet1.transform.rotation = Quaternion.Euler(bullet1.transform.rotation.x, bullet1.transform.rotation.y, j); bullet1.transform.position = this.transform.position; bullet1.SetActive(true); } yield return new WaitForSeconds(0.2f); } yield return new WaitForSeconds(1.0f); } } 追加予定の攻撃処理、これを変数で扱いたい

using

1using System.Collections.Generic; 2using System.IO; 3using UnityEngine; 4using UnityEngine.Events; 5 6public class BossInfo : MonoBehaviour 7{ 8 [SerializeField, Tooltip("体力/現在値")] 9 private float m_hp; 10 [SerializeField, Tooltip("体力/最大値")] 11 private float m_hp_max; 12 [SerializeField, Tooltip("ウェーブ数")] 13 private int m_wave; 14 public float hp { get { return m_hp; } set { m_hp = value; } } 15 public float max_hp { get { return m_hp_max; } set { m_hp_max = value; } } 16 public int wave { get {return m_wave; } set { m_wave = value; } } 17} 18敵の情報

using

1using System.Collections.Generic; 2using UnityEngine; 3using UnityEditor; 4 5[CustomEditor(typeof(BossInfo))] 6public class BossEditor : Editor 7{ 8 public override void OnInspectorGUI() 9 { 10 BossInfo boss = target as BossInfo; 11 12 EditorGUILayout.BeginVertical(GUI.skin.box); 13 EditorGUILayout.LabelField("ステータス"); 14 EditorGUILayout.BeginHorizontal(); 15 EditorGUILayout.LabelField("体力/現在値"); 16 boss.hp = EditorGUILayout.FloatField(boss.hp); 17 if (GUILayout.Button("Reset")) 18 { 19 Debug.Log("Reset"); 20 boss.hp = boss.max_hp; 21 } 22 EditorGUILayout.EndHorizontal(); 23 EditorGUILayout.BeginHorizontal(); 24 EditorGUILayout.LabelField("体力/最大値"); 25 boss.max_hp = EditorGUILayout.FloatField(boss.max_hp); 26 EditorGUILayout.EndHorizontal(); 27 EditorGUILayout.EndVertical(); 28 29 if( GUILayout.Button("Attck!!")) 30 { 31 boss.hp -= 1; 32 } 33 34 EditorGUILayout.BeginVertical(GUI.skin.box); 35 EditorGUILayout.LabelField("ウェーブ数"); 36 EditorGUILayout.BeginHorizontal(); 37 boss.wave = EditorGUILayout.IntField(boss.wave); 38 EditorGUILayout.EndHorizontal(); 39 EditorGUILayout.LabelField("攻撃変化"); 40 for (int i = 1; i <= boss.wave; i++) 41 { 42 EditorGUILayout.BeginVertical(GUI.skin.box); 43 EditorGUILayout.LabelField("Wave" + i); 44 //関数またはフォルダから攻撃を選ぶプルダウンを表示 45 //選ばれたらそれぞれの設定可能パラメータの表示 46 EditorGUILayout.EndVertical(); 47 } 48 EditorGUILayout.EndVertical(); 49 50 } 51 52} 53ボスEditor、攻撃を扱いたい部分

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

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

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

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

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

guest

回答2

0

ベストアンサー

using

1using System.Collections; 2using System.Collections.Generic; 3using System.IO; 4using System.Reflection; 5using UnityEngine; 6using UnityEngine.Events; 7 8 9public class BossInfo : MonoBehaviour 10{ 11 [SerializeField, Tooltip("体力/現在値")] 12 private float m_hp; 13 [SerializeField, Tooltip("体力/最大値")] 14 private float m_hp_max; 15 [SerializeField, Tooltip("ウェーブ数")] 16 private int m_wave; 17 public enum ATTACK 18 { 19 RollingShot, 20 RandomShot, 21 HomingShot, 22 SpecialShot, 23 RoundShot, 24 25 } 26 27 public ATTACK attack; 28 public float hp { get { return m_hp; } set { m_hp = value; } } 29 public float max_hp { get { return m_hp_max; } set { m_hp_max = value; } } 30 public int wave { get {return m_wave; } set { m_wave = value; } } 31 32} 33ボス情報、攻撃関数名を追加

using

1using System.Collections.Generic; 2using UnityEngine; 3using UnityEditor; 4 5[CustomEditor(typeof(BossInfo))] 6public class BossEditor : Editor 7{ 8 9 public override void OnInspectorGUI() 10 { 11 12 BossInfo boss = target as BossInfo; 13 14 EditorGUILayout.BeginVertical(GUI.skin.box); 15 EditorGUILayout.LabelField("ステータス"); 16 EditorGUILayout.BeginHorizontal(); 17 EditorGUILayout.LabelField("体力/現在値"); 18 boss.hp = EditorGUILayout.FloatField(boss.hp); 19 if (GUILayout.Button("Reset")) 20 { 21 Debug.Log("Reset"); 22 boss.hp = boss.max_hp; 23 } 24 EditorGUILayout.EndHorizontal(); 25 EditorGUILayout.BeginHorizontal(); 26 EditorGUILayout.LabelField("体力/最大値"); 27 boss.max_hp = EditorGUILayout.FloatField(boss.max_hp); 28 EditorGUILayout.EndHorizontal(); 29 EditorGUILayout.EndVertical(); 30 31 if( GUILayout.Button("Attck!!")) 32 { 33 boss.hp -= 1; 34 } 35 36 EditorGUILayout.BeginVertical(GUI.skin.box); 37 EditorGUILayout.LabelField("ウェーブ数"); 38 EditorGUILayout.BeginHorizontal(); 39 boss.wave = EditorGUILayout.IntField(boss.wave); 40 EditorGUILayout.EndHorizontal(); 41 EditorGUILayout.LabelField("攻撃変化"); 42 for (int i = 1; i <= boss.wave; i++) 43 { 44 EditorGUILayout.BeginVertical(GUI.skin.box); 45 EditorGUILayout.LabelField("Wave" + i); 46 //関数またはフォルダから攻撃を選ぶプルダウンを表示 47 //選ばれたらそれぞれの設定可能パラメータの表示 48 boss.attack =(BossInfo.ATTACK) EditorGUILayout.EnumPopup("攻撃", boss.attack);//追加箇所 49 EditorGUILayout.EndVertical(); 50 } 51 EditorGUILayout.EndVertical(); 52 53 } 54} 55 56Editor側、forの中の処理に不備あり

インスペクターで処理の名前を選択できるようになったのであとは呼び出せるようになったらOKです。ただforの中の処理で一つの値を変えると繰り返し表示したところの値まで変わってしまうので修正が必要です。

投稿2020/03/05 03:59

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

0

RollingShot とか SpecialShot とかをリストに入れたい感じでしょうか

クラスが書かれてないですが、多分こういう感じで呼び出すはずです

Bullet bullet = new Bullet(); StartCoroutine(bullet.RollingShot())

ここで、bulletという変数が必要になります
とりあえずラムダ式使ってしまうのが楽かと思います

List<Func<Bullet, IEnumerator>> funclist = new List<Func<Bullet, IEnumerator>>(); funclist.Add(n => n.RoundShot()); StartCoroutine(funclist[0](bullet));

投稿2020/03/04 02:40

izmktr

総合スコア2856

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

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

退会済みユーザー

退会済みユーザー

2020/03/04 06:35

ありがとうございます。この方法で可能なのはわかったのですがEditorの方でリストやfuncをどう扱えば良いか調べてもわからないのですがわかりますでしょうか…もし不可能ならコルーチンの名前をEnum で登録してそのEnumをインスペクターから選び選ばれたものをToStringかGetNameで変換し、処理を呼び出す方法に変えようかと考えています。
izmktr

2020/03/04 08:20

UnityEvent でどうですか?
退会済みユーザー

退会済みユーザー

2020/03/04 08:43

MonoBehaviourを継承したスクリプトでは変数にUnityEventを指定すると使えると思うのですが、Editorを継承したスクリプトの場合EditorGUILayout.hoge でインスペクターに表示する必要があると認識しており、hogeにobjectfieldを指定する事で大体のものは表示できるという記事を見ました。ですがtypeofで何を指定したら良いかわからなかったので違うやり方はないだろうかと思い質問させていただきました。なのでUnityEvent では不可能だと考えています。
izmktr

2020/03/04 09:31

大体わかりました 多分探せばありそうですが、私がやってないのでわかりません 今持っている私の知識だったら、文字列で関数をもらってリフレクションを使って呼び出す感じですね
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問