从世界坐标转换成ui的rect坐标的方法

时间:2023-03-08 20:06:58

这个东西整整折磨了我一个通宵。原谅我先这样放上来。明天整理整理

 using UnityEngine;
using System.Collections;
using UnityEngine.UI; public class test_temp2 : MonoBehaviour {
public GameObject 要出现的世界点;
public Camera 摄像机;
private RectTransform 要修改的屏幕点;
public GameObject 预制件;
private GameObject 脚跟死皮;
//预制件的名字是 canvas/jiafen public void 大狗逼熬夜一晚上才整明白的傻逼方法()
{ //你妈的无论是GetComponent还是GetComponentInChildren()都只能获得canvas预制件的父组件,尼玛有什么用啊 害我一晚上没睡
脚跟死皮 = Instantiate(预制件);
脚跟死皮.GetComponent<RectTransform>().FindChild("jiafen").GetComponent<RectTransform>().anchoredPosition=
RectTransformUtility.WorldToScreenPoint(摄像机, 要出现的世界点.transform.position);
//temp.GetComponentInChildren<RectTransform>().anchoredPosition= RectTransformUtility.WorldToScreenPoint(摄像机, 要出现的世界点.transform.position);
//temp.GetComponentInChildren<Text>().text = temp.GetComponentInChildren<RectTransform>().FindChild("jiafen").GetComponent<RectTransform>().anchoredPosition.ToString();
//temp.GetComponentInChildren<RectTransform>().anchoredPosition = new Vector2(320.6f, 70.1f);
//temp.GetComponentInChildren<RectTransform>().anchoredPosition;
//要修改的屏幕点 = temp.GetComponentInChildren<RectTransform>();//获得要修改坐标的物体
//要修改的屏幕点.anchoredPosition= RectTransformUtility.WorldToScreenPoint(摄像机, 要出现的世界点.transform.position);
//temp.GetComponentInChildren<Text>().text= "asdlkfjalsdkf";
Destroy(脚跟死皮.gameObject, 1f);
//*终于整明白了。原来temp.GetComponentInChildren<RectTransform>()获得的还是父物体 }
}