Skip to content

Instantly share code, notes, and snippets.

@TAK-EMI
Last active August 29, 2015 14:07
Show Gist options
  • Save TAK-EMI/ec2f895ea4127be7a614 to your computer and use it in GitHub Desktop.
Save TAK-EMI/ec2f895ea4127be7a614 to your computer and use it in GitHub Desktop.
Unity用Editorスクリプト。移動回転拡縮を同時にできるハンドルを表示します。Editorフォルダに入れてください。コメントにunitypackageも用意しました。質問等あればこちらまでどうぞ。https://twitter.com/TAK_EMI
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(Transform))]
public class TAK_Handle : Editor
{
static bool viewFlag = true;
bool ctrlFlag = false;
Vector3 preScale = Vector3.one;
[MenuItem("TAK_Handle/view %#w")]
static void view()
{
viewFlag = true;
return;
}
[MenuItem("TAK_Handle/view %#w", true)]
static bool isView()
{
return !viewFlag;
}
[MenuItem("TAK_Handle/hide %#q")]
static void hide()
{
viewFlag = false;
return;
}
[MenuItem("TAK_Handle/hide %#q", true)]
static bool isHide()
{
return viewFlag;
}
public override void OnInspectorGUI()
{
//this.DrawDefaultInspector();
Transform trans = this.target as Transform;
trans.localPosition = EditorGUILayout.Vector3Field("Position", trans.localPosition);
trans.rotation = Quaternion.Euler(EditorGUILayout.Vector3Field("Rotation", trans.eulerAngles));
trans.localScale = EditorGUILayout.Vector3Field("Scale", trans.localScale);
EditorUtility.SetDirty(this.target);
return;
}
void OnSceneGUI()
{
if (!viewFlag || (Tools.viewTool == ViewTool.Orbit))
return;
Transform trans = this.target as Transform;
Event e = Event.current;
if (e.type == EventType.mouseDrag)
Undo.RecordObject(this.target, "Transform Object");
float defLeng = HandleUtility.GetHandleSize(Vector3.left);
Vector3 pos, scale;
pos = trans.position;
scale = trans.localScale;
Quaternion rot = trans.rotation;
rot = trans.rotation = Handles.RotationHandle(rot, pos);
pos = Handles.FreeMoveHandle(pos, rot, defLeng * 0.4f, pos, Handles.RectangleCap);
pos = trans.position = Handles.PositionHandle(pos, rot);
bool ctrl = this.ctrlFlag;
if (e.type == EventType.keyDown && (e.keyCode == KeyCode.LeftControl || e.keyCode == KeyCode.RightControl))
ctrl = this.ctrlFlag = true;
else if (e.type == EventType.keyUp && (e.keyCode == KeyCode.LeftControl || e.keyCode == KeyCode.RightControl))
ctrl = this.ctrlFlag = false;
if (ctrl)
{
Vector3 preScale = this.preScale;
if (e.type == EventType.mouseDown && e.button == 0)
preScale = this.preScale = scale;
Vector3 tscale = scale;
tscale = Handles.ScaleHandle(tscale, pos, rot, -defLeng);
Vector3 diff = tscale - scale;
if (diff.x != 0.0f)
{
float pow = tscale.x / preScale.x;
trans.localScale = new Vector3(preScale.x, preScale.y * pow, preScale.z * pow);
}
else if (diff.y != 0.0f)
{
float pow = tscale.y / preScale.y;
trans.localScale = new Vector3(preScale.x * pow, preScale.y, preScale.z * pow);
}
else if (diff.z != 0.0f)
{
float pow = tscale.z / preScale.z;
trans.localScale = new Vector3(preScale.x * pow, preScale.y * pow, preScale.z);
}
}
else
scale = trans.localScale = Handles.ScaleHandle(scale, pos, rot, -defLeng);
return;
}
}
@TAK-EMI
Copy link
Author

TAK-EMI commented Oct 2, 2014

ちょっと、しばらく改修できそうにないのと、長くなってきたので、コメントを整理しました。
unitypackageのダウンロードはこちら
Ctrlキーを押しながら拡縮ハンドルを操作すると「2軸同時操作」になります。

大体修正しました。
現在はローカル座標系にしか対応させてないです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment