At first i put it in Update().
But value only changes when “item” is equipped/taked off, so its seems wasteful.
I tried to change UI value from the sourse of a change and use Canvas.ForceUpdateCanvases(); Doesnt work.
May be some method ?
By the way i did not using methods almost at all in my game. My code is just bunch of IF-statemants and varibales.
Something like this where i create my ““Dynamic UI””
using System;
using System.Collections;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
public class OnHoverItemDescription : MonoBehaviour
{
private void OnMouseEnter()
{
if (gameObject.GetComponent<IsItem>())
{
GameObject obj = new(gameObject.name + "_Parent");
obj.transform.SetParent(transform, true);
obj.AddComponent<RectTransform>();
obj.AddComponent<Canvas>();
obj.AddComponent<SpriteRenderer>();
obj.AddComponent<CanvasRenderer>();
obj.AddComponent<GridLayoutGroup>();
obj.GetComponent<RectTransform>().sizeDelta = new Vector2(5, 5);
obj.GetComponent<RectTransform>().localPosition = new Vector3(-1, 0, 0);
obj.GetComponent<Canvas>().renderMode = RenderMode.WorldSpace;
obj.GetComponent<Canvas>().worldCamera = Camera.main;
obj.GetComponent<Canvas>().sortingOrder = 15;
obj.GetComponent<GridLayoutGroup>().cellSize = new Vector2(4, 1);
obj.GetComponent<GridLayoutGroup>().constraint = GridLayoutGroup.Constraint.FixedColumnCount;
obj.GetComponent<GridLayoutGroup>().constraintCount = 1;
obj.GetComponent<GridLayoutGroup>().spacing = new Vector2(0, -0.5f);
for (int i = 0; i < gameObject.GetComponent<IsItem>().props.Count; i++)
{
if (transform.GetChild(0))
{
_ = new GameObject(i.ToString());
GameObject.Find(i.ToString()).transform.SetParent(transform.GetChild(0), true);
transform.GetChild(0).GetChild(i).AddComponent<TextMeshProUGUI>();
transform.GetChild(0).GetChild(i).GetComponent<TextMeshProUGUI>().text = gameObject.GetComponent<IsItem>().props[i].ToString();
transform.GetChild(0).GetChild(i).GetComponent<TextMeshProUGUI>().fontSize = 0.3f;
}
}
}
}
private void OnMouseOver()
{
if (transform.childCount > 0)
{
if (Input.GetMouseButton(0))
{
transform.GetChild(0).gameObject.SetActive(false);
}
else
{
transform.GetChild(0).gameObject.SetActive(true);
}
LayerMask mask = LayerMask.GetMask("Wrapper");
if (Physics2D.Raycast(transform.position, Vector2.left, 5f, mask))
{
transform.GetChild(0).localPosition = new Vector3(6, 0, 0);
}
else
{
transform.GetChild(0).localPosition = new Vector3(-1, 0, 0);
}
}
}
private void OnMouseExit()
{
if (transform.childCount > 0)
{
transform.GetChild(0).gameObject.SetActive(false);
Destroy(transform.GetChild(0).gameObject);
}
}
}