Unity 將UI放置在鼠標點擊位置的位置

  • 1、當渲染模式爲:Screen Space-Overlay
Vector2 localPoint = Input.mousePosition - new Vector3(Screen.width*0.5f,Screen.height*0.5f);
target.transform.localPosition = localPoint/canvans.transform.localScale.x;
  • 2、當渲染模式爲:Screen Space-Camera
Vector2 localPoint;
RectTransformUtility.ScreenPointToLocalPointInRectangle(p,Input.mousePosition,cam,out localPoint);
target.transform.localPosition = localPoint;

其中p爲目標父節點

  • 3、當渲染模式爲:World Space 代碼同2
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章