「Unity/3d/3dから2dの座標変換」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「 参考:http://tsubakit1.hateblo.jp/entry/2016/03/01/020510」) |
|||
行1: | 行1: | ||
+ | LabelPosition.cs | ||
+ | <pre> | ||
+ | using UnityEngine; | ||
+ | public class LabelPosition : MonoBehaviour | ||
+ | { | ||
+ | [SerializeField] | ||
+ | public Camera mainCamera, uiCamera; | ||
+ | [SerializeField] | ||
+ | public GameObject cube; | ||
+ | [SerializeField] | ||
+ | public Canvas canvas; | ||
+ | |||
+ | void Update() | ||
+ | { | ||
+ | var pos = Vector2.zero; | ||
+ | var worldCamera = mainCamera; | ||
+ | var canvasRect = canvas.GetComponent<RectTransform>(); | ||
+ | |||
+ | Vector3 position = | ||
+ | new Vector3( | ||
+ | cube.transform.position.x, | ||
+ | cube.transform.position.y + 1f, | ||
+ | cube.transform.position.z | ||
+ | ); | ||
+ | var screenPos = RectTransformUtility.WorldToScreenPoint(worldCamera, position); | ||
+ | RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPos, uiCamera, out pos); | ||
+ | this.GetComponent<RectTransform>().localPosition = pos; | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | 呼び出し方サンプル | ||
+ | |||
+ | Example.cs | ||
+ | <pre> | ||
+ | void LabelCall() { | ||
+ | Camera camera = GameObject.Find("FPS Camera").GetComponent<Camera>(); | ||
+ | GameObject zombie = GameObject.Find("Zombie" + wordcnt + "Object"); | ||
+ | GameObject obj = GameObject.Find("NewText"); | ||
+ | LabelPosition labelPosition = obj.GetComponent<LabelPosition>(); | ||
+ | labelPosition.mainCamera = camera; | ||
+ | labelPosition.uiCamera = camera; | ||
+ | labelPosition.cube = zombie; | ||
+ | labelPosition.canvas = GameObject.Find("Canvas").GetComponent<Canvas>(); | ||
+ | } | ||
+ | </pre> | ||
参考:http://tsubakit1.hateblo.jp/entry/2016/03/01/020510 | 参考:http://tsubakit1.hateblo.jp/entry/2016/03/01/020510 |
2020年9月17日 (木) 09:08時点における版
LabelPosition.cs
using UnityEngine; public class LabelPosition : MonoBehaviour { [SerializeField] public Camera mainCamera, uiCamera; [SerializeField] public GameObject cube; [SerializeField] public Canvas canvas; void Update() { var pos = Vector2.zero; var worldCamera = mainCamera; var canvasRect = canvas.GetComponent<RectTransform>(); Vector3 position = new Vector3( cube.transform.position.x, cube.transform.position.y + 1f, cube.transform.position.z ); var screenPos = RectTransformUtility.WorldToScreenPoint(worldCamera, position); RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPos, uiCamera, out pos); this.GetComponent<RectTransform>().localPosition = pos; } }
呼び出し方サンプル
Example.cs
void LabelCall() { Camera camera = GameObject.Find("FPS Camera").GetComponent<Camera>(); GameObject zombie = GameObject.Find("Zombie" + wordcnt + "Object"); GameObject obj = GameObject.Find("NewText"); LabelPosition labelPosition = obj.GetComponent<LabelPosition>(); labelPosition.mainCamera = camera; labelPosition.uiCamera = camera; labelPosition.cube = zombie; labelPosition.canvas = GameObject.Find("Canvas").GetComponent<Canvas>(); }