Skip to content
Snippets Groups Projects
Commit 1c9d87ec authored by Victor's avatar Victor
Browse files

Update how prefabs handle coroutine

parent 703a8801
No related branches found
No related tags found
No related merge requests found
Showing
with 91 additions and 56 deletions
...@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ...@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
## 1.0.4 - 2025-07-03
### Changed
- Update how prefabs handle coroutine.
- Update URP material version.
## 1.0.3 - 2025-07-02 ## 1.0.3 - 2025-07-02
### Fixed ### Fixed
......
...@@ -140,4 +140,4 @@ MonoBehaviour: ...@@ -140,4 +140,4 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3} m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
version: 9 version: 10
...@@ -137,4 +137,4 @@ MonoBehaviour: ...@@ -137,4 +137,4 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3} m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
version: 9 version: 10
...@@ -12,7 +12,7 @@ MonoBehaviour: ...@@ -12,7 +12,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3} m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
version: 9 version: 10
--- !u!21 &2100000 --- !u!21 &2100000
Material: Material:
serializedVersion: 8 serializedVersion: 8
......
...@@ -121,6 +121,7 @@ Material: ...@@ -121,6 +121,7 @@ Material:
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1} - _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _Position: {r: 0, g: 0, b: 0, a: 0} - _Position: {r: 0, g: 0, b: 0, a: 0}
m_BuildTextureStacks: [] m_BuildTextureStacks: []
m_AllowLocking: 1
--- !u!114 &6463663647676341978 --- !u!114 &6463663647676341978
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 11 m_ObjectHideFlags: 11
...@@ -133,4 +134,4 @@ MonoBehaviour: ...@@ -133,4 +134,4 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3} m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
version: 9 version: 10
...@@ -12,7 +12,7 @@ MonoBehaviour: ...@@ -12,7 +12,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3} m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
version: 9 version: 10
--- !u!21 &2100000 --- !u!21 &2100000
Material: Material:
serializedVersion: 8 serializedVersion: 8
...@@ -144,3 +144,4 @@ Material: ...@@ -144,3 +144,4 @@ Material:
- _FrontColor: {r: 5.5908804, g: 0, b: 3.9941745, a: 0.31764707} - _FrontColor: {r: 5.5908804, g: 0, b: 3.9941745, a: 0.31764707}
- _VertexAmount: {r: 0.02, g: 0.02, b: 0.02, a: 0} - _VertexAmount: {r: 0.02, g: 0.02, b: 0.02, a: 0}
m_BuildTextureStacks: [] m_BuildTextureStacks: []
m_AllowLocking: 1
...@@ -12,7 +12,7 @@ MonoBehaviour: ...@@ -12,7 +12,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3} m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
version: 9 version: 10
--- !u!21 &2100000 --- !u!21 &2100000
Material: Material:
serializedVersion: 8 serializedVersion: 8
......
...@@ -12,7 +12,7 @@ MonoBehaviour: ...@@ -12,7 +12,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3} m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
version: 9 version: 10
--- !u!21 &2100000 --- !u!21 &2100000
Material: Material:
serializedVersion: 8 serializedVersion: 8
......
...@@ -121,6 +121,7 @@ Material: ...@@ -121,6 +121,7 @@ Material:
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1} - _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _Position: {r: -2.1472923e-10, g: 0.00000074505806, b: 0.00000074505806, a: 0} - _Position: {r: -2.1472923e-10, g: 0.00000074505806, b: 0.00000074505806, a: 0}
m_BuildTextureStacks: [] m_BuildTextureStacks: []
m_AllowLocking: 1
--- !u!114 &6463663647676341978 --- !u!114 &6463663647676341978
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 11 m_ObjectHideFlags: 11
...@@ -133,4 +134,4 @@ MonoBehaviour: ...@@ -133,4 +134,4 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3} m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
version: 9 version: 10
...@@ -18,19 +18,21 @@ namespace Hyper.Interaction ...@@ -18,19 +18,21 @@ namespace Hyper.Interaction
[SerializeField] protected Transform buttonVisualTransform; [SerializeField] protected Transform buttonVisualTransform;
[SerializeField] protected PokeInteractable pokeInteractable; [SerializeField] protected PokeInteractable pokeInteractable;
protected bool IsHovered; protected bool _isHovered;
protected float StartingButtonVisualZPosition; protected float _startingButtonVisualZPosition;
protected Coroutine _animateActiveStateCoroutine;
private Coroutine _activateAfterDelayCoroutine;
private void Awake() private void Awake()
{ {
StartingButtonVisualZPosition = buttonVisualTransform.localPosition.z; _startingButtonVisualZPosition = buttonVisualTransform.localPosition.z;
} }
private void Update() private void Update()
{ {
if (!IsHovered) return; if (!_isHovered) return;
var normalizedValue = Mathf.InverseLerp(0, StartingButtonVisualZPosition, buttonVisualTransform.localPosition.z); var normalizedValue = Mathf.InverseLerp(0, _startingButtonVisualZPosition, buttonVisualTransform.localPosition.z);
var newScaleValue = Mathf.Lerp(borderPressScale, borderDefaultScale, normalizedValue); var newScaleValue = Mathf.Lerp(borderPressScale, borderDefaultScale, normalizedValue);
borderTransform.localScale = new Vector3(newScaleValue, newScaleValue, 1f); borderTransform.localScale = new Vector3(newScaleValue, newScaleValue, 1f);
...@@ -38,46 +40,46 @@ namespace Hyper.Interaction ...@@ -38,46 +40,46 @@ namespace Hyper.Interaction
public void OnHover() public void OnHover()
{ {
IsHovered = true; _isHovered = true;
} }
public virtual void OnUnhover() public virtual void OnUnhover()
{ {
IsHovered = false; _isHovered = false;
borderTransform.localScale = new Vector3(borderDefaultScale, borderDefaultScale, 1f); borderTransform.localScale = new Vector3(borderDefaultScale, borderDefaultScale, 1f);
} }
public virtual void OnSelect() public virtual void OnSelect()
{ {
StartCoroutine(AnimateBorder()); StartCoroutine(AnimateBorderCoroutine());
} }
public void Activate() public void Activate()
{ {
gameObject.SetActive(true); if (_animateActiveStateCoroutine != null) StopCoroutine(_animateActiveStateCoroutine);
StartCoroutine(AnimateActiveState(-180f, 0, true)); _animateActiveStateCoroutine = StartCoroutine(AnimateActiveStateCoroutine(-180f, 0, true));
} }
public void Deactivate() public void Deactivate()
{ {
if (gameObject.activeSelf) if (_animateActiveStateCoroutine != null) StopCoroutine(_animateActiveStateCoroutine);
StartCoroutine(AnimateActiveState(0, 180f, false)); _animateActiveStateCoroutine = StartCoroutine(AnimateActiveStateCoroutine(0, 180f, false));
} }
public virtual void ActivateAfterDelay(float delay) public void ActivateAfterDelay(float delay)
{ {
transform.localRotation = Quaternion.Euler(-180f, 0, 0); if (_activateAfterDelayCoroutine != null) StopCoroutine(_activateAfterDelayCoroutine);
gameObject.SetActive(true); _activateAfterDelayCoroutine = StartCoroutine(ActivateAfterDelayCoroutine(delay));
StartCoroutine(ActivateAfterDelayCoroutine(delay));
} }
protected IEnumerator ActivateAfterDelayCoroutine(float delay) private IEnumerator ActivateAfterDelayCoroutine(float delay)
{ {
yield return new WaitForSeconds(delay); yield return new WaitForSeconds(delay);
StartCoroutine(AnimateActiveState(-180f, 0, true)); Activate();
_activateAfterDelayCoroutine = null;
} }
protected IEnumerator AnimateBorder() protected IEnumerator AnimateBorderCoroutine()
{ {
var elapsedTime = 0f; var elapsedTime = 0f;
...@@ -93,15 +95,19 @@ namespace Hyper.Interaction ...@@ -93,15 +95,19 @@ namespace Hyper.Interaction
yield return null; yield return null;
} }
if (IsHovered) if (_isHovered)
borderTransform.localScale = new Vector3(borderPressScale, borderPressScale, 1f); borderTransform.localScale = new Vector3(borderPressScale, borderPressScale, 1f);
else else
borderTransform.localScale = new Vector3(borderDefaultScale, borderDefaultScale, 1f); borderTransform.localScale = new Vector3(borderDefaultScale, borderDefaultScale, 1f);
} }
protected virtual IEnumerator AnimateActiveState(float startXRotation, float endXRotation, bool activeState) protected virtual IEnumerator AnimateActiveStateCoroutine(float startXRotation, float endXRotation, bool activeState)
{ {
if (!activeState) if (activeState)
{
pokeInteractable.gameObject.SetActive(true);
}
else
{ {
yield return new WaitForSeconds(borderAnimDuration); yield return new WaitForSeconds(borderAnimDuration);
pokeInteractable.enabled = false; pokeInteractable.enabled = false;
...@@ -123,9 +129,15 @@ namespace Hyper.Interaction ...@@ -123,9 +129,15 @@ namespace Hyper.Interaction
transform.localRotation = Quaternion.Euler(0, 0, 0); transform.localRotation = Quaternion.Euler(0, 0, 0);
if (activeState) if (activeState)
{
pokeInteractable.enabled = true; pokeInteractable.enabled = true;
}
else else
gameObject.SetActive(false); {
pokeInteractable.gameObject.SetActive(false);
}
_animateActiveStateCoroutine = null;
} }
} }
} }
...@@ -23,7 +23,7 @@ namespace Hyper.Interaction ...@@ -23,7 +23,7 @@ namespace Hyper.Interaction
private void Awake() private void Awake()
{ {
StartingButtonVisualZPosition = buttonVisualTransform.localPosition.z; _startingButtonVisualZPosition = buttonVisualTransform.localPosition.z;
_greyCircleImage = greyCircleGameObject.GetComponent<Image>(); _greyCircleImage = greyCircleGameObject.GetComponent<Image>();
_greenCircleImage = greenCircleGameObject.GetComponent<Image>(); _greenCircleImage = greenCircleGameObject.GetComponent<Image>();
_greyCircleRectTransform = greyCircleGameObject.GetComponent<RectTransform>(); _greyCircleRectTransform = greyCircleGameObject.GetComponent<RectTransform>();
...@@ -38,8 +38,8 @@ namespace Hyper.Interaction ...@@ -38,8 +38,8 @@ namespace Hyper.Interaction
_isHolding = false; _isHolding = false;
buttonBorderHoldGameObject.SetActive(false); buttonBorderHoldGameObject.SetActive(false);
borderTransform.gameObject.SetActive(true); borderTransform.gameObject.SetActive(true);
StartCoroutine(AnimateBorder()); StartCoroutine(AnimateBorderCoroutine());
StartCoroutine(ResetButton()); StartCoroutine(ResetButtonCoroutine());
whenHoldDone?.Invoke(); whenHoldDone?.Invoke();
} }
...@@ -59,9 +59,9 @@ namespace Hyper.Interaction ...@@ -59,9 +59,9 @@ namespace Hyper.Interaction
_greenCircleImage.fillAmount = 0; _greenCircleImage.fillAmount = 0;
} }
if (!IsHovered) return; if (!_isHovered) return;
var normalizedValue = Mathf.InverseLerp(0, StartingButtonVisualZPosition, buttonVisualTransform.localPosition.z); var normalizedValue = Mathf.InverseLerp(0, _startingButtonVisualZPosition, buttonVisualTransform.localPosition.z);
var newScaleValue = Mathf.Lerp(borderPressScale, borderDefaultScale, normalizedValue); var newScaleValue = Mathf.Lerp(borderPressScale, borderDefaultScale, normalizedValue);
_greyCircleRectTransform.localScale = new Vector3(newScaleValue, newScaleValue, 1f); _greyCircleRectTransform.localScale = new Vector3(newScaleValue, newScaleValue, 1f);
...@@ -69,7 +69,7 @@ namespace Hyper.Interaction ...@@ -69,7 +69,7 @@ namespace Hyper.Interaction
public override void OnUnhover() public override void OnUnhover()
{ {
IsHovered = false; _isHovered = false;
_greyCircleRectTransform.localScale = new Vector3(borderDefaultScale, borderDefaultScale, 1f); _greyCircleRectTransform.localScale = new Vector3(borderDefaultScale, borderDefaultScale, 1f);
} }
...@@ -83,15 +83,7 @@ namespace Hyper.Interaction ...@@ -83,15 +83,7 @@ namespace Hyper.Interaction
_isHolding = false; _isHolding = false;
} }
public override void ActivateAfterDelay(float delay) private IEnumerator ResetButtonCoroutine()
{
_greyCircleImage.enabled = false;
transform.localRotation = Quaternion.Euler(-180f, 0, 0);
gameObject.SetActive(true);
StartCoroutine(ActivateAfterDelayCoroutine(delay));
}
private IEnumerator ResetButton()
{ {
yield return new WaitForSeconds(borderAnimDuration); yield return new WaitForSeconds(borderAnimDuration);
...@@ -102,9 +94,14 @@ namespace Hyper.Interaction ...@@ -102,9 +94,14 @@ namespace Hyper.Interaction
_isHoldDone = false; _isHoldDone = false;
} }
protected override IEnumerator AnimateActiveState(float startXRotation, float endXRotation, bool activeState) protected override IEnumerator AnimateActiveStateCoroutine(float startXRotation, float endXRotation, bool activeState)
{ {
if (!activeState) if (activeState)
{
_greyCircleImage.enabled = false;
pokeInteractable.gameObject.SetActive(true);
}
else
{ {
yield return new WaitForSeconds(borderAnimDuration); yield return new WaitForSeconds(borderAnimDuration);
pokeInteractable.enabled = false; pokeInteractable.enabled = false;
...@@ -128,9 +125,15 @@ namespace Hyper.Interaction ...@@ -128,9 +125,15 @@ namespace Hyper.Interaction
transform.localRotation = Quaternion.Euler(0, 0, 0); transform.localRotation = Quaternion.Euler(0, 0, 0);
if (activeState) if (activeState)
{
pokeInteractable.enabled = true; pokeInteractable.enabled = true;
}
else else
gameObject.SetActive(false); {
pokeInteractable.gameObject.SetActive(false);
}
_animateActiveStateCoroutine = null;
} }
} }
} }
...@@ -13,6 +13,7 @@ namespace Hyper.Interaction ...@@ -13,6 +13,7 @@ namespace Hyper.Interaction
private Material _material; private Material _material;
private Color _color; private Color _color;
private Coroutine _animateCoroutine;
private void Start() private void Start()
{ {
...@@ -22,15 +23,17 @@ namespace Hyper.Interaction ...@@ -22,15 +23,17 @@ namespace Hyper.Interaction
public void OnHover() public void OnHover()
{ {
StartCoroutine(Animate(unhoverScale, hoverScale, 0, 1f, animDuration)); if (_animateCoroutine != null) StopCoroutine(_animateCoroutine);
_animateCoroutine = StartCoroutine(AnimateCoroutine(unhoverScale, hoverScale, 0, 1f, animDuration));
} }
public void OnUnhover() public void OnUnhover()
{ {
StartCoroutine(Animate(hoverScale, unhoverScale, 1f, 0, animDuration)); if (_animateCoroutine != null) StopCoroutine(_animateCoroutine);
_animateCoroutine = StartCoroutine(AnimateCoroutine(hoverScale, unhoverScale, 1f, 0, animDuration));
} }
private IEnumerator Animate( private IEnumerator AnimateCoroutine(
float startScale, float endScale, float startScale, float endScale,
float startAlpha, float endAlpha, float startAlpha, float endAlpha,
float duration float duration
...@@ -52,6 +55,8 @@ namespace Hyper.Interaction ...@@ -52,6 +55,8 @@ namespace Hyper.Interaction
yield return null; yield return null;
} }
transform.localScale = new Vector3(endScale, endScale, 1f); transform.localScale = new Vector3(endScale, endScale, 1f);
_animateCoroutine = null;
} }
} }
} }
...@@ -13,6 +13,7 @@ namespace Hyper.Interaction ...@@ -13,6 +13,7 @@ namespace Hyper.Interaction
[SerializeField] private AnimationCurve animCurve; [SerializeField] private AnimationCurve animCurve;
private Material _material; private Material _material;
private Coroutine _animateCoroutine;
private void Start() private void Start()
{ {
...@@ -21,15 +22,17 @@ namespace Hyper.Interaction ...@@ -21,15 +22,17 @@ namespace Hyper.Interaction
public void OnHover() public void OnHover()
{ {
StartCoroutine(Animate(unhoverRotation, hoverRotation, 0, 1f, animDuration)); if (_animateCoroutine != null) StopCoroutine(_animateCoroutine);
_animateCoroutine = StartCoroutine(AnimateCoroutine(unhoverRotation, hoverRotation, 0, 1f, animDuration));
} }
public void OnUnhover() public void OnUnhover()
{ {
StartCoroutine(Animate(hoverRotation, unhoverRotation, 1f, 0, animDuration)); if (_animateCoroutine != null) StopCoroutine(_animateCoroutine);
_animateCoroutine = StartCoroutine(AnimateCoroutine(hoverRotation, unhoverRotation, 1f, 0, animDuration));
} }
private IEnumerator Animate( private IEnumerator AnimateCoroutine(
Vector3 startRotation, Vector3 endRotation, Vector3 startRotation, Vector3 endRotation,
float startAlpha, float endAlpha, float startAlpha, float endAlpha,
float duration float duration
...@@ -52,6 +55,8 @@ namespace Hyper.Interaction ...@@ -52,6 +55,8 @@ namespace Hyper.Interaction
yield return null; yield return null;
} }
transform.rotation = Quaternion.Euler(endRotation); transform.rotation = Quaternion.Euler(endRotation);
_animateCoroutine = null;
} }
} }
} }
{ {
"name": "de.tu-dresden.hyper.interaction", "name": "de.tu-dresden.hyper.interaction",
"version": "1.0.3", "version": "1.0.4",
"displayName": "HYPER Interaction", "displayName": "HYPER Interaction",
"description": "This package provides ready-to-use interaction components.", "description": "This package provides ready-to-use interaction components.",
"unity": "6000.0", "unity": "6000.0",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment