Xentios
Xentios

Reputation: 80

How to "Copy World Placement" in script?

I am using Unity 2020.1.15f1.

First of all if you don't know you can right click on unity editor on a transform or rect transform select "Copy World Placement" at the bottom and get this info.

UnityEditor.TransformWorldPlacementJSON:{"position":{"x":-17.771259307861329,"y":-9.999999046325684,"z":90.0},"rotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"scale":{"x":1.0,"y":1.0,"z":1.0}}

This is exactly what i need in script but could not find a way to get this info from rect transform components. It does not have to be in JSON format all i need is THE SAME x and y values.

PS: transform.position is not what i want. It does not give these values for a rect transform.

Edit:The reason I could not get these values in script from position was the project was taking these values in void Awake() method which changes position values you get if you call it in void Start()

So i guess derHugos first answer is correct for this question.

Upvotes: 0

Views: 1953

Answers (2)

derHugo
derHugo

Reputation: 90724

For me (Unity 2020.2.5f1) this simply returns the transform.position, transform.rotation and transform.localScale regardless of whether it is a RectTransform or just a Transform.

Note though that a RectTransform in a Canvas of type Screenspace - Overlay is in pixel space!

I don't know how it works internally but basically you could do something like

public static class TransformExtensions
{
    [Serializable]
    private struct TransformData
    {
        public Vector3 position;
        public Quaternion rotation;
        public Vector3 scale;

        public TransformData(Transform transform)
        {
            position = transform.position;
            rotation = transform.rotation;
            scale = transform.localScale;
        }
    }

    public static string CopyWorldPlacementJson(this Transform transform, bool humanReadable = false)
    {
        var data = new TransformData(transform);
        var json = JsonUtility.ToJson(data, humanReadable);
        return json;
    }
}

With that extension method anywhere in your project you can simply do

var json = someGameObject.transform.CopyWorldPlacementJson();
Debug.Log(json);

In particular to UI stuff you might have to wait for the next re-draw of the rects. You could force this using Canvas.ForceUpdateCanvases

Force all canvases to update their content.

A canvas performs its layout and content generation calculations at the end of a frame, just before rendering, in order to ensure that it's based on all the latest changes that may have happened during that frame. This means that in the Start callback and the first Update callback, the layout and content under the canvas may not be up-to-date.

Code that relies on up-to-date layout or content can call this method to ensure it before executing code that relies on it.

Upvotes: 1

Xentios
Xentios

Reputation: 80

The reason I could not get these values in script from position was the project was taking these values in void Awake() method which changes position values you get if you call it in void Start()

So i guess derHugos first answer is correct for this question.

Upvotes: 0

Related Questions