How Can I Stick (Pin) Sprite(3D World) To Ui Element In Unity3D?

Viewed 17

I'm working on new project with portrait orientation. I have some UI element like button and etc. on top and bottom of my game also I have a TV in game and I want to stick it to my top UI Section and water bottle to bottom UI section(for example) on all mobile devices.

So,I try to convert my UIs section to world space with this : https://docs.unity3d.com/ScriptReference/RectTransform.GetWorldCorners.html and https://www.robotmonkeybrain.com/convert-unity-ui-screen-space-position-to-world-position/ and etc.

Is there any solution for my issue?

My code for TV:

public class RectToWorld : MonoBehaviour
{
    public RectTransform rectTransformTv;
    public Vector3 screenToWorldPosition;
    public Transform _tvSectionPivot;
    void Start()
    {
        DisplayWorldCorners();
    }
    void Update()
    {
        DisplayWorldCorners();
    }
    Vector3[] v = new Vector3[4];
    void DisplayWorldCorners()
    {
        screenToWorldPosition = Camera.main.ScreenToWorldPoint(rectTransformTv.transform.position);
        screenToWorldPosition = new Vector3(screenToWorldPosition.x, screenToWorldPosition.y, 0);
        _tvSectionPivot.localPosition = screenToWorldPosition;
    }
}

Images:

On Galaxy Note8

On Z Fold Tablet

On Redmi Note 7

And So On!

0 Answers
Related