поигрался с корутинами
This commit is contained in:
1 parent
02a1d74b2d
commit
2746850727
6 files changed
+165
-21
No files matched your search
@@ -0,0 +1,66 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace _Project.Scripts
|
||||
{//Only for learning ideas
|
||||
public class ColorChanger : MonoBehaviour
|
||||
{
|
||||
|
||||
Coroutine coroutine;
|
||||
Image image;
|
||||
[SerializeField] GameObject target;
|
||||
private Color prevColor;
|
||||
[SerializeField] private float TimeForChange;
|
||||
[SerializeField] private bool isStarted = false;
|
||||
private bool isWorking = false;
|
||||
[SerializeField]private bool isLooped = false;
|
||||
|
||||
void Start()
|
||||
{
|
||||
image = target.GetComponent<Image>();
|
||||
prevColor = image.color;
|
||||
}
|
||||
void Update()
|
||||
{
|
||||
if (isStarted && !isWorking && isLooped)
|
||||
{
|
||||
while (isLooped)
|
||||
{
|
||||
isStarted = false;
|
||||
isWorking = true;
|
||||
coroutine = StartCoroutine(ColorCoroutine());
|
||||
}
|
||||
}
|
||||
if (isStarted && !isWorking)
|
||||
{
|
||||
isStarted = false;
|
||||
isWorking = true;
|
||||
coroutine = StartCoroutine(ColorCoroutine());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
IEnumerator ColorCoroutine()
|
||||
{
|
||||
float elapsedTime = 0f;
|
||||
|
||||
while (elapsedTime < TimeForChange)
|
||||
{
|
||||
image.color = Color.Lerp(prevColor, Color.clear, elapsedTime / TimeForChange);
|
||||
elapsedTime += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
elapsedTime = 0f;
|
||||
while (elapsedTime < TimeForChange)
|
||||
{
|
||||
elapsedTime += Time.deltaTime;
|
||||
image.color = Color.Lerp( Color.clear,prevColor, elapsedTime / TimeForChange);
|
||||
yield return null;
|
||||
}
|
||||
coroutine = null;
|
||||
isWorking = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8cf1795fc21943f48878d4a8f1b0a792
|
||||
timeCreated: 1790073521
|
||||
Reference in new issue
Block a user