using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; /// /// /// public class TestA : MonoBehaviour { public GameObject trg = null; public Image moveImg = null; public Vector3 sencePos; public Canvas canvas;//画布 private RectTransform rectTransform;//坐标 void Start() { canvas = GetComponent(); rectTransform = canvas.transform as RectTransform; //也可以写成this.GetComponent(),但是不建议; } void Update() { //sencePos = CameraManager.Instance.SenceCamara.WorldToScreenPoint(trg.transform.position); //moveImg.transform.position = CameraManager.Instance.SenceUICamera.ScreenToWorldPoint(sencePos); Vector3 uiCenterPos = WorldToGUI(trg.transform.position); //Debug.LogError("3:屏幕中心:打到己方中心位置....." + trg.transform.position.ToString ()+"/"+ uiCenterPos.ToString()); moveImg.transform.localPosition = WorldToGUI(trg.transform.position); //if (Input.GetMouseButtonDown(0)) //{ // Vector2 pos; // if (RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, trg.transform.position, canvas.worldCamera, out pos)) // { // //rectTransform.anchoredPosition = pos; // moveImg.transform.localPosition = pos; // Debug.Log(pos); // } //} } void OnGUI() { //if (GUI.Button(new Rect(10, 10, 100, 100), "hide")) //{ // this.a.CrossFadeAlpha(0f, 0, false); //} //if (GUI.Button(new Rect(10, 130, 100, 100), "Idle")) //{ // this.a.CrossFadeAlpha(1f, 0, false); //} } /// /// /// /// Vector2 WorldToGUI(Vector3 orgPos) { Vector2 pos; Vector3 screenPos = CameraManager.Instance.SenceCamara.WorldToScreenPoint(orgPos); RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, screenPos, CameraManager.Instance.SenceUICamera, out pos); return pos; } }