From Unify Community Wiki
Revision as of 20:23, 8 July 2006 by Aarku (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Author: Jonathan Czeck (aarku)


This editor script allows you to select any number of objects in your scene at any time and select Custom/Transform Saver/Record Selected Transforms. Then at any time you can choose Custom/Transform Saver/Apply Saved Transforms to have the stored position, rotation, and localScale of the saved object's Transforms applied again.

This has been tested to work across the Play Mode / Edit Mode boundary successfully. That means you can use these menu items to, for instance, hit play, allow the physics engine to settle, record the appropriate objects, hit stop, and apply their settled positions and rotations.


Place this script in YourProject/Assets/Editor and menu items will automatically appear in the Custom menu after it is compiled.

C# - TransformSaver.cs

<csharp> using UnityEngine; using UnityEditor; using System.Collections;

public class TransformSaver : ScriptableObject {

   private class TransformSave
       public int instanceID;
       public Vector3 position;
       public Quaternion rotation;
       public Vector3 localScale;
       public TransformSave(int instanceID, Vector3 position, Quaternion rotation, Vector3 localScale)
           this.instanceID = instanceID;
           this.position = position;
           this.rotation = rotation;
           this.localScale = localScale;
   private static ArrayList transformSaves = new ArrayList();
   [MenuItem ("Custom/Transform Saver/Record Selected Transforms")]
   static void DoRecord()
       Transform[] selection = Selection.GetTransforms(SelectionMode.Editable | SelectionMode.ExcludePrefab);
       transformSaves = new ArrayList(selection.Length);
       foreach (Transform selected in selection)
           TransformSave transformSave = new TransformSave(selected.GetInstanceID(), selected.position, selected.rotation, selected.localScale);
       EditorUtility.DisplayDialog("Transform Saver Record", "Recorded " + transformSaves.Count + " Transforms.", "OK", "");
   [MenuItem ("Custom/Transform Saver/Apply Saved Transforms")]
   static void DoApply()
       Transform[] transforms = FindObjectsOfType(typeof(Transform)) as Transform[];
       int numberApplied = 0;
       foreach (Transform transform in transforms)
           TransformSave found = null;
           for (int i=0; i < transformSaves.Count; i++)
               if (((TransformSave) transformSaves[i]).instanceID == transform.GetInstanceID())
                   found = (TransformSave) transformSaves[i];
           if (found != null)
               transform.position = found.position;
               transform.rotation = found.rotation;
               transform.localScale = found.localScale;
       EditorUtility.DisplayDialog("Transform Saver Apply", "Applied " + numberApplied + " Transforms successfully out of " + transformSaves.Count + " possible.", "OK", "");

} </csharp>

Personal tools