エディター拡張2(オブジェクト配置支援ツール)

オブジェクトの配置
// 変更
using UnityEngine;
using UnityEditor;
public class SetObject : EditorWindow // ←変更
{
private Vector3 selectPos;
private Vector3 rot;
private string prefabName;
// MenuItem("文字/文字")・・・>ここの表記がツールバーに出現する。
[MenuItem("SetObject/Set")]
static void WindowOpen() // staticを付けないとツールバーに出現しない(staticを付けるとどのシーンでも出現する)
{
GetWindow<SetObject>();
}
// OnGUI()・・・>コードでUIを構築していく。
private void OnGUI()
{
prefabName = EditorGUILayout.TextField("①オブジェクトの選択", prefabName);
if(GUILayout.Button("②設置場所の選択"))
{
selectPos = Selection.activeTransform.position;
}
rot = EditorGUILayout.Vector3Field("③向きの選択", rot);
if(GUILayout.Button("④配置"))
{
GameObject prefab = (GameObject)Resources.Load(prefabName);
GameObject clone = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
clone.transform.position = new Vector3(selectPos.x, selectPos.y, selectPos.z);
clone.transform.localRotation = Quaternion.Euler(rot.x, rot.y, rot.z);
}
if(GUILayout.Button("終了"))
{
Close();
}
}
}








【2019版】TowerD Ⅱ(基礎/全4回)
1 | エディター拡張1(タイルマップ作製支援ツール) |
2 | エディター拡張2(オブジェクト配置支援ツール) |
3 | エディター拡張3(オブジェクト配置支援ツールの改良) |
4 | エディター拡張4(Undo機能の実装) |

オブジェクトの配置
// 変更
using UnityEngine;
using UnityEditor;
public class SetObject : EditorWindow // ←変更
{
private Vector3 selectPos;
private Vector3 rot;
private string prefabName;
// MenuItem("文字/文字")・・・>ここの表記がツールバーに出現する。
[MenuItem("SetObject/Set")]
static void WindowOpen() // staticを付けないとツールバーに出現しない(staticを付けるとどのシーンでも出現する)
{
GetWindow<SetObject>();
}
// OnGUI()・・・>コードでUIを構築していく。
private void OnGUI()
{
prefabName = EditorGUILayout.TextField("①オブジェクトの選択", prefabName);
if(GUILayout.Button("②設置場所の選択"))
{
selectPos = Selection.activeTransform.position;
}
rot = EditorGUILayout.Vector3Field("③向きの選択", rot);
if(GUILayout.Button("④配置"))
{
GameObject prefab = (GameObject)Resources.Load(prefabName);
GameObject clone = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
clone.transform.position = new Vector3(selectPos.x, selectPos.y, selectPos.z);
clone.transform.localRotation = Quaternion.Euler(rot.x, rot.y, rot.z);
}
if(GUILayout.Button("終了"))
{
Close();
}
}
}








エディター拡張2(オブジェクト配置支援ツール)