136 lines
4.6 KiB
C#
136 lines
4.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using Unheimlich.Utility;
|
|
|
|
namespace Unheimlich.UI.DetectiveBoard
|
|
{
|
|
public class PostItScribbleEffect : MonoBehaviour
|
|
{
|
|
[SerializeField] private Material _scribbleMaterialTemplate;
|
|
[SerializeField] private RectTransform _targetRectTransform;
|
|
|
|
private Image _scribbleOverlay;
|
|
private Material _scribbleMaterialInstance;
|
|
private bool _overlayCreated = false;
|
|
private bool _isEliminated = false;
|
|
|
|
private static readonly int ProgressID = Shader.PropertyToID("_Progress");
|
|
private int _scribbleTweenId = -1;
|
|
|
|
private void Awake()
|
|
{
|
|
if (_targetRectTransform == null)
|
|
_targetRectTransform = GetComponent<RectTransform>();
|
|
}
|
|
|
|
private void CreateScribbleOverlay()
|
|
{
|
|
if (_overlayCreated || _scribbleMaterialTemplate == null || _targetRectTransform == null)
|
|
return;
|
|
|
|
GameObject overlayObject = new GameObject("ScribbleOverlay");
|
|
overlayObject.transform.SetParent(_targetRectTransform, false);
|
|
|
|
_scribbleOverlay = overlayObject.AddComponent<Image>();
|
|
_scribbleOverlay.raycastTarget = false;
|
|
|
|
RectTransform overlayRect = overlayObject.GetComponent<RectTransform>();
|
|
|
|
overlayRect.anchorMin = Vector2.zero;
|
|
overlayRect.anchorMax = Vector2.one;
|
|
overlayRect.sizeDelta = Vector2.zero;
|
|
overlayRect.anchoredPosition = Vector2.zero;
|
|
|
|
_scribbleMaterialInstance = new Material(_scribbleMaterialTemplate);
|
|
_scribbleMaterialInstance.SetFloat(ProgressID, 0f);
|
|
_scribbleOverlay.material = _scribbleMaterialInstance;
|
|
|
|
_scribbleOverlay.gameObject.SetActive(false);
|
|
|
|
_overlayCreated = true;
|
|
}
|
|
|
|
public void SetEliminated(bool eliminated)
|
|
{
|
|
if (_isEliminated == eliminated)
|
|
return;
|
|
|
|
_isEliminated = eliminated;
|
|
|
|
if (eliminated)
|
|
{
|
|
ApplyScribble();
|
|
}
|
|
else
|
|
{
|
|
RemoveScribble();
|
|
}
|
|
}
|
|
|
|
private void ApplyScribble()
|
|
{
|
|
CancelActiveTweens();
|
|
CreateScribbleOverlay();
|
|
|
|
if (_scribbleOverlay == null || _scribbleMaterialInstance == null)
|
|
return;
|
|
|
|
_scribbleOverlay.gameObject.SetActive(true);
|
|
|
|
_scribbleTweenId = LeanTween.value(gameObject, 0f, 1f, 0.4f)
|
|
.setEase(LeanTweenType.easeInOutQuad)
|
|
.setOnUpdate((float val) => {
|
|
if (_scribbleMaterialInstance != null)
|
|
_scribbleMaterialInstance.SetFloat(ProgressID, val);
|
|
})
|
|
.setOnComplete(() => {
|
|
_scribbleTweenId = -1;
|
|
}).id;
|
|
}
|
|
|
|
private void RemoveScribble()
|
|
{
|
|
if (!_overlayCreated || _scribbleOverlay == null)
|
|
return;
|
|
|
|
if (!_scribbleOverlay.gameObject.activeSelf)
|
|
return;
|
|
|
|
CancelActiveTweens();
|
|
|
|
if (_scribbleMaterialInstance != null)
|
|
{
|
|
float currentProgress = _scribbleMaterialInstance.GetFloat(ProgressID);
|
|
|
|
_scribbleTweenId = LeanTween.value(gameObject, currentProgress, 0f, 0.3f)
|
|
.setEase(LeanTweenType.easeOutQuad)
|
|
.setOnUpdate((float val) => {
|
|
if (_scribbleMaterialInstance != null)
|
|
_scribbleMaterialInstance.SetFloat(ProgressID, val);
|
|
})
|
|
.setOnComplete(() => {
|
|
_scribbleTweenId = -1;
|
|
if (_scribbleOverlay != null)
|
|
_scribbleOverlay.gameObject.SetActive(false);
|
|
}).id;
|
|
}
|
|
}
|
|
|
|
private void CancelActiveTweens()
|
|
{
|
|
TweenHelper.CancelTween(ref _scribbleTweenId);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
CancelActiveTweens();
|
|
|
|
if (_scribbleMaterialInstance != null)
|
|
Destroy(_scribbleMaterialInstance);
|
|
|
|
if (_scribbleOverlay != null)
|
|
Destroy(_scribbleOverlay.gameObject);
|
|
}
|
|
}
|
|
}
|