简体   繁体   中英

How do you temporarily make a sprite disappear in Unity 3D?

In unity, I've added a 2D sprite which has the skin of a booster for my character's rocket boots. I want to make the 2D sprite appear after pressing space and disappear after 1/4 of a second.

How would I turn on and off the visibility of the thruster sprite from C# code?

You may also want to look into using Coroutines . The idea is that it's a method call which may contain delays or waiting periods before the method is "done". See the linked page for a detailed explanation.

Here is a sample coroutine method:

public IEnumerator ShowBoostersForQuarterSecond() {
    spriteRenderer.enabled = true;           //show
    yield return new WaitForSeconds(0.25f);  //wait
    spriteRenderer.enabled = false;          //hide
}

And you would call it by doing

StartCoroutine(ShowBoostersForQuarterSecond());

Note that you might have to do gameObject.renderer instead of spriteRenderer . Or make a field to hold the Sprite Renderer reference, up to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM