Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created February 29, 2016 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/6869f196d201f4f4c9e8 to your computer and use it in GitHub Desktop.
Save tsubaki/6869f196d201f4f4c9e8 to your computer and use it in GitHub Desktop.
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