【Unity】マップなどの配置ツール
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEditor; | |
using UnityEngine; | |
//================================================================================ | |
/// <summary> | |
/// 配置ツール | |
/// </summary> | |
//================================================================================ | |
public class MapTools : EditorWindow | |
{ | |
//================================================================================ | |
/* 定数 */ | |
#region -- 定数 | |
const string TOOL_NAME = "MapTools"; | |
#endregion | |
//================================================================================ | |
/* フィールド */ | |
#region -- フィールド | |
/// <summary> | |
/// スクロールの位置 | |
/// </summary> | |
Vector2 mScrollPos = Vector2.zero; | |
/// <summary> | |
/// 相対移動値 | |
/// </summary> | |
float mChangePos = 0.0f; | |
/// <summary> | |
/// 相対回転値 | |
/// </summary> | |
float mChangeRot = 0.0f; | |
/// <summary> | |
/// 方向のタイプ | |
/// </summary> | |
enum eTypeDirection | |
{ | |
pX, | |
mX, | |
pY, | |
mY, | |
pZ, | |
mZ, | |
} | |
#endregion | |
//================================================================================ | |
/* 標準関数 */ | |
#region -- 標準関数 | |
//================================================================================ | |
/// <summary> | |
/// メニューのウィンドウに追加 | |
/// </summary> | |
[MenuItem("Custom/Prefabs/" + TOOL_NAME)] | |
public static void OpenWindow() { | |
EditorWindow.GetWindow<MapTools>(TOOL_NAME); | |
} | |
//================================================================================ | |
/// <summary> | |
/// メイン描画処理 | |
/// </summary> | |
void OnGUI() { | |
mScrollPos = EditorGUILayout.BeginScrollView(mScrollPos, GUI.skin.scrollView); | |
{ | |
if (CommonEditorTools.DrawHeader("Tools Action")) { | |
CommonEditorTools.BeginContents(false); | |
UIPositionEx(); | |
UIAddPosition(); | |
UIAddPositionParts(); | |
UIAddRotation(); | |
UISetScale(); | |
UIReversal(); | |
CommonEditorTools.EndContents(); | |
} | |
if (CommonEditorTools.DrawHeader("Sub Tools")) { | |
CommonEditorTools.BeginContents(false); | |
if (GUILayout.Button("SizeCheck")) { | |
Debug.Log(CommonEditorTools.GetObjSize()); | |
} | |
CommonEditorTools.EndContents(); | |
} | |
} | |
EditorGUILayout.EndScrollView(); | |
} | |
#endregion | |
//================================================================================ | |
/* Private関数 */ | |
#region -- Private関数 | |
//================================================================================ | |
/// <summary> | |
/// 選択オブジェクトに位置を設定 | |
/// </summary> | |
/// <param name="trans"></param> | |
void SetPosition(Vector3 trans) { | |
foreach (GameObject gameObj in Selection.objects) { | |
Transform transform = gameObj.transform; | |
Undo.RecordObject(transform, TOOL_NAME + " - Position"); | |
transform.localPosition = trans; | |
} | |
} | |
//================================================================================ | |
/// <summary> | |
/// 選択オブジェクトに位置の加算処理 | |
/// </summary> | |
/// <param name="trans"></param> | |
void AddPosition(Vector3 trans) { | |
foreach (GameObject gameObj in Selection.objects) { | |
Transform transform = gameObj.transform; | |
Undo.RecordObject(transform, TOOL_NAME + " - Position"); | |
transform.localPosition += trans; | |
} | |
} | |
//================================================================================ | |
/// <summary> | |
/// 選択オブジェクトに角度の加算処理 | |
/// </summary> | |
/// <param name="trans"></param> | |
void AddRotate(Vector3 trans) { | |
foreach (GameObject gameObj in Selection.objects) { | |
Transform transform = gameObj.transform; | |
Undo.RecordObject(transform, TOOL_NAME + " - Rotation"); | |
transform.localEulerAngles += trans; | |
} | |
} | |
//================================================================================ | |
/// <summary> | |
/// 選択オブジェクトにスケールの設定処理 | |
/// </summary> | |
/// <param name="trans"></param> | |
void SetScale(Vector3 trans) { | |
foreach (GameObject gameObj in Selection.objects) { | |
Transform transform = gameObj.transform; | |
Undo.RecordObject(transform, TOOL_NAME + " - Scale"); | |
transform.localScale = trans; | |
} | |
} | |
//================================================================================ | |
/// <summary> | |
/// 選択オブジェクトにスケールの乗算処理 | |
/// </summary> | |
/// <param name="trans"></param> | |
void MultiScale(Vector3 trans) { | |
foreach (GameObject gameObj in Selection.objects) { | |
Transform transform = gameObj.transform; | |
Undo.RecordObject(transform, TOOL_NAME + " - Scale"); | |
transform.localScale = Vector3.Scale(transform.localScale, trans); | |
} | |
} | |
//================================================================================ | |
/// <summary> | |
/// 選択オブジェクトのパーツ幅移動 | |
/// </summary> | |
/// <param name="type"></param> | |
void AddPositionParts(eTypeDirection type) { | |
foreach (GameObject gameObj in Selection.objects) { | |
Transform transform = gameObj.transform; | |
Vector3 trans = new Vector3(); | |
float x, y, z; | |
Undo.RecordObject(transform, TOOL_NAME + " - Scale"); | |
switch (type) { | |
case eTypeDirection.pX: | |
x = CommonEditorTools.GetObjSize().x; | |
trans = new Vector3(x, 0, 0); | |
break; | |
case eTypeDirection.mX: | |
x = CommonEditorTools.GetObjSize().x; | |
trans = new Vector3(-x, 0, 0); | |
break; | |
case eTypeDirection.pY: | |
y = CommonEditorTools.GetObjSize().y; | |
trans = new Vector3(0, y, 0); | |
break; | |
case eTypeDirection.mY: | |
y = CommonEditorTools.GetObjSize().y; | |
trans = new Vector3(0, -y, 0); | |
break; | |
case eTypeDirection.pZ: | |
z = CommonEditorTools.GetObjSize().z; | |
trans = new Vector3(0, 0, z); | |
break; | |
case eTypeDirection.mZ: | |
z = CommonEditorTools.GetObjSize().z; | |
trans = new Vector3(0, 0, -z); | |
break; | |
} | |
/* サイズが取得出来たか判定 */ | |
if (!float.IsNaN(trans.x) && !float.IsNaN(trans.y) && !float.IsNaN(trans.z)) { | |
transform.localPosition += trans; | |
} else { | |
Debug.LogWarning("[" + Selection.activeGameObject.name + "」にはMeshが含まれていません"); | |
} | |
} | |
} | |
//================================================================================ | |
/// <summary> | |
/// 選択オブジェクトを近似値に設定 | |
/// ※複数オブジェ非対応 | |
/// </summary> | |
void SetApproximation() { | |
GameObject target = Selection.activeObject as GameObject; | |
/* 近似値の計算 */ | |
float sizeX = CommonEditorTools.GetObjSize().x; | |
float setX = Mathf.Round(target.transform.position.x / sizeX) * sizeX; | |
float sizeY = CommonEditorTools.GetObjSize().y; | |
float setY = Mathf.Round(target.transform.position.y / sizeY) * sizeY; | |
float sizeZ = CommonEditorTools.GetObjSize().z; | |
float setZ = Mathf.Round(target.transform.position.z / sizeZ) * sizeZ; | |
Vector3 result = new Vector3(setX, setY, setZ); | |
/* サイズが取得出来たか判定 */ | |
if (!float.IsNaN(result.x) && !float.IsNaN(result.y) && !float.IsNaN(result.z)) { | |
target.transform.position = result; | |
} else { | |
Debug.LogWarning("[" + Selection.activeGameObject.name + "」にはMeshが含まれていません"); | |
} | |
} | |
//================================================================================ | |
/// <summary> | |
/// 選択オブジェが一つか判定 | |
/// </summary> | |
/// <returns></returns> | |
bool CheckSelectionOne() { | |
if (Selection.objects.Length > 1) { | |
return false; | |
} | |
return true; | |
} | |
#endregion | |
//================================================================================ | |
/* UIレイアウト */ | |
#region -- UIレイアウト | |
//================================================================================ | |
/// <summary> | |
/// 【UI】特殊移動関連 | |
/// </summary> | |
void UIPositionEx() { | |
EditorGUILayout.HelpBox("【移動 - 特殊】", MessageType.None); | |
EditorGUILayout.BeginHorizontal(); | |
{ | |
if (GUILayout.Button("原点")) { | |
SetPosition(Vector3.zero); | |
} | |
GUI.enabled = CheckSelectionOne(); | |
{ | |
if (GUILayout.Button("近似値")) { | |
SetApproximation(); | |
} | |
} | |
GUI.enabled = true; | |
} | |
EditorGUILayout.EndHorizontal(); | |
} | |
//================================================================================ | |
/// <summary> | |
/// 【UI】相対移動 | |
/// </summary> | |
void UIAddPosition() { | |
EditorGUILayout.HelpBox("【移動 - 相対】", MessageType.None); | |
mChangePos = EditorGUILayout.FloatField("", mChangePos); | |
EditorGUILayout.BeginHorizontal(); | |
{ | |
if (GUILayout.Button("+X")) { | |
Vector3 trans = new Vector3(mChangePos, 0, 0); | |
AddPosition(trans); | |
} | |
if (GUILayout.Button("-X")) { | |
Vector3 trans = new Vector3(mChangePos * -1, 0, 0); | |
AddPosition(trans); | |
} | |
if (GUILayout.Button("+Y")) { | |
Vector3 trans = new Vector3(0, mChangePos, 0); | |
AddPosition(trans); | |
} | |
if (GUILayout.Button("-Y")) { | |
Vector3 trans = new Vector3(0, mChangePos * -1, 0); | |
AddPosition(trans); | |
} | |
if (GUILayout.Button("+Z")) { | |
Vector3 trans = new Vector3(0, 0, mChangePos); | |
AddPosition(trans); | |
} | |
if (GUILayout.Button("-Z")) { | |
Vector3 trans = new Vector3(0, 0, mChangePos * -1); | |
AddPosition(trans); | |
} | |
} | |
EditorGUILayout.EndHorizontal(); | |
} | |
//================================================================================ | |
/// <summary> | |
/// 【UI】パーツ幅移動 | |
/// </summary> | |
void UIAddPositionParts() { | |
EditorGUILayout.HelpBox("【移動 - パーツ幅】", MessageType.None); | |
EditorGUILayout.BeginHorizontal(); | |
{ | |
GUI.enabled = CheckSelectionOne(); | |
{ | |
if (GUILayout.Button("+X")) { | |
AddPositionParts(eTypeDirection.pX); | |
} | |
if (GUILayout.Button("-X")) { | |
AddPositionParts(eTypeDirection.mX); | |
} | |
if (GUILayout.Button("+Y")) { | |
AddPositionParts(eTypeDirection.pY); | |
} | |
if (GUILayout.Button("-Y")) { | |
AddPositionParts(eTypeDirection.mY); | |
} | |
if (GUILayout.Button("+Z")) { | |
AddPositionParts(eTypeDirection.pZ); | |
} | |
if (GUILayout.Button("-Z")) { | |
AddPositionParts(eTypeDirection.mZ); | |
} | |
} | |
GUI.enabled = true; | |
} | |
EditorGUILayout.EndHorizontal(); | |
} | |
//================================================================================ | |
/// <summary> | |
/// 【UI】相対回転 | |
/// </summary> | |
void UIAddRotation() { | |
EditorGUILayout.HelpBox("【回転 - 相対】", MessageType.None); | |
/* 指定値 */ | |
mChangeRot = EditorGUILayout.FloatField("", mChangeRot); | |
EditorGUILayout.BeginHorizontal(); | |
{ | |
if (GUILayout.Button("+X")) { | |
Vector3 trans = new Vector3(mChangePos, 0, 0); | |
AddPosition(trans); | |
} | |
if (GUILayout.Button("-X")) { | |
Vector3 trans = new Vector3(mChangePos * -1, 0, 0); | |
AddPosition(trans); | |
} | |
if (GUILayout.Button("+Y")) { | |
Vector3 trans = new Vector3(0, mChangePos, 0); | |
AddPosition(trans); | |
} | |
if (GUILayout.Button("-Y")) { | |
Vector3 trans = new Vector3(0, mChangePos * -1, 0); | |
AddPosition(trans); | |
} | |
if (GUILayout.Button("+Z")) { | |
Vector3 trans = new Vector3(0, 0, mChangePos); | |
AddPosition(trans); | |
} | |
if (GUILayout.Button("-Z")) { | |
Vector3 trans = new Vector3(0, 0, mChangePos * -1); | |
AddPosition(trans); | |
} | |
EditorGUILayout.EndHorizontal(); | |
/* 汎用回転 */ | |
EditorGUILayout.BeginHorizontal(); | |
if (GUILayout.Button("+90°")) { | |
Vector3 trans = new Vector3(0, 90.0f, 0); | |
AddRotate(trans); | |
} | |
if (GUILayout.Button("-90°")) { | |
Vector3 trans = new Vector3(0, -90.0f, 0); | |
AddRotate(trans); | |
} | |
if (GUILayout.Button("+180°")) { | |
Vector3 trans = new Vector3(0, 180.0f, 0); | |
AddRotate(trans); | |
} | |
} | |
EditorGUILayout.EndHorizontal(); | |
} | |
//================================================================================ | |
/// <summary> | |
/// 【UI】スケール | |
/// </summary> | |
void UISetScale() { | |
EditorGUILayout.HelpBox("【スケール】", MessageType.None); | |
EditorGUILayout.BeginHorizontal(); | |
{ | |
if (GUILayout.Button("x0.5")) { | |
Vector3 trans = new Vector3(0.5f, 0.5f, 0.5f); | |
SetScale(trans); | |
} | |
if (GUILayout.Button("x1")) { | |
Vector3 trans = Vector3.one; | |
SetScale(trans); | |
} | |
if (GUILayout.Button("x2")) { | |
Vector3 trans = new Vector3(2.0f, 2.0f, 2.0f); | |
SetScale(trans); | |
} | |
if (GUILayout.Button("x4")) { | |
Vector3 trans = new Vector3(4.0f, 4.0f, 4.0f); | |
SetScale(trans); | |
} | |
} | |
EditorGUILayout.EndHorizontal(); | |
} | |
//================================================================================ | |
/// <summary> | |
/// 【UI】反転 | |
/// </summary> | |
void UIReversal() { | |
EditorGUILayout.HelpBox("【反転】", MessageType.None); | |
EditorGUILayout.BeginHorizontal(); | |
{ | |
if (GUILayout.Button("X反転")) { | |
Vector3 trans = new Vector3(-1.0f, 1.0f, 1.0f); | |
MultiScale(trans); | |
} | |
if (GUILayout.Button("Y反転")) { | |
Vector3 trans = new Vector3(1.0f, -1.0f, 1.0f); | |
MultiScale(trans); | |
} | |
if (GUILayout.Button("Z反転")) { | |
Vector3 trans = new Vector3(1.0f, 1.0f, -1.0f); | |
MultiScale(trans); | |
} | |
} | |
EditorGUILayout.EndHorizontal(); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment