Files
2022-07-06 12:36:49 +03:00

87 lines
3.4 KiB
C#

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class hint : MonoBehaviour, IPointerEnterHandler, IPointerDownHandler, IPointerExitHandler, IPointerUpHandler
{
[HideInInspector] public Manager manager;
public static bool IsHint = true;
private Image[] img;
private GameObject hintObj;
public GameObject go;
[SerializeField]
private string t;
void Start()
{
img = this.GetComponentsInChildren<Image>();
img[0].gameObject.SetActive(true);
img[1].gameObject.SetActive(false);
gameObject.GetComponentInChildren<Image>().color = (IsHint) ? Color.green : Color.white;
manager = GameObject.Find("Manager").GetComponent<Manager>();
}
public void OnPointerUp(PointerEventData eventData)
{
IsHint = !IsHint;
gameObject.GetComponentInChildren<Image>().color = (IsHint) ? Color.green : Color.white;
}
public void OnPointerEnter(PointerEventData eventData)
{
img[0].gameObject.SetActive(false);
img[1].gameObject.SetActive(true);
gameObject.GetComponentInChildren<Image>().color = (IsHint) ? Color.green : Color.white;
var h = GameObject.Find("hintContainer");
Debug.Log(h);
if (h != null)
{
Destroy(h);
}
hintObj = OnHint(this.gameObject, go, t);
//manager.mouseEnterUI();
}
public void OnPointerDown(PointerEventData eventData)
{
gameObject.GetComponentInChildren<Image>().color = Color.green;
}
public void OnPointerExit(PointerEventData eventData)
{
img[0].gameObject.SetActive(true);
img[1].gameObject.SetActive(false);
gameObject.GetComponentInChildren<Image>().color = (IsHint) ? Color.green : Color.white;
Destroy(hintObj);
//manager.mouseExitUI();
}
public GameObject OnHint(GameObject btn, GameObject objGo, string t)
{
Vector2 hintPos = new Vector2(btn.transform.localPosition.x + 15, btn.transform.localPosition.y + 55);//позиция хинта
GameObject hint = Instantiate(objGo, btn.transform.parent);//создание хинта
hint.name = "hintContainer";
hint.transform.localPosition = hintPos;//позиция хинта устанавливаем
var hintBase = hint.GetComponentInChildren<Image>();
Text txt = hint.GetComponentInChildren<Text>();
txt.text = t;
var s = txt.text.Length;
//Debug.Log($"s = {s}");
RectTransform rectTransformText = txt.GetComponent<RectTransform>();
rectTransformText.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, s * 10 + 20);
RectTransform rectTransformBase = hintBase.GetComponent<RectTransform>();
rectTransformBase.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, rectTransformText.rect.width + 35);
if ((rectTransformBase.rect.width + hintBase.transform.position.x) > Screen.width)
{
hint.transform.localPosition = new Vector2(hint.transform.localPosition.x - rectTransformBase.rect.width/2 + 25, hint.transform.localPosition.y);
var hintTr = hint.transform.GetChild(1).GetComponent<Image>();
hintTr.transform.localPosition = new Vector2(hintTr.transform.localPosition.x + rectTransformBase.rect.width/2 - 30, hintTr.transform.localPosition.y);
//Debug.Log($"hintTr.name = {hintTr.name}");
}
return hint;
}
}