CopyTransform

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "</csharp>" to "</syntaxhighlight>")
(C# - CopyTransform.cs)
Line 18: Line 18:
 
using System.Collections;
 
using System.Collections;
 
   
 
   
public class TransformCopier : ScriptableObject
+
public class TransformCopier : ScriptableObject {
{
+
        private static Vector3 position;
+
private static Vector3 position;
        private static Quaternion rotation;
+
private static Quaternion rotation;
        private static Vector3 scale;
+
private static Vector3 scale;
        private static string myName;  
+
private static string myName;
   
+
    [MenuItem ("Custom/Transform Copier/Copy Transform")]
+
[MenuItem ("Window/Transform Copier/Copy")]
    static void DoRecord()
+
static void DoRecord () {
    {
+
position = Selection.activeTransform.localPosition;
      position = Selection.activeTransform.localPosition;
+
rotation = Selection.activeTransform.localRotation;
      rotation = Selection.activeTransform.localRotation;
+
scale = Selection.activeTransform.localScale;
      scale = Selection.activeTransform.localScale;
+
myName = Selection.activeTransform.name;
      myName = Selection.activeTransform.name;      
+
EditorUtility.DisplayDialog("Transform Copy", "Local position, rotation, & scale of "+myName +" copied relative to parent.", "OK", "");
       
+
        EditorUtility.DisplayDialog("Transform Copy", "Local position, rotation, & scale of "+myName +" copied relative to parent.", "OK", "");
+
 
     }
 
     }
+
    [MenuItem ("Custom/Transform Copier/Paste Transform")]
+
[MenuItem ("Window/Transform Copier/Paste Scale XYZ")]
     static void DoApply()
+
     static void DoApplyScaleXYZ () {
    {
+
         Transform[] selections  = Selection.transforms;
         Selection.activeTransform.localPosition = position;
+
foreach (Transform selection  in selections) selection.localScale = scale;
        Selection.activeTransform.localRotation = rotation;
+
        Selection.activeTransform.localScale = scale;     
+
       
+
        EditorUtility.DisplayDialog("Transform Paste", "Local position, rotation, and scale of "+myName +"  pasted relative to parent of "+Selection.activeTransform.name+".", "OK", "");
+
 
     }
 
     }
 +
 +
[MenuItem ("Window/Transform Copier/Paste Scale X")]
 +
    static void DoApplyScaleX () {
 +
        Transform[] selections  = Selection.transforms;
 +
foreach (Transform selection  in selections) selection.localScale = new Vector3(scale.x, selection.localScale.y, selection.localScale.z);
 +
    }
 +
 +
[MenuItem ("Window/Transform Copier/Paste Scale Y")]
 +
    static void DoApplyScaleY () {
 +
        Transform[] selections  = Selection.transforms;
 +
foreach (Transform selection  in selections) selection.localScale = new Vector3(selection.localScale.x, scale.y, selection.localScale.z);
 +
    }
 +
 +
[MenuItem ("Window/Transform Copier/Paste Scale Z")]
 +
    static void DoApplyScaleZ () {
 +
        Transform[] selections  = Selection.transforms;
 +
foreach (Transform selection  in selections) selection.localScale = new Vector3(selection.localScale.x, selection.localScale.y, scale.z);
 +
    }
 +
 +
[MenuItem ("Window/Transform Copier/Paste Rotation XYZ")]
 +
    static void DoApplyRotationXYZ () {
 +
        Transform[] selections  = Selection.transforms;
 +
foreach (Transform selection  in selections) selection.localRotation = rotation;
 +
    }
 +
 +
[MenuItem ("Window/Transform Copier/Paste Rotation X")]
 +
static void DoApplyRotationX () {
 +
Transform[] selections  = Selection.transforms;
 +
foreach (Transform selection  in selections) selection.localRotation = Quaternion.Euler(rotation.eulerAngles.x, selection.localRotation.eulerAngles.y, selection.localRotation.eulerAngles.z);
 +
}
 +
 +
[MenuItem ("Window/Transform Copier/Paste Rotation Y")]
 +
    static void DoApplyRotationY () {
 +
        Transform[] selections  = Selection.transforms;
 +
foreach (Transform selection  in selections) selection.localRotation = Quaternion.Euler(selection.localRotation.eulerAngles.x, rotation.eulerAngles.y, selection.localRotation.eulerAngles.z);
 +
}
 +
 +
[MenuItem ("Window/Transform Copier/Paste Rotation Z")]
 +
    static void DoApplyRotationZ () {
 +
        Transform[] selections  = Selection.transforms;
 +
foreach (Transform selection  in selections) selection.localRotation = Quaternion.Euler(selection.localRotation.eulerAngles.x, selection.localRotation.eulerAngles.y, rotation.eulerAngles.z);
 +
}
 +
 +
[MenuItem ("Window/Transform Copier/Paste Position XYZ")]
 +
    static void DoApplyPositionXYZ () {
 +
        Transform[] selections  = Selection.transforms;
 +
foreach (Transform selection  in selections) selection.localPosition = position;
 +
    }
 +
 +
[MenuItem ("Window/Transform Copier/Paste Position X")]
 +
    static void DoApplyPositionX () {
 +
        Transform[] selections  = Selection.transforms;
 +
foreach (Transform selection  in selections) selection.localPosition = new Vector3(position.x, selection.localPosition.y, selection.localPosition.z);
 +
    }
 +
 +
[MenuItem ("Window/Transform Copier/Paste Position Y")]
 +
    static void DoApplyPositionY () {
 +
        Transform[] selections  = Selection.transforms;
 +
foreach (Transform selection  in selections) selection.localPosition = new Vector3(selection.localPosition.x, position.y, selection.localPosition.z);
 +
}
 +
 +
[MenuItem ("Window/Transform Copier/Paste Position Z")]
 +
    static void DoApplyPositionZ () {
 +
        Transform[] selections  = Selection.transforms;
 +
foreach (Transform selection  in selections) selection.localPosition = new Vector3(selection.localPosition.x, selection.localPosition.y, position.z);
 +
    }
 +
 +
[MenuItem ("Window/Transform Copier/localRotation.x + 90")]
 +
    static void localRotateX90 () {
 +
        Transform[] selections  = Selection.transforms;
 +
foreach (Transform selection  in selections) selection.localRotation = selection.localRotation*Quaternion.Euler(90f,0f,0f);
 +
    }
 +
 +
[MenuItem ("Window/Transform Copier/localRotation.y + 90")]
 +
    static void localRotateY90 () {
 +
        Transform[] selections  = Selection.transforms;
 +
foreach (Transform selection  in selections) selection.localRotation = selection.localRotation*Quaternion.Euler(0f,90f,0f);
 +
    }
 +
 +
[MenuItem ("Window/Transform Copier/localRotation.z + 90")]
 +
    static void localRotateZ90 () {
 +
        Transform[] selections  = Selection.transforms;
 +
foreach (Transform selection  in selections) selection.localRotation = selection.localRotation*Quaternion.Euler(0f,0f,90f);
 +
}
 +
 +
[MenuItem ("Window/Transform Copier/Swap Y and Z Rotation")]
 +
    static void SwapYZRotation () {
 +
Transform[] selections  = Selection.transforms;
 +
foreach (Transform selection  in selections) selection.localRotation = Quaternion.Euler(selection.localRotation.x,selection.localRotation.z,selection.localRotation.y);
 +
}
 +
 +
[MenuItem ("Window/Transform Copier/Swap Y and Z Scale")]
 +
static void SwapYZScale () {
 +
Transform[] selections  = Selection.transforms;
 +
foreach (Transform selection  in selections) selection.localScale = new Vector3 (selection.lossyScale.x,selection.lossyScale.z,selection.lossyScale.y);
 +
}
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 09:41, 26 October 2013

Author: Steve Allison-Bunnell (with thanks to aarku)

Description

Allows you to copy the local position, rotation, and scale of the currently selected object in the scene. Then you can paste this transform information into another object. Useful for positioning new prefabs in the location of a different prefab if you want to swap them.

Usage

You must place the script in a folder named Editor in your project's Assets folder for it to work properly.

Select an object in the scene heirarchy, then choose Transform Copier -> Copy Transform. This copies the local position, rotation, and scale of the selected object. Then select another object and choose Transform Copier -> Paste Transform. This will apply the copied transform to the new object. Both objects must either be at the root of the scene heirarchy or within the same parent for the transform to be applied properly.

Zipped file: TransformCopier.cs.zip

C# - CopyTransform.cs

using UnityEngine;
using UnityEditor;
using System.Collections;
 
public class TransformCopier : ScriptableObject {
 
	private static Vector3 position;
	private static Quaternion rotation;
	private static Vector3 scale;
	private static string myName;
 
	[MenuItem ("Window/Transform Copier/Copy")]
	static void DoRecord () {
		position = Selection.activeTransform.localPosition;
		rotation = Selection.activeTransform.localRotation;
		scale = Selection.activeTransform.localScale;
		myName = Selection.activeTransform.name;
		EditorUtility.DisplayDialog("Transform Copy", "Local position, rotation, & scale of "+myName +" copied relative to parent.", "OK", "");
    }
 
	[MenuItem ("Window/Transform Copier/Paste Scale XYZ")]
    static void DoApplyScaleXYZ () {
        Transform[] selections  = Selection.transforms;
		foreach (Transform selection  in selections) selection.localScale = scale;
    }
 
	[MenuItem ("Window/Transform Copier/Paste Scale X")]
    static void DoApplyScaleX () {
        Transform[] selections  = Selection.transforms;
		foreach (Transform selection  in selections) selection.localScale = new Vector3(scale.x, selection.localScale.y, selection.localScale.z);
    }
 
	[MenuItem ("Window/Transform Copier/Paste Scale Y")]
    static void DoApplyScaleY () {
        Transform[] selections  = Selection.transforms;
		foreach (Transform selection  in selections) selection.localScale = new Vector3(selection.localScale.x, scale.y, selection.localScale.z);
    }
 
	[MenuItem ("Window/Transform Copier/Paste Scale Z")]
    static void DoApplyScaleZ () {
        Transform[] selections  = Selection.transforms;
		foreach (Transform selection  in selections) selection.localScale = new Vector3(selection.localScale.x, selection.localScale.y, scale.z);
    }
 
	[MenuItem ("Window/Transform Copier/Paste Rotation XYZ")]
    static void DoApplyRotationXYZ () {
        Transform[] selections  = Selection.transforms;
		foreach (Transform selection  in selections) selection.localRotation = rotation;
    }
 
	[MenuItem ("Window/Transform Copier/Paste Rotation X")]
	static void DoApplyRotationX () {
		Transform[] selections  = Selection.transforms;
		foreach (Transform selection  in selections) selection.localRotation = Quaternion.Euler(rotation.eulerAngles.x, selection.localRotation.eulerAngles.y, selection.localRotation.eulerAngles.z);
	}
 
	[MenuItem ("Window/Transform Copier/Paste Rotation Y")]
    static void DoApplyRotationY () {
        Transform[] selections  = Selection.transforms;
		foreach (Transform selection  in selections) selection.localRotation = Quaternion.Euler(selection.localRotation.eulerAngles.x, rotation.eulerAngles.y, selection.localRotation.eulerAngles.z);
	}
 
	[MenuItem ("Window/Transform Copier/Paste Rotation Z")]
    static void DoApplyRotationZ () {
        Transform[] selections  = Selection.transforms;
		foreach (Transform selection  in selections) selection.localRotation = Quaternion.Euler(selection.localRotation.eulerAngles.x, selection.localRotation.eulerAngles.y, rotation.eulerAngles.z);
	}
 
	[MenuItem ("Window/Transform Copier/Paste Position XYZ")]
    static void DoApplyPositionXYZ () {
        Transform[] selections  = Selection.transforms;
		foreach (Transform selection  in selections) selection.localPosition = position;
    }
 
	[MenuItem ("Window/Transform Copier/Paste Position X")]
    static void DoApplyPositionX () {
        Transform[] selections  = Selection.transforms;
		foreach (Transform selection  in selections) selection.localPosition = new Vector3(position.x, selection.localPosition.y, selection.localPosition.z);
    }
 
	[MenuItem ("Window/Transform Copier/Paste Position Y")]
    static void DoApplyPositionY () {
        Transform[] selections  = Selection.transforms;
		foreach (Transform selection  in selections) selection.localPosition = new Vector3(selection.localPosition.x, position.y, selection.localPosition.z);
	}
 
	[MenuItem ("Window/Transform Copier/Paste Position Z")]
    static void DoApplyPositionZ () {
        Transform[] selections  = Selection.transforms;
		foreach (Transform selection  in selections) selection.localPosition = new Vector3(selection.localPosition.x, selection.localPosition.y, position.z);
    }
 
	[MenuItem ("Window/Transform Copier/localRotation.x + 90")]
    static void localRotateX90 () {
        Transform[] selections  = Selection.transforms;
		foreach (Transform selection  in selections) selection.localRotation = selection.localRotation*Quaternion.Euler(90f,0f,0f);
    }
 
	[MenuItem ("Window/Transform Copier/localRotation.y + 90")]
    static void localRotateY90 () {
        Transform[] selections  = Selection.transforms;
		foreach (Transform selection  in selections) selection.localRotation = selection.localRotation*Quaternion.Euler(0f,90f,0f);
    }
 
	[MenuItem ("Window/Transform Copier/localRotation.z + 90")]
    static void localRotateZ90 () {
        Transform[] selections  = Selection.transforms;
		foreach (Transform selection  in selections) selection.localRotation = selection.localRotation*Quaternion.Euler(0f,0f,90f);
	}
 
	[MenuItem ("Window/Transform Copier/Swap Y and Z Rotation")]
    static void SwapYZRotation () {
		Transform[] selections  = Selection.transforms;
		foreach (Transform selection  in selections) selection.localRotation = Quaternion.Euler(selection.localRotation.x,selection.localRotation.z,selection.localRotation.y);
	}
 
	[MenuItem ("Window/Transform Copier/Swap Y and Z Scale")]
	static void SwapYZScale () {
		Transform[] selections  = Selection.transforms;
		foreach (Transform selection  in selections) selection.localScale = new Vector3 (selection.lossyScale.x,selection.lossyScale.z,selection.lossyScale.y);
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox