TransformSaver

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
 
m (Text replace - "</csharp>" to "</syntaxhighlight>")
 
(One intermediate revision by one user not shown)
Line 14: Line 14:
  
 
==C# - TransformSaver.cs==
 
==C# - TransformSaver.cs==
<csharp>
+
<syntaxhighlight lang="csharp">
 
using UnityEngine;
 
using UnityEngine;
 
using UnityEditor;
 
using UnityEditor;
Line 85: Line 85:
 
     }
 
     }
 
}
 
}
</csharp>
+
</syntaxhighlight>

Latest revision as of 20:47, 10 January 2012

Author: Jonathan Czeck (aarku)

[edit] Description

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.

[edit] Usage

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

[edit] C# - TransformSaver.cs

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);
            transformSaves.Add(transformSave);
        }
 
        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];
                    break;
                }
            }
 
            if (found != null)
            {
                transform.position = found.position;
                transform.rotation = found.rotation;
                transform.localScale = found.localScale;
                numberApplied++;
            }
        }
 
        EditorUtility.DisplayDialog("Transform Saver Apply", "Applied " + numberApplied + " Transforms successfully out of " + transformSaves.Count + " possible.", "OK", "");
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox