84 lines
2.7 KiB
C#
84 lines
2.7 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
public class AllyUpgrade : MonoBehaviour, IPointerClickHandler
|
|
{
|
|
[SerializeField]
|
|
private GameObject _upgradeUI;
|
|
[SerializeField]
|
|
private List<UnitUpgrade> _upgradeList = new List<UnitUpgrade>();
|
|
private GameObject _instance = null;
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
{
|
|
if (eventData.button == 0) //Left click
|
|
{
|
|
if (!_instance)
|
|
{
|
|
GameObject sceneCanvas = GameObject.Find("Canvas");
|
|
if (sceneCanvas != null)
|
|
{
|
|
Canvas canvas = sceneCanvas.GetComponent<Canvas>();
|
|
if (canvas == null)
|
|
{
|
|
Debug.Log("Canvas introuvable...");
|
|
}
|
|
else
|
|
{
|
|
_instance = Instantiate(_upgradeUI, Camera.main.WorldToScreenPoint(transform.position), Quaternion.identity, canvas.transform);
|
|
AssignUpgrades(canvas);
|
|
//EventSystem.current.SetSelectedGameObject(_instance);
|
|
//_instance.GetComponent<Selectable>().Select();
|
|
//_instance.AddComponent<DeselectedAction>();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Destroy(_instance);
|
|
_instance = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void AssignUpgrades(Canvas canvas)
|
|
{
|
|
// Assign upgrade Prefabs
|
|
for (int i = 0; i < _instance.transform.childCount - 1; i++)
|
|
{
|
|
if (_upgradeList.Count > i && _upgradeList[i] != null)
|
|
{
|
|
GameObject upgradeEnfant = _instance.transform.Find("Upgrade" + (i + 1)).gameObject;
|
|
if (upgradeEnfant.TryGetComponent<UpgradePlacementButton>(out UpgradePlacementButton button))
|
|
{
|
|
button.Initialize(_upgradeList[i], gameObject, canvas);
|
|
button.GetComponent<Selectable>().Select();
|
|
button.gameObject.AddComponent<DeselectedAction>().Init(_instance);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (_instance != null)
|
|
{
|
|
Debug.Log("Destroying upgrade UI");
|
|
Destroy(_instance);
|
|
_instance = null;
|
|
}
|
|
}
|
|
|
|
//public void OnDeselect(BaseEventData eventData)
|
|
//{
|
|
// if (_instance != null)
|
|
// {
|
|
// Debug.Log("Destroying upgrade UI");
|
|
// Destroy(_instance);
|
|
// _instance = null;
|
|
// }
|
|
//}
|
|
}
|