Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs

Made a script to turn off DoodleAnimator components when not onscreen

A topic by wedgiebee created 14 days ago Views: 16
Viewing posts 1 to 1

(Not a new feature or a suggestion but thought it seemed like the right place to put this.) This is a script t to turn off DoodleAnimator components when not onscreen in case you have a bunch of doodles and they're slowing things down. I thought that's what was happening to me but it actually WASN'T, but I figured I'd share the script anyway!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DoodleStudio95;
public class DoodleHider : MonoBehaviour
{
    [Header("References")]
    public SpriteRenderer spriteRenderer;
    public DoodleAnimator doodleAnimator;
    private void Awake()
    {
        Hide();
    }
    private void OnBecameInvisible()
    {
        Hide();
    }
    protected void Hide()
    {
        if (spriteRenderer.enabled)
        {
            doodleAnimator.enabled = false;
            spriteRenderer.sprite = doodleAnimator.Sprites[0]; // This line makes it so the SpriteRenderer still has the correct dimensions and knows when it really is or isn't visible
        }
    }
    private void OnBecameVisible()
    {
        Show();
    }
    protected void Show()
    {
        doodleAnimator.enabled = true;
    }
}