Repository navigation
Version 0.4
🧪 What's new? 9/20/24
🧪 1) SchedulableBehaviour Class:
Manages method execution order and timing to optimize performance and update frequency.
Methods can be called in a specific sequence using DiscreteUpdateAttribute.
Supports customizable update intervals using EnumerableDiscreteUpdate for better frame management.
Why Use SchedulableBehaviour?
In game development, optimizing performance and managing updates efficiently is crucial. The Update method in Unity runs every frame, which can lead to performance issues if all updates are processed simultaneously. Using SchedulableBehaviour and DiscreteUpdateAttribute allows you to:
a) Prioritize Critical Updates:
Ensure that high-priority updates occur before less critical ones, reducing potential frame rate drops.
b) Reduce Update Frequency:
Optimize performance by controlling how often certain updates are executed.
c) Balance Load:
Distribute computationally intensive tasks across multiple frames to avoid spikes in resource usage.
EnumerableDiscreteUpdate Example:
using UnityEngine;
public class TestCode : SchedulableBehaviour
{
private float timer;
private const float interval = 1f;
//This example has some issues but is just given to help you understand how to use it.
protected override IEnumerator EnumerableDiscreteUpdate()
{
timer += Time.deltaTime;
if (timer >= interval)
{
Camera.main.backgroundColor = Color.blue;
timer = 0f;
}
yield return 1; // wait 1 frame to continue
Vector3 position = transform.position;
position.x += Mathf.Sin(Time.time) * 0.1f;
transform.position = position;
yield return 1;
float rotationSpeed = 50f;
transform.Rotate(Vector3.up, rotationSpeed * Time.deltaTime);
yield return 1;
GameObject uiElement = GameObject.Find("UIElement");
if (uiElement != null)
{
yield return 1;
RectTransform rectTransform = uiElement.GetComponent<RectTransform>();
rectTransform.anchoredPosition = new Vector2(Screen.width / 2, Screen.height / 2);
}
}
}EnumerableDiscreteUpdate Example:
using UnityEngine;
public class CharacterController : SchedulableBehaviour
{
private Vector3 moveDirection;
private Animator animator;
private bool isInteracting;
private void Start()
{
animator = GetComponent<Animator>();
}
[DiscreteUpdate(1)] // Update every => currentFrame % 3 == 0
void UpdateMovement()
{
float moveInput = Input.GetAxis("Horizontal");
moveDirection = new Vector3(moveInput, 0, 0) * Time.deltaTime * 5f;
transform.position += moveDirection;
animator.SetFloat("Speed", Mathf.Abs(moveInput));
}
[DiscreteUpdate(2)] // Update every => currentFrame % 3 == 1
void UpdateAnimation()
{
if (animator != null)
{
float speed = animator.GetFloat("Speed");
animator.SetFloat("AnimationSpeed", speed);
}
}
[DiscreteUpdate(3)] // Update every => currentFrame % 3 == 2
void UpdateInteractions()
{
if (Input.GetKeyDown(KeyCode.E))
{
isInteracting = !isInteracting;
Debug.Log($"Interaction: {isInteracting}");
}
if (isInteracting)
{
Debug.Log("The character interacts with an object.");
}
}
}