ShowScreenArrow.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
using System.Collections; | |
public class ShowScreenArrow : MonoBehaviour { | |
public Transform target; | |
RectTransform rect; | |
Image image; | |
void Awake() { | |
rect = GetComponent<RectTransform>(); | |
image = GetComponent<Image>(); | |
} | |
// Update is called once per frame | |
void Update () { | |
Camera mainCamera = Camera.main; | |
float marginX = rect.rect.width/2; | |
float marginY = rect.rect.height/2; | |
float maxWidth = Mathf.Abs(mainCamera.pixelWidth); | |
float maxHeight = Mathf.Abs (mainCamera.pixelHeight); | |
Vector3 screenPoint = mainCamera.WorldToScreenPoint (target.position); | |
bool imageVisible = screenPoint.x <= marginX | |
|| screenPoint.x >= (maxWidth-marginX) | |
|| screenPoint.y <= marginY | |
|| screenPoint.y >= (maxHeight-marginY); | |
screenPoint.x = Mathf.Min (Mathf.Max (screenPoint.x, marginX), maxWidth - marginX); | |
screenPoint.y = Mathf.Min (Mathf.Max (screenPoint.y, marginY), maxHeight - marginY); | |
float angle = Mathf.Atan2 (screenPoint.y-maxWidth/2, screenPoint.x - maxHeight/2) * Mathf.Rad2Deg; | |
rect.rotation = Quaternion.Euler (0,0,angle); | |
rect.position = screenPoint; | |
image.enabled = imageVisible; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment