Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Improved TransformCopier
- // by @robotduck 2011
- // Copy single or multiple local transforms from and to sets of gameobjects.
- // based on original single-object-copy script from UnifyCommunity Wiki by Steve Allison-Bunnell (with thanks to aarku)
- using UnityEngine;
- using UnityEditor;
- using System.Collections;
- using System.Collections.Generic;
- public class TransformCopier : ScriptableObject
- {
- private static Vector3 position;
- private static Quaternion rotation;
- private static Vector3 scale;
- private static List<StoredLocalTransform> storedTransforms;
- [MenuItem("Custom/Transform Copier/Copy Transform")]
- static void DoRecord ()
- {
- storedTransforms = new List<StoredLocalTransform>();
- foreach (Transform t in Selection.transforms) {
- storedTransforms.Add(new StoredLocalTransform(t));
- }
- if (storedTransforms.Count == 1)
- {
- EditorUtility.DisplayDialog ("Transform Copy", "Local position, rotation, & scale of " + Selection.activeTransform.name + " copied.", "OK", "");
- } else {
- EditorUtility.DisplayDialog ("Transform Copy", "Local position, rotation, & scale of " + storedTransforms.Count + " objects copied.", "OK", "");
- }
- }
- [MenuItem("Custom/Transform Copier/Paste Transform")]
- static void DoApply ()
- {
- string resultString = "";
- if (storedTransforms.Count == 1)
- {
- StoredLocalTransform stored = storedTransforms[0];
- // paste single transform to all selected:
- foreach (Transform t in Selection.transforms) {
- t.localPosition = stored.localPosition;
- t.localRotation = stored.localRotation;
- t.localScale = stored.localScale;
- }
- int num = Selection.transforms.Length;
- resultString = "Local position, rotation, and scale of " + stored.name + " pasted to ";
- resultString += (num == 1 ? Selection.activeTransform.name : num+" selected objects")+".";
- } else {
- // stored & selected counts must match:
- if (storedTransforms.Count == Selection.transforms.Length)
- {
- int n=0;
- foreach (Transform t in Selection.transforms)
- {
- t.localPosition = storedTransforms[n].localPosition;
- t.localRotation = storedTransforms[n].localRotation;
- t.localScale = storedTransforms[n].localScale;
- ++n;
- }
- resultString = "Local position, rotation, & scale of " + storedTransforms.Count + " objects pasted.";
- } else {
- resultString = "Error: When copying from multiple objects, the number of objects selected must match the number of objects copied from.";
- }
- }
- EditorUtility.DisplayDialog ("Transform Paste", resultString, "OK", "");
- }
- }
- struct StoredLocalTransform
- {
- public Vector3 localPosition;
- public Quaternion localRotation;
- public Vector3 localScale;
- public string name;
- public StoredLocalTransform (Transform t)
- {
- this.localPosition = t.localPosition;
- this.localRotation = t.localRotation;
- this.localScale = t.localScale;
- this.name = t.name;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment