如何从画布上的对象RectTransform坐标更改为屏幕坐标?

我有此代码可用于更新:

let newArrayOfObjects = [
  {
    Name: "Apple",Type: "Fruit",times: 3,},{
    Name: "carrot",Type: "Vegetable",times: 4,}
];

但是现在我必须使用操纵杆来使指针在屏幕上移动,然后按下按钮来执行某些操作。

赞:

    if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);

        if(touch.phase == TouchPhase.Began)
        {
            touchPosition = touch.position;

            if(arRaycastManager.Raycast(touchPosition,hits,UnityEngine.XR.ARSubsystems.TrackableType.PlaneWithinPolygon))
            {
                Pose hitPose = hits[0].pose;//ok
            }
        }
     }

关于如何解决此问题的任何想法?

asdff_px 回答:如何从画布上的对象RectTransform坐标更改为屏幕坐标?

public Vector3 GetScreenPosition()
{
    RectTransform rectTransform = (RectTransform)transform;
    Vector4 worldLocation = transform.localToWorldMatrix * new Vector4(rectTransform.anchoredPosition3D.x,rectTransform.anchoredPosition3D.y,rectTransform.anchoredPosition3D.z,1);

    return yourCamera.WorldToScreenPoint(new Vector3(worldLocation.x,worldLocation.y,worldLocation.z));
}
,
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.XR.ARFoundation;

    public class SomeClassOnMyARSessionOriginThatContainsMyARCamera : MonoBehaviour
    {     
     public ARCameraManager arCameraManager;//its another child go
     public ARRaycastManager arRaycastManager;
     private static List<ARRaycastHit> hits = new List<ARRaycastHit>();
     private Vector2 touchPosition = default;

     public GameObject startPoint;
     public GameObject endPoint;
     public GameObject startPointbyJoy;
     public GameObject endPointbyJoy;

     public GameObject finger;//It´s a UICanvas
     public float vel;

     void Update()
     {        
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        finger.GetComponent<RectTransform>().anchoredPosition += new Vector2(h * vel * Time.deltaTime,v * vel * Time.deltaTime);

        if (Input.GetKey(KeyCode.JoystickButton13))
        {
            Vector3 fingerScreenPos = GetScreenPosition(finger);
            touchPosition = new Vector2(fingerScreenPos.x,fingerScreenPos.y);//here I use that method

            if (arRaycastManager.Raycast(touchPosition,hits,UnityEngine.XR.ARSubsystems.TrackableType.PlaneWithinPolygon))
            {
                startPointbyJoy.SetActive(true);
                Pose hitPose = hits[0].pose;
                startPointbyJoy.transform.SetPositionAndRotation(hitPose.position,hitPose.rotation);
            }
        }
        if (Input.GetKey(KeyCode.JoystickButton12))
        {            
            Vector3 fingerScreenPos = GetScreenPosition(finger);
            touchPosition = new Vector2(fingerScreenPos.x,UnityEngine.XR.ARSubsystems.TrackableType.PlaneWithinPolygon))
            {
                endPointbyJoy.SetActive(true);
                Pose hitPose = hits[0].pose;
                endPointbyJoy.transform.SetPositionAndRotation(hitPose.position,hitPose.rotation);
            }
        }

        if (Input.touchCount > 0)//these are OK
        {
            Touch touch = Input.GetTouch(0);
            if (touch.phase == TouchPhase.Began)
            {
                touchPosition = touch.position;
                if (arRaycastManager.Raycast(touchPosition,UnityEngine.XR.ARSubsystems.TrackableType.PlaneWithinPolygon))
                {
                    startPoint.SetActive(true);
                    Pose hitPose = hits[0].pose;
                    startPoint.transform.SetPositionAndRotation(hitPose.position,hitPose.rotation);

                }
            }
            if(touch.phase == TouchPhase.Moved)
            {
                touchPosition = touch.position;                
                if(arRaycastManager.Raycast(touchPosition,UnityEngine.XR.ARSubsystems.TrackableType.PlaneWithinPolygon))
                {                    
                    endPoint.SetActive(true);
                    Pose hitPose = hits[0].pose;
                    endPoint.transform.SetPositionAndRotation(hitPose.position,hitPose.rotation);
                }
            }
        }
    }

    public Vector3 GetScreenPosition(GameObject finger)
    {
    //HERE. Which "transform" should I get? The UICancas finger?
    //The go this script is attached to?
        RectTransform rectTransform = (RectTransform)finger.transform;
        Vector4 worldLocation = rectTransform.localToWorldMatrix * new  Vector4(rectTransform.anchoredPosition3D.x,1);

        //is it different using a arcamera?
        return arCameraManager.GetComponent<Camera>().WorldToScreenPoint(new Vector3(worldLocation.x,worldLocation.z));
     }
    }
本文链接:https://www.f2er.com/3142520.html

大家都在问