Skip to content

Instantly share code, notes, and snippets.

@aprius
Last active November 23, 2022 17:41
Show Gist options
  • Save aprius/04f03ca0afdbae0754580fc65b7f33d0 to your computer and use it in GitHub Desktop.
Save aprius/04f03ca0afdbae0754580fc65b7f33d0 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class BlinkSample1 : MonoBehaviour
{
[SerializeField] private Renderer target;
[SerializeField] private float cycle = 1;
private float _time;
private void Update()
{
_time += Time.deltaTime;
// Lấy giá trị lặp lại theo chu kỳ
// Lấy giá trị trong khoảng từ 0 đến cycle
var repeatValue = Mathf.Repeat(_time, cycle);
target.enabled = repeatValue >= cycle * 0.5f;
}
}
using UnityEngine;
public class BlinkSample2 : MonoBehaviour
{
[SerializeField] private Behaviour target;
[SerializeField] private float cycle = 1;
private float _time;
private void Update()
{
_time += Time.deltaTime;
// Lấy giá trị lặp lại theo chu kỳ
// Lấy giá trị trong khoảng từ 0 đến cycle
var repeatValue = Mathf.Repeat(_time, cycle);
target.enabled = repeatValue >= cycle * 0.5f;
}
}
using UnityEngine;
public class BlinkSample3 : MonoBehaviour
{
[SerializeField] private Renderer target;
[SerializeField] private float cycle = 1;
[SerializeField, Range(0, 1)] private float dutyRate = 0.5f;
private float _time;
private void Update()
{
_time += Time.deltaTime;
// Lấy giá trị lặp lại theo chu kỳ
// Lấy giá trị trong khoảng từ 0 đến cycle
var repeatValue = Mathf.Repeat(_time, cycle);
target.enabled = repeatValue >= cycle * (1 - dutyRate);
}
}
using UnityEngine;
public class BlinkSample4 : MonoBehaviour
{
[SerializeField] private Renderer target;
[SerializeField] private float cycle = 1;
[SerializeField, Range(0, 1)] private float dutyRate = 0.5f;
private float _time;
private Material _material;
private void Awake()
{
_material = target.material;
}
private void Update()
{
_time += Time.deltaTime;
// Lấy giá trị lặp lại theo chu kỳ
// Lấy giá trị trong khoảng từ 0 đến cycle
var repeatValue = Mathf.Repeat(_time, cycle);
var color = _material.color;
color.a = repeatValue >= cycle * (1 - dutyRate) ? 1 : 0;
_material.color = color;
}
private void OnDestroy()
{
// Xóa bỏ material khi bạn không dùng nữa
Destroy(_material);
}
}
using UnityEngine;
public class BlinkSample5 : MonoBehaviour
{
[SerializeField] private SpriteRenderer target;
[SerializeField] private float cycle = 1;
[SerializeField, Range(0, 1)] private float dutyRate = 0.5f;
private float _time;
private void Update()
{
_time += Time.deltaTime;
// Lấy giá trị lặp lại theo chu kỳ
// Lấy giá trị trong khoảng từ 0 đến cycle
var repeatValue = Mathf.Repeat(_time, cycle);
var color = target.color;
color.a = repeatValue >= cycle * (1 - dutyRate) ? 1 : 0;
target.color = color;
}
}
using UnityEngine;
public class BlinkSample6 : MonoBehaviour
{
[SerializeField] private SpriteRenderer target;
[SerializeField] private float cycle = 1;
private float _time;
private void Update()
{
_time += Time.deltaTime;
// Tính toán giá trị alpha theo đồ thị Sine lặp lại theo chu kỳ
var alpha = Mathf.Cos(2 * Mathf.PI * _time / cycle) * 0.5f + 0.5f;
var color = target.color;
color.a = alpha;
target.color = color;
}
}
using System;
using UnityEngine;
public class BlinkSample7 : MonoBehaviour
{
[SerializeField] private SpriteRenderer target;
[SerializeField] private AnimationCurve alphaCurve = AnimationCurve.Linear(0, 0, 1, 1);
private float _time;
private float _cycle;
private void Start()
{
var length = alphaCurve.length;
if (length < 1) return;
_cycle = alphaCurve.keys[length - 1].time;
}
private void Update()
{
_time += Time.deltaTime;
if (_time > _cycle) _time = Mathf.Repeat(_time, _cycle);
var alpha = alphaCurve.Evaluate(_time);
var color = target.color;
color.a = alpha;
target.color = color;
}
}
using UnityEngine;
public class BlinkSample8 : MonoBehaviour
{
[SerializeField] private SpriteRenderer target;
[SerializeField] private float cycle = 1;
private float _time;
private bool _isBlinking;
private float _defaultAlpha;
public void BeginBlink()
{
if (_isBlinking) return;
_isBlinking = true;
_time = 0;
}
public void EndBlink()
{
_isBlinking = false;
SetAlpha(_defaultAlpha);
}
private void Start() { _defaultAlpha = target.color.a; }
private void Update()
{
if (!_isBlinking) return;
_time += Time.deltaTime;
// Lấy giá trị lặp lại theo chu kỳ
// Lấy giá trị trong khoảng từ 0 đến cycle
var repeatValue = Mathf.Repeat(_time, cycle);
SetAlpha(repeatValue >= cycle * 0.5f ? 1 : 0);
}
private void SetAlpha(float alpha)
{
var color = target.color;
color.a = alpha;
target.color = color;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment