- Home /
The logic behind copy word placement from transform inspector
Hey , kina struggling to put my image's position to another image position . I Tried different method even transforming between global and local coordinate but my image position will not be the same . finally I tried copying world placement of my first image and pasting the value to second Image transform and it worked !! but with different scaling. I'm wonder what is the logic behind it so I can use it to mimic it and place my image's position correctly
Answer by CodesCove · Dec 18, 2021 at 10:57 PM
Logic simply is that the Copy World Placement stores the position, scale and rotation of the transform (or Rect Transform) and Paste World Placement will set these values. These are real world position and rotation (not local ones) so event if the there is child parent relation ship between objects, it will not matter.
If you want to copy only the position, did you try to use the "Copy Position" and "Paste Position"? That copies only the position.
Difference here is that when you paste the position it will be set as local position, not world position. In this case if the the objects have relationship or different parents then the world position might not be same.
Anyway here is example code that creates custom editor to copy Transform (works also for RectTransform) properties.
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
public class TransformCopier : EditorWindow
{
private Transform source;
private Transform target;
[MenuItem("Window/Transform Copier")]
private static void OpenWindow()
{
TransformCopier window = GetWindow<TransformCopier>();
window.titleContent = new GUIContent("Transform Copier");
window.Show();
}
private void OnGUI()
{
this.DrawTransforms();
this.DrawCopy();
}
private void DrawTransforms()
{
source = EditorGUILayout.ObjectField(new GUIContent("Source: "), source, typeof(Transform), allowSceneObjects: true) as Transform;
target = EditorGUILayout.ObjectField(new GUIContent("Destination: "), target, typeof(Transform), allowSceneObjects: true) as Transform;
}
private void DrawCopy()
{
if (GUILayout.Button("Copy World Position"))
{
Undo.RecordObject(target,"Undo set position"); target.position = source.position;
}
if (GUILayout.Button("Copy World Rotation"))
{
Undo.RecordObject(target, "Undo set rotation"); target.rotation = source.rotation;
}
if (GUILayout.Button("Copy Scale"))
{
Undo.RecordObject(target, "Undo set scale"); target.localScale = source.localScale;
}
}
}
#endif
Just make TransformCopier.cs file and copy this code there. Place the file to the Editor folder. The Transform Copier editor will be then found under Window menu..
PS. I took this from my earlier made code and edited it to work independently. I did only test the basic usage so use at your own risk :)
Your answer
Follow this Question
Related Questions
Sprite changing position when button pressed 1 Answer
Discrepancy Between Editor and Build World Transform Positions 1 Answer
How to change a prefab position during play mode 1 Answer
How can I Instantiate a prefab (projectile) consistently from the character? 0 Answers
Moving object with transform.position ignore other objects even if they collided 1 Answer