Greetings,
I am working with UI elements with my personal project at the moment, and while I am trying to design what I want my UI to look like, I was wondering how you would assign a ui element to pop up only around certain objects.
As a real world example, take the menu system from old school Secret of Mana. When you hit the Menu button, the menu would appear around just your character.
I want to have a similar effect for objects as well. For example, clicking on a tree produces a UI which gives you options of what you can do with that tree.
Thank you for any input you can give me.
You can use this script to Anchor a recttransform to a transform to in your scene.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class WorldSpaceAnchor : MonoBehaviour {
public Transform Anchor;
private RectTransform myRectTransform;
private Vector2 guiScreenPos;
[Header("Optional")]
[Tooltip("If empty the main Camera will be userd.")]
public Camera TargetCamera;
private CanvasScaler myCanvasScaler;
private CanvasGroup myCanvasGroup;
private float scaleFactorX , scaleFactorY ;
private Vector3 screenPos;
// Use this for initialization
void Start () {
myRectTransform = GetComponent<RectTransform>();
myCanvasScaler = transform.root.GetComponent<CanvasScaler>();
myRectTransform.anchorMin = new Vector2(0,0);
myRectTransform.anchorMax = new Vector2(0,0);
}
// Update is called once per frame
void Update () {
if(Anchor != null)
{
if(TargetCamera != null)
{
screenPos = TargetCamera.WorldToScreenPoint(Anchor.position);
}
else
{
screenPos = Camera.main.WorldToScreenPoint(Anchor.position);
}
scaleFactorX = myCanvasScaler.referenceResolution.x / Screen.width;
scaleFactorY = myCanvasScaler.referenceResolution.y / Screen.height;
myRectTransform.anchoredPosition = new Vector2(screenPos.x * scaleFactorX, screenPos.y * scaleFactorY);
}
}
}
1 Like
Ill give that a try, thank you
fffMalzbier:
You can use this script to Anchor a recttransform to a transform to in your scene.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class WorldSpaceAnchor : MonoBehaviour {
public Transform Anchor;
private RectTransform myRectTransform;
private Vector2 guiScreenPos;
[Header("Optional")]
[Tooltip("If empty the main Camera will be userd.")]
public Camera TargetCamera;
private CanvasScaler myCanvasScaler;
private CanvasGroup myCanvasGroup;
private float scaleFactorX , scaleFactorY ;
private Vector3 screenPos;
// Use this for initialization
void Start () {
myRectTransform = GetComponent<RectTransform>();
myCanvasScaler = transform.root.GetComponent<CanvasScaler>();
myRectTransform.anchorMin = new Vector2(0,0);
myRectTransform.anchorMax = new Vector2(0,0);
}
// Update is called once per frame
void Update () {
if(Anchor != null)
{
if(TargetCamera != null)
{
screenPos = TargetCamera.WorldToScreenPoint(Anchor.position);
}
else
{
screenPos = Camera.main.WorldToScreenPoint(Anchor.position);
}
scaleFactorX = myCanvasScaler.referenceResolution.x / Screen.width;
scaleFactorY = myCanvasScaler.referenceResolution.y / Screen.height;
myRectTransform.anchoredPosition = new Vector2(screenPos.x * scaleFactorX, screenPos.y * scaleFactorY);
}
}
}