MoveToOrigin

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(An editor script for moving one or more selected GameObjects to the origin (0, 0, 0).)
 
(Enabled undo)
Line 15: Line 15:
 
using UnityEngine;
 
using UnityEngine;
  
/// <summary>
+
public class MoveToOrigin {
/// Moves the selected GameObject(s) to (0, 0, 0) with the keyboard shortcut cmd-0 (Mac) / ctrl-0 (Windows).
+
/// </summary>
+
public class MoveToOrigin : MonoBehaviour {
+
 
/// <summary>
 
/// <summary>
/// Adds "Move To Origin" option to the GameObject menu.
+
/// Moves the selected game object(s) to (0, 0, 0).
 
/// <summary>
 
/// <summary>
 +
/// <remarks>Keyboard shortcut: cmd-0 (Mac), ctrl-0 (Windows).</remarks>
 
[MenuItem ("GameObject/Move To Origin %0")]
 
[MenuItem ("GameObject/Move To Origin %0")]
 
static void MoveToOrigin () {
 
static void MoveToOrigin () {
 
// Move each selected transform to (0, 0, 0)
 
// Move each selected transform to (0, 0, 0)
 
foreach (Transform t in Selection.transforms) {
 
foreach (Transform t in Selection.transforms) {
//Undo.RegisterUndo(t.gameObject, "Move " + t.name); // This doesn't seem to work, or I'm using it improperly
+
Undo.RegisterUndo(t, "Move " + t.name);
 
t.position = Vector3.zero;
 
t.position = Vector3.zero;
}
+
Debug.Log("Moving " + t.name + " to origin");
+
// Print a debug message so that the user knows what they hit
+
if (Selection.transforms.Length > 1) {
+
Debug.Log("Moving multiple game objects to (0, 0, 0)");
+
} else {
+
Debug.Log("Moving game object '" + Selection.activeTransform.name + "' to (0, 0, 0)");
+
 
}
 
}
 
     }
 
     }

Revision as of 07:59, 7 September 2009

Author: Matthew Miner

Description

Moves the selected GameObject(s) to (0, 0, 0).

Usage

Place the script inside the Editor folder. Select one or more GameObjects and choose GameObject > Move To Origin or press cmd-0 (Mac) / ctrl-0 (Windows). This is functionally equivalent to invoking Reset on a GameObject's transform.

C# - MoveToOrigin.cs

<csharp> using UnityEditor; using UnityEngine;

public 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 MoveToOrigin () { // 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", true)] static bool ValidateMoveToOrigin () { return Selection.activeTransform != null; } } </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox