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

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

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

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

Unity

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

Q&A

1回答

378閲覧

エディタ拡張で生成したobjectにstringを付与する

ryom_a_rys36

総合スコア2

C#

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

Unity

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

0グッド

0クリップ

投稿2021/07/07 00:54

それぞれのPrefabにstringを付与したい

エディタ拡張である決まったPrefabを簡単に出現させます。
それぞれにstringデータを付与したいのですがどうやったら良いかわかりません。(それぞれ生成したObjectのInspectorに記録されてほしい)
Prefabを作成するごとにstringを手動で打ち込み、Createでそれが生成される仕組みにしたいです。
教えてください。
(付与したいstringは location と discription です。)

また、Createを押したらこのWindowが閉じるコードもできたら教えていただきたいです。

現在のコード

c#

1using UnityEngine; 2using UnityEditor; 3using System.Collections; 4 5public class CreateBuoys : EditorWindow 6 7{ 8 9 private GameObject cubePrefab; 10 string location; 11 string discription; 12 13 [MenuItem("Editor/PrefabSet")] 14 static void Init() 15 { 16 EditorWindow.GetWindow<CreateBuoys>(true); 17 } 18 19 void OnEnable() 20 { 21 22 } 23 24 void OnSelectionChange() 25 { 26 27 } 28 29 void OnGUI() 30 { 31 try 32 { 33 34 location = EditorGUILayout.TextField("Location", location); 35 discription = EditorGUILayout.TextField("Discription", discription); 36 37 GUILayout.Label("", EditorStyles.boldLabel); 38 if (GUILayout.Button("Create")) 39 Place(); 40 } 41 catch (System.FormatException) { } 42 } 43 44 static void Place() 45 { 46 GameObject cubePrefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Cube.prefab"); 47 48 GameObject p = PrefabUtility.InstantiatePrefab(cubePrefab) as GameObject; 49 p.transform.position = new Vector3(0, 0, 0); 50 } 51}

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

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

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

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

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

guest

回答1

0

Prefab自体に管理させるなら、なんかクラスでも作ってそれアタッチしとくんじゃダメですかね?

PrefabInfo.cs

cs

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PrefabInfo : MonoBehaviour { 6 public string Location; 7 public string Discription; 8} 9

CreateBuoys.cs

cs

1using UnityEngine; 2using UnityEditor; 3using System.Collections; 4 5public class CreateBuoys : EditorWindow { 6 7 private GameObject cubePrefab; 8 string location; 9 string discription; 10 11 [MenuItem("Editor/PrefabSet")] 12 static void Init() { 13 EditorWindow.GetWindow<CreateBuoys>(true); 14 } 15 16 void OnEnable() { 17 18 } 19 20 void OnSelectionChange() { 21 22 } 23 24 void OnGUI() { 25 try { 26 27 location = EditorGUILayout.TextField("Location", location); 28 discription = EditorGUILayout.TextField("Discription", discription); 29 30 GUILayout.Label("", EditorStyles.boldLabel); 31 if (GUILayout.Button("Create")) 32 Place(); 33 } catch (System.FormatException) { } 34 } 35 36 void Place() { 37 GameObject cubePrefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Cube.prefab"); 38 39 GameObject p = PrefabUtility.InstantiatePrefab(cubePrefab) as GameObject; 40 p.transform.position = new Vector3(0, 0, 0); 41 42 var _info = p.AddComponent<PrefabInfo>(); 43 _info.Location = location; 44 _info.Discription = discription; 45 46 this.Close(); 47 } 48}

投稿2021/07/07 07:35

hogefugapiyo

総合スコア3302

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問