242 lines
5.6 KiB
C#
242 lines
5.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace EnergyBarToolkit
|
|
{
|
|
[ExecuteInEditMode]
|
|
[RequireComponent(typeof(EnergyBar))]
|
|
public class TransformRendererUGUI : EnergyBarUGUIBase
|
|
{
|
|
public SpriteTex spriteObject = new SpriteTex();
|
|
|
|
public Vector2 spriteObjectPivot = new Vector2(0.5f, 0.5f);
|
|
|
|
public bool transformTranslate;
|
|
|
|
public bool transformRotate;
|
|
|
|
public bool transformScale;
|
|
|
|
public TranslateFunction translateFunction = new TranslateFunction();
|
|
|
|
public RotateFunction rotateFunction = new RotateFunction();
|
|
|
|
public ScaleFunction scaleFunction = new ScaleFunction();
|
|
|
|
[SerializeField]
|
|
private const int BuildVersion = 1;
|
|
|
|
[SerializeField]
|
|
private int lastRebuildHash;
|
|
|
|
[SerializeField]
|
|
private bool dirty;
|
|
|
|
[SerializeField]
|
|
private Image imageObject;
|
|
|
|
[SerializeField]
|
|
private Transform imageObjectContainer;
|
|
|
|
public override void SetNativeSize()
|
|
{
|
|
Vector2? vector = ComputeNativeSize();
|
|
if (vector.HasValue)
|
|
{
|
|
SetSize(rectTransform, vector.Value);
|
|
}
|
|
}
|
|
|
|
private Vector2? ComputeNativeSize()
|
|
{
|
|
Sprite sprite = FirstBackgroundOrForegroundSprite();
|
|
if (sprite == null)
|
|
{
|
|
Rebuild();
|
|
if (sprite == null)
|
|
{
|
|
Debug.LogWarning("Cannot resize bar that has not been created yet");
|
|
return null;
|
|
}
|
|
}
|
|
return new Vector2(sprite.rect.width, sprite.rect.height);
|
|
}
|
|
|
|
private Sprite FirstBackgroundOrForegroundSprite()
|
|
{
|
|
for (int i = 0; i < spritesBackground.Count; i++)
|
|
{
|
|
SpriteTex spriteTex = spritesBackground[i];
|
|
if (spriteTex != null && spriteTex.sprite != null)
|
|
{
|
|
return spriteTex.sprite;
|
|
}
|
|
}
|
|
for (int j = 0; j < spritesForeground.Count; j++)
|
|
{
|
|
SpriteTex spriteTex2 = spritesForeground[j];
|
|
if (spriteTex2 != null && spriteTex2.sprite != null)
|
|
{
|
|
return spriteTex2.sprite;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
protected override void Update()
|
|
{
|
|
base.Update();
|
|
UpdateRebuild();
|
|
if (IsValid())
|
|
{
|
|
UpdateColor();
|
|
UpdateAnchor();
|
|
UpdateSize();
|
|
UpdateTransform();
|
|
}
|
|
}
|
|
|
|
private void UpdateAnchor()
|
|
{
|
|
imageObject.rectTransform.pivot = spriteObjectPivot;
|
|
}
|
|
|
|
private void UpdateSize()
|
|
{
|
|
RectTransform component = GetComponent<RectTransform>();
|
|
for (int i = 0; i < createdChildren.Count; i++)
|
|
{
|
|
GameObject gameObject = createdChildren[i];
|
|
if (!(gameObject.gameObject == imageObject.gameObject))
|
|
{
|
|
RectTransform component2 = gameObject.GetComponent<RectTransform>();
|
|
if (component2 != null)
|
|
{
|
|
SetSize(component2, component.rect.size);
|
|
}
|
|
}
|
|
}
|
|
Vector2 nativeSize;
|
|
if (TryGetNativeSize(out nativeSize))
|
|
{
|
|
Vector2 vector = new Vector2(component.rect.width / nativeSize.x, component.rect.height / nativeSize.y);
|
|
MadTransform.SetLocalScale(imageObjectContainer.transform, vector);
|
|
}
|
|
}
|
|
|
|
private bool TryGetNativeSize(out Vector2 nativeSize)
|
|
{
|
|
for (int i = 0; i < createdChildren.Count; i++)
|
|
{
|
|
GameObject gameObject = createdChildren[i];
|
|
if (!(gameObject.gameObject == imageObject.gameObject))
|
|
{
|
|
Image component = gameObject.GetComponent<Image>();
|
|
if (component != null)
|
|
{
|
|
nativeSize = component.sprite.rect.size;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
nativeSize = Vector2.zero;
|
|
return false;
|
|
}
|
|
|
|
private void UpdateColor()
|
|
{
|
|
if (imageObject != null)
|
|
{
|
|
imageObject.color = ComputeColor(spriteObject.color);
|
|
imageObject.material = spriteObject.material;
|
|
}
|
|
}
|
|
|
|
private void UpdateTransform()
|
|
{
|
|
if (transformTranslate)
|
|
{
|
|
Vector2 vector = translateFunction.Value(ValueF2);
|
|
Vector2 nativeSize;
|
|
if (TryGetNativeSize(out nativeSize))
|
|
{
|
|
imageObject.transform.localPosition = new Vector2(vector.x * nativeSize.x, vector.y * nativeSize.y);
|
|
}
|
|
}
|
|
if (transformRotate)
|
|
{
|
|
Quaternion rotation = rotateFunction.Value(ValueF2);
|
|
Quaternion quaternion = Quaternion.identity * Quaternion.Inverse(rotation);
|
|
if (imageObject.transform.localRotation != quaternion)
|
|
{
|
|
imageObject.transform.localRotation = quaternion;
|
|
}
|
|
}
|
|
if (transformScale)
|
|
{
|
|
Vector3 localScale = scaleFunction.Value(ValueF2);
|
|
MadTransform.SetLocalScale(imageObject.transform, localScale);
|
|
}
|
|
}
|
|
|
|
private void UpdateRebuild()
|
|
{
|
|
if (!IsValid())
|
|
{
|
|
RemoveCreatedChildren();
|
|
}
|
|
if (RebuildNeeded())
|
|
{
|
|
Rebuild();
|
|
}
|
|
}
|
|
|
|
private bool IsValid()
|
|
{
|
|
return spriteObject.sprite != null;
|
|
}
|
|
|
|
private bool RebuildNeeded()
|
|
{
|
|
int currentHash = 37;
|
|
currentHash = MadHashCode.Add(currentHash, 1);
|
|
currentHash = MadHashCode.Add(currentHash, (spriteObject != null) ? spriteObject.GetHashCode() : 0);
|
|
currentHash = MadHashCode.AddList(currentHash, spritesBackground);
|
|
currentHash = MadHashCode.AddList(currentHash, spritesForeground);
|
|
currentHash = MadHashCode.Add(currentHash, spriteObjectPivot);
|
|
currentHash = MadHashCode.Add(currentHash, label);
|
|
currentHash = MadHashCode.Add(currentHash, effectBurn);
|
|
currentHash = MadHashCode.Add(currentHash, effectBurnSprite);
|
|
currentHash = MadHashCode.Add(currentHash, rectTransform.pivot);
|
|
if (currentHash != lastRebuildHash || dirty)
|
|
{
|
|
lastRebuildHash = currentHash;
|
|
dirty = false;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private void Rebuild()
|
|
{
|
|
if (IsValid())
|
|
{
|
|
RemoveCreatedChildren();
|
|
BuildBackgroundImages();
|
|
BuildObject();
|
|
BuildForegroundImages();
|
|
UpdateSize();
|
|
MoveLabelToTop();
|
|
}
|
|
}
|
|
|
|
private void BuildObject()
|
|
{
|
|
imageObjectContainer = CreateChild<Transform>("container");
|
|
imageObject = CreateChild<Image>("bar", imageObjectContainer);
|
|
imageObject.sprite = spriteObject.sprite;
|
|
imageObject.SetNativeSize();
|
|
}
|
|
}
|
|
}
|