Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
World SpaceをScreenSpaceCameraに変換
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UIFollowTarget : MonoBehaviour
{
RectTransform rectTransform = null;
[SerializeField] Transform target = null;
[SerializeField]
Canvas canvas;
void Awake()
{
rectTransform = GetComponent<RectTransform> ();
canvas = GetComponent<Graphic> ().canvas;
}
void Update ()
{
var pos = Vector2.zero;
var uiCamera = Camera.main;
var worldCamera = Camera.main;
var canvasRect = canvas.GetComponent<RectTransform> ();
var screenPos = RectTransformUtility.WorldToScreenPoint (worldCamera, target.position);
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPos, uiCamera, out pos);
rectTransform.localPosition = pos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.