- Home /
Check if rotated UI are overlapping based on Rotation.Z
I am able see if two things are overlapping however I have issue with rotated UI this is important as it for a inventory system and system needs allow rotation of items bellow is current code
Disclaimer yes I have looked for it online try way solve this but have not found a answer that works it may be that I am making a error I am not sure.
public class Item_Select : MonoBehaviour
{
public GridItem Selected;
public RectTransform rectTransform1;
public RectTransform rectTransform2;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
rectTransform2 = this.GetComponent<RectTransform>();
if (Selected != null)
{
/*
Rotation of UI based on Z needs go here unrotated as else statement.
*/
if (Selected.Rotate)
{
rectTransform1 = Selected.GetComponent<RectTransform>();
Vector3[] corners = new Vector3[4];
rectTransform1.GetLocalCorners(corners);
Rect rec = new Rect(corners[0].x, corners[0].y, corners[2].x - corners[0].x, corners[2].y - corners[0].y);
rectTransform2.GetLocalCorners(corners);
Rect rec2 = new Rect(corners[0].x, corners[0].y, corners[2].x - corners[0].x, corners[2].y - corners[0].y);
if (rec.Overlaps(rec2))
{
this.GetComponent<RawImage>().color = Color.red;
}
else
{
this.GetComponent<RawImage>().color = Color.gray;
}
}
else
{
rectTransform1 = Selected.GetComponent<RectTransform>();
Vector3[] corners = new Vector3[4];
rectTransform1.GetWorldCorners(corners);
Rect rec = new Rect(corners[0].x, corners[0].y, corners[2].x - corners[0].x, corners[2].y - corners[0].y);
rectTransform2.GetWorldCorners(corners);
Rect rec2 = new Rect(corners[0].x, corners[0].y, corners[2].x - corners[0].x, corners[2].y - corners[0].y);
if (rec.Overlaps(rec2))
{
this.GetComponent<RawImage>().color = Color.red;
}
else
{
this.GetComponent<RawImage>().color = Color.gray;
}
}
}
}
}
Comment