- 平时有时候只想移动物体的中心点,但是每次快捷编辑起来有点麻烦,所以写了个小脚本
- 选中物体后按 shift + m 就可以移动物体,而不影响子问题的位置了
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class MoveRootPointEditor
{
public static bool openMove = false;
[InitializeOnLoadMethod]
public static void OnInit()
{
SceneView.duringSceneGui+=SceneViewOnduringSceneGui;
Selection.selectionChanged+=SelectionChanged;
}
private static void SelectionChanged()
{
openMove = false;
}
[MenuItem("Assets/SceneEditorTool/MoveRootPoint #M")]
public static void OpenCheck()
{
openMove = true;
}
private static void SceneViewOnduringSceneGui(SceneView obj)
{
if (!openMove)
{
return;
}
if (Selection.activeGameObject==null)
{
return;
}
Transform component = Selection.activeGameObject.GetComponent<Transform>();
if (component==null)
{
return;
}
Vector3 delta = Vector3.one*obj.size*0.1f;
Vector3 checkPoint = component.position-delta;
Handles.DrawWireCube(checkPoint,delta);
Vector3 newPoint= Handles.PositionHandle(checkPoint, Quaternion.identity);
if (newPoint!=checkPoint)
{
Undo.RecordObject(component,"MoveRootPoint");
component.position = newPoint + delta;
int componentChildCount = component.childCount;
EditorUtility.SetDirty(component);
for (int i = 0; i < componentChildCount; i++)
{
Transform transform = component.GetChild(i).transform;
Undo.RecordObject(transform,"MoveRootPoint");
transform.position-=newPoint-checkPoint;
EditorUtility.SetDirty(transform);
}
}
}
}
```