纳金网

标题: 将GameObject移动到(0,0,0)[Editor Script的威力] (转载) [打印本页]

作者: 她。    时间: 2012-11-3 11:34
标题: 将GameObject移动到(0,0,0)[Editor Script的威力] (转载)
是不是您已经对每次将大堆的GameObject移动到原点几经感到厌烦了呢?

来看看下面的代码是如何为Unity3d添加新功能的吧


代码:

using UnityEditor;
using UnityEngine;

class MoveToOrigin {
    /// <summary>
    /// Moves the selected game object(s) to (0, 0, 0).
    /// <summary>
    /// <remarks>Keyboard shortcut: cmd-0 (Mac), ctrl-0 (Windows).</remarks>
    [MenuItem ("GameObject/Move To Origin %0")]
    static void MenuMoveToOrigin () {
        // Move each selected transform to (0, 0, 0)
        foreach (Transform t in Selection.transforms) {
            Undo.RegisterUndo(t, "Move " + t.name);
            t.position = Vector3.zero;
            Debug.Log("Moving " + t.name + " to origin");
        }
    }
   
    /// <summary>
    /// Validates the "Move To Origin" menu item.
    /// </summary>
    /// <remarks>The menu item will be disabled if no transform is selected.</remarks>
    [MenuItem ("GameObject/Move To Origin %0", ***e)]
    static bool ValidateMoveToOrigin () {
        return Selection.activeTransform != null;
    }
}

将此代码命名为MoveToOrigin.cs

并且在你的工程下建立名为Editor的文件夹,并将MoveToOrigin.cs放入其中

你将会在GameObject菜单下发现Move To Origin选项



现在选中一个或多个GameObject然后按cmd+0(Mac系统)/ctrl+0(Windows)

所有选中物体都移动到了原点位置
作者: may    时间: 2012-11-3 12:31
不错的资料,学习!
作者: 狂风大尉    时间: 2012-11-4 00:38
我倒是经常用move to view这个功能




欢迎光临 纳金网 (http://old.narkii.com/club/) Powered by Discuz! X2.5