Files
beyond/Assets/Scripts/UI/ItemPanelController.cs
2024-11-20 15:21:28 +01:00

130 lines
3.6 KiB
C#

using Invector.vItemManager;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Beyond
{
public class ItemPanelController : MonoBehaviour
{
[SerializeField]
private Image image;
[SerializeField]
private Sprite defaultSprite;
[SerializeField]
private Button button;
private Color transparentColor = new Color(1, 1, 1, 0.5f);
private Color fullyVisibleColor = new Color(1, 1, 1);
private string imagePath = "";
public string defaultSpritePath = "";
public bool disableObjectOnEmpty = false;
private void OnEnable()
{
if (image.sprite != null)
{
return;
}
//because unloaded in menu image needs to be reloaded again, instead could also add a bool to not remove certain elements in menu
if (imagePath.Length > 1)
{
image.sprite = Resources.Load<Sprite>(imagePath);
EnableImage();
}
else if (defaultSpritePath.Length > 1)
{
image.sprite = Resources.Load<Sprite>(defaultSpritePath);
EnableImage();
// image.enabled = true;
}
}
private void EnableImage()
{
if (disableObjectOnEmpty)
{
gameObject.SetActive(true);
}
else
{
image.enabled = true;
}
}
private void DisableImage()
{
if (disableObjectOnEmpty)
{
gameObject.SetActive(false);
}
else
{
image.enabled = false;
}
}
public void SetPanelToItemImage(bItem item)
{
//different icon in menu and different as use button, only for weapons as of now
if (item.secondaryIconPath.Length > 1 && (item.type == bItemType.Swords || item.type == bItemType.Axes))
{
// UnloadCurrentImage();
imagePath = item.secondaryIconPath;
image.sprite = Resources.Load<Sprite>(item.secondaryIconPath);
}
else
{
// Debug.LogError("hej hej man");
// UnloadCurrentImage();
imagePath = item.iconPath;
image.sprite = Resources.Load<Sprite>(imagePath);
}
EnableImage();
//image.enabled = true;
}
public void SetPanelToDefaultImage(bItem removedItem)
{
imagePath = "";
/// UnloadCurrentImage();
// image.enabled = false;
if (defaultSpritePath.Length > 1)
{
image.sprite = Resources.Load<Sprite>(defaultSpritePath);
EnableImage();
//image.enabled = true;
}
else
{
DisableImage();
}
}
private void UnloadCurrentImage()
{
// Sprite spriteToUnload = image.sprite;
// image.sprite = null;
Resources.UnloadAsset(image.sprite);
//Resources.UnloadUnusedAssets();
}
public void DisableButtonInteraction()
{
button.interactable = false;
image.color = transparentColor;
}
public void EnableButtonInteraction()
{
button.interactable = true;
image.color = fullyVisibleColor;
}
}
}