using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TestFindObj : MonoBehaviour
{
public Image image;
public GameObject mobj;
public Canvas canvas;
private TestCoroutine com;
// Start is called before the first frame update
void Start()
{
com = transform.GetComponentInChildren<TestCoroutine>();
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
obj.transform.position = new Vector3(0,0,0);
obj.transform.SetParent(transform);
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Q))
{
TestScreenPoint(Camera.main,mobj,canvas.transform as RectTransform);
Debug.Log("---TestScreenPoint");
}
}
public void TestScreenPoint(Camera ca ,GameObject obj,RectTransform canvasRec)
{
Vector2 mouseDown = ca.WorldToScreenPoint(obj.transform.position);
Debug.Log("---TestScreenPoint" + mouseDown);
Vector2 mouseUGUIPos = new Vector2();
bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRec, mouseDown, ca, out mouseUGUIPos);
if (isRect)
{
Debug.Log("mouseUGUIPos " + mouseUGUIPos);
image.rectTransform.anchoredPosition = mouseUGUIPos;
}
}
}