51 lines
1.6 KiB
C#
51 lines
1.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
using UnityEngine.Events;
|
|
using Cysharp.Threading.Tasks;
|
|
namespace BITKit.UX
|
|
{
|
|
public class UXImage : UXElement<VisualElement>
|
|
{
|
|
[Header(Constant.Header.Settings)]
|
|
[SerializeField] private Optional<float> lerpPosition;
|
|
|
|
[Header(Constant.Header.InternalVariables)]
|
|
private Vector2 currentPosition;
|
|
private VisualElement icon;
|
|
public override void OnStart()
|
|
{
|
|
base.OnStart();
|
|
icon = GetVisualElement().Q(UXConstant.Icon);
|
|
}
|
|
public async void Set(Sprite sprite)
|
|
{
|
|
await UniTask.SwitchToMainThread();
|
|
var _icon = this.icon ?? visualElement;
|
|
_icon.style.backgroundImage =sprite? new StyleBackground(sprite):null;
|
|
}
|
|
public async void Set(Texture2D texture)
|
|
{
|
|
await UniTask.SwitchToMainThread();
|
|
var _icon = this.icon ?? visualElement;
|
|
_icon.style.backgroundImage =texture? new StyleBackground(texture):null;
|
|
}
|
|
|
|
protected override void SetPosition(ref Vector2 position)
|
|
{
|
|
if (currentPosition.sqrMagnitude is 0)
|
|
{
|
|
currentPosition = position;
|
|
}
|
|
|
|
if (!lerpPosition.Allow) return;
|
|
currentPosition = Vector2.Lerp(currentPosition, position, lerpPosition.Value * Time.deltaTime);
|
|
|
|
position = currentPosition;
|
|
}
|
|
|
|
public void SetSprite(Sprite sprite) => Set(sprite);
|
|
public void SetTexture(Texture2D texture2D) => Set(texture2D);
|
|
}
|
|
} |