エディター拡張4(Undo機能の実装)
Undo機能の実装
using UnityEngine;
using UnityEditor;
public class SetObject : EditorWindow
{
private Vector3 selectPos;
private Vector3 rot;
private string prefabName;
// ★追加
private int yokoSize;
private int tateSize;
[MenuItem("SetObject/Set")]
static void WindowOpen()
{
GetWindow<SetObject>();
}
private void OnGUI()
{
prefabName = EditorGUILayout.TextField("①オブジェクトの選択", prefabName);
if (GUILayout.Button("②設置場所の選択"))
{
selectPos = Selection.activeTransform.position;
}
// ★追加
yokoSize = EditorGUILayout.IntField("横に何個並べるか?", yokoSize);
tateSize = EditorGUILayout.IntField("縦に何個並べるか?", tateSize);
rot = EditorGUILayout.Vector3Field("③向きの選択", rot);
if (GUILayout.Button("④配置"))
{
// ★追加
for (int i = 0; i < yokoSize; i++)
{
for (int j = 0; j < tateSize; j++)
{
GameObject prefab = (GameObject)Resources.Load(prefabName);
GameObject clone = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
clone.transform.position = new Vector3(selectPos.x + i, selectPos.y, selectPos.z + j); // ★一部追加
clone.transform.localRotation = Quaternion.Euler(rot.x, rot.y, rot.z);
// ★★追加(Undo機能の実装)
Undo.RegisterCreatedObjectUndo(clone, "Create Object");
}
}
}
if (GUILayout.Button("終了"))
{
Close();
}
}
}
【2019版】TowerD Ⅱ(基礎/全4回)
1 | エディター拡張1(タイルマップ作製支援ツール) |
2 | エディター拡張2(オブジェクト配置支援ツール) |
3 | エディター拡張3(オブジェクト配置支援ツールの改良) |
4 | エディター拡張4(Undo機能の実装) |
Undo機能の実装
using UnityEngine;
using UnityEditor;
public class SetObject : EditorWindow
{
private Vector3 selectPos;
private Vector3 rot;
private string prefabName;
// ★追加
private int yokoSize;
private int tateSize;
[MenuItem("SetObject/Set")]
static void WindowOpen()
{
GetWindow<SetObject>();
}
private void OnGUI()
{
prefabName = EditorGUILayout.TextField("①オブジェクトの選択", prefabName);
if (GUILayout.Button("②設置場所の選択"))
{
selectPos = Selection.activeTransform.position;
}
// ★追加
yokoSize = EditorGUILayout.IntField("横に何個並べるか?", yokoSize);
tateSize = EditorGUILayout.IntField("縦に何個並べるか?", tateSize);
rot = EditorGUILayout.Vector3Field("③向きの選択", rot);
if (GUILayout.Button("④配置"))
{
// ★追加
for (int i = 0; i < yokoSize; i++)
{
for (int j = 0; j < tateSize; j++)
{
GameObject prefab = (GameObject)Resources.Load(prefabName);
GameObject clone = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
clone.transform.position = new Vector3(selectPos.x + i, selectPos.y, selectPos.z + j); // ★一部追加
clone.transform.localRotation = Quaternion.Euler(rot.x, rot.y, rot.z);
// ★★追加(Undo機能の実装)
Undo.RegisterCreatedObjectUndo(clone, "Create Object");
}
}
}
if (GUILayout.Button("終了"))
{
Close();
}
}
}
エディター拡張4(Undo機能の実装)