284 lines
6.2 KiB
C#
284 lines
6.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace EnergyBarToolkit
|
|
{
|
|
public abstract class EnergyBarUGUIBase : EnergyBarBase
|
|
{
|
|
[Serializable]
|
|
public class SpriteTex : AbstractTex
|
|
{
|
|
public Sprite sprite;
|
|
|
|
public Material material;
|
|
|
|
public SpriteTex()
|
|
{
|
|
}
|
|
|
|
public SpriteTex(Sprite sprite)
|
|
{
|
|
this.sprite = sprite;
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
int currentHash = 37;
|
|
currentHash = MadHashCode.Add(currentHash, (sprite != null) ? sprite.GetInstanceID() : 0);
|
|
currentHash = MadHashCode.Add(currentHash, (material != null) ? material.GetInstanceID() : 0);
|
|
return MadHashCode.Add(currentHash, color);
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class SpriteBind
|
|
{
|
|
public Image image;
|
|
|
|
public int index;
|
|
|
|
public SpriteBind(Image image, int index)
|
|
{
|
|
this.image = image;
|
|
this.index = index;
|
|
}
|
|
}
|
|
|
|
public List<SpriteTex> spritesBackground = new List<SpriteTex>();
|
|
|
|
public List<SpriteTex> spritesForeground = new List<SpriteTex>();
|
|
|
|
public Text label;
|
|
|
|
public SpriteTex effectBurnSprite;
|
|
|
|
public bool debugMode;
|
|
|
|
[SerializeField]
|
|
protected List<GameObject> createdChildren = new List<GameObject>(32);
|
|
|
|
[SerializeField]
|
|
protected List<SpriteBind> backgroundBinds = new List<SpriteBind>(16);
|
|
|
|
[SerializeField]
|
|
protected List<SpriteBind> foregroundBinds = new List<SpriteBind>(16);
|
|
|
|
protected RectTransform rectTransform;
|
|
|
|
public override Rect DrawAreaRect
|
|
{
|
|
get
|
|
{
|
|
return default(Rect);
|
|
}
|
|
}
|
|
|
|
public abstract void SetNativeSize();
|
|
|
|
protected override void OnEnable()
|
|
{
|
|
base.OnEnable();
|
|
rectTransform = GetComponent<RectTransform>();
|
|
}
|
|
|
|
protected override void Update()
|
|
{
|
|
base.Update();
|
|
UpdateLabel();
|
|
UpdateBackgroundForegroundColor();
|
|
if (Application.isEditor)
|
|
{
|
|
UpdateDebugMode(debugMode);
|
|
}
|
|
}
|
|
|
|
private void UpdateBackgroundForegroundColor()
|
|
{
|
|
UpdateColor(backgroundBinds, spritesBackground);
|
|
UpdateColor(foregroundBinds, spritesForeground);
|
|
}
|
|
|
|
private void UpdateColor(List<SpriteBind> binds, List<SpriteTex> sprites)
|
|
{
|
|
for (int i = 0; i < binds.Count; i++)
|
|
{
|
|
SpriteBind spriteBind = binds[i];
|
|
Image image = spriteBind.image;
|
|
int index = spriteBind.index;
|
|
if (sprites.Count > index)
|
|
{
|
|
SpriteTex spriteTex = sprites[index];
|
|
image.color = ComputeColor(spriteTex.color);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void UpdateLabel()
|
|
{
|
|
if (label != null)
|
|
{
|
|
label.text = LabelFormatResolve(labelFormat);
|
|
}
|
|
}
|
|
|
|
protected void BuildBackgroundImages()
|
|
{
|
|
BuildImagesFromList(spritesBackground, "bg_", ref backgroundBinds);
|
|
}
|
|
|
|
protected void BuildForegroundImages()
|
|
{
|
|
BuildImagesFromList(spritesForeground, "fg_", ref foregroundBinds);
|
|
}
|
|
|
|
private void BuildImagesFromList(List<SpriteTex> sprites, string prefix, ref List<SpriteBind> store)
|
|
{
|
|
for (int i = 0; i < sprites.Count; i++)
|
|
{
|
|
SpriteTex spriteTex = sprites[i];
|
|
Sprite sprite = spriteTex.sprite;
|
|
if (!(sprite == null))
|
|
{
|
|
Image image = CreateChild<Image>(prefix + (i + 1));
|
|
image.sprite = sprite;
|
|
image.material = spriteTex.material;
|
|
image.SetNativeSize();
|
|
image.type = GetImageType();
|
|
image.color = spriteTex.color;
|
|
store.Add(new SpriteBind(image, i));
|
|
}
|
|
}
|
|
}
|
|
|
|
protected virtual Image.Type GetImageType()
|
|
{
|
|
return Image.Type.Simple;
|
|
}
|
|
|
|
protected T CreateChild<T>(string childName) where T : Component
|
|
{
|
|
return CreateChild<T>(childName, base.transform);
|
|
}
|
|
|
|
protected T CreateChild<T>(string childName, Transform parent) where T : Component
|
|
{
|
|
T result = MadTransform.CreateChild<T>(parent, "generated_" + childName);
|
|
createdChildren.Add(result.gameObject);
|
|
SetAsLastGenerated(result.transform);
|
|
ApplyDebugMode(result.gameObject, debugMode);
|
|
return result;
|
|
}
|
|
|
|
private void SetAsLastGenerated(Transform child)
|
|
{
|
|
int num = NonGeneratedLowestIndex();
|
|
if (num != -1)
|
|
{
|
|
child.SetSiblingIndex(num);
|
|
}
|
|
else
|
|
{
|
|
child.SetAsLastSibling();
|
|
}
|
|
}
|
|
|
|
private int NonGeneratedLowestIndex()
|
|
{
|
|
int num = int.MaxValue;
|
|
for (int i = 0; i < base.transform.childCount; i++)
|
|
{
|
|
Transform child = base.transform.GetChild(i);
|
|
if (!IsGenerated(child))
|
|
{
|
|
num = Mathf.Min(child.GetSiblingIndex(), num);
|
|
}
|
|
}
|
|
if (num == int.MaxValue)
|
|
{
|
|
return -1;
|
|
}
|
|
return num;
|
|
}
|
|
|
|
private bool IsGenerated(Transform child)
|
|
{
|
|
return child.name.StartsWith("generated_");
|
|
}
|
|
|
|
protected void RemoveCreatedChildren()
|
|
{
|
|
for (int i = 0; i < createdChildren.Count; i++)
|
|
{
|
|
MadGameObject.SafeDestroy(createdChildren[i]);
|
|
}
|
|
List<Transform> list = MadTransform.FindChildren(base.transform, (Transform c) => c.name.StartsWith("generated_"), 0);
|
|
for (int num = 0; num < list.Count; num++)
|
|
{
|
|
Transform transform = list[num];
|
|
MadGameObject.SafeDestroy(transform.gameObject);
|
|
}
|
|
createdChildren.Clear();
|
|
backgroundBinds.Clear();
|
|
foregroundBinds.Clear();
|
|
}
|
|
|
|
protected void SetSize(RectTransform rt, float w, float h)
|
|
{
|
|
SetSize(rt, new Vector2(w, h));
|
|
}
|
|
|
|
protected void SetSize(RectTransform rt, Vector2 size)
|
|
{
|
|
Rect rect = rt.rect;
|
|
if (!Mathf.Approximately(rect.width, size.x))
|
|
{
|
|
rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size.x);
|
|
}
|
|
if (!Mathf.Approximately(rect.height, size.y))
|
|
{
|
|
rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size.y);
|
|
}
|
|
}
|
|
|
|
protected override bool IsVisible()
|
|
{
|
|
if (!base.IsVisible())
|
|
{
|
|
return false;
|
|
}
|
|
if (rectTransform.rect.size.x <= 0f || rectTransform.rect.size.y <= 0f)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
protected void MoveLabelToTop()
|
|
{
|
|
if (!(label == null) && label.transform.parent == base.transform)
|
|
{
|
|
label.transform.SetAsLastSibling();
|
|
}
|
|
}
|
|
|
|
private void UpdateDebugMode(bool value)
|
|
{
|
|
for (int i = 0; i < createdChildren.Count; i++)
|
|
{
|
|
GameObject gameObject = createdChildren[i];
|
|
if (gameObject != null)
|
|
{
|
|
ApplyDebugMode(gameObject.gameObject, value);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ApplyDebugMode(GameObject gameObject, bool enabled)
|
|
{
|
|
gameObject.hideFlags = ((!enabled) ? HideFlags.HideInHierarchy : HideFlags.None);
|
|
}
|
|
}
|
|
}
|