Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Loading Screen in an unconventional manner

Discussion in 'Scripting' started by wagglies, Sep 3, 2020.

  1. wagglies

    wagglies

    Joined:
    Jul 9, 2020
    Posts:
    9
    so as the title says, i managed to choose an unconventional way of having my loading screen work. my plan is to have a 6s walking animation comprised of 3 sprites at 40fps. the way im doing it is using different scenes that all go back to my build index however i cant get the script to work and i know part of it is wrong and i have no idea what to do lol.

    Script (ik there are brackets missing in this snippet, i have other code that is commented out below this part):
    Code (CSharp):
    1.     void wait()
    2.     {
    3.         IEnumerator wait()
    4.         {
    5.             yield return new WaitForSeconds(0.4);
    6.         }
    7.     }
    8.  
    9.  
    10.     void walking()
    11.     {
    12.         //int previousWalk = 0;
    13.        
    14.         for(int walkAn = 0; walkAn < 4; walkAn++)
    15.         {
    16.         //making wait function work somehow on 04/09/2020
    17.             void wait()
    18.             Application.LoadLevel("loading screen 2");
    19.             void wait()
    20.             Application.LoadLevel("loading screen 3");
    21.             void wait()
    22.             Application.LoadLevel("loading screen 2");
    23.             void wait()
    24.             Application.LoadLevel("loading screen1");
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Ok there's a lot that's wrong here...
    • Application.LoadLevel has been obsolete for a very long time. You should be using
      SceneManager.LoadScene
      https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html
    • This is definitely not valid C#. You no doubt have a lot of compile errors. What's with the random
      void wait()
      s in the middle of your code? Are you trying to call the
      wait
      function? You don't include the return type (void in this case) when invoking a function, you just need the name and the parentheses, e.g.
      wait()
      . You're also missing a semicolon on each of those lines.
    • You have a
      wait()
      function, and then inside that function you're declaring another
      wait
      function that is a local function inside the first. That isn't going to actually do anything.
    • Even if this did compile, there's a lot wrong with your understanding of coroutines and the syntax for coroutines. After you start a coroutine, the code that started it is going to just keep on running. It's not going to wait for the coroutine to yield (no delay will happen).
    • Why does your loading screen consist of multiple different scenes? What's in each scene?
    We should step back - what is it you're actually trying to achieve here?
     
    Yoreki and Bunny83 like this.
  3. wagglies

    wagglies

    Joined:
    Jul 9, 2020
    Posts:
    9
    so aha XD
    i have 3 scenes in which im trying to switch between to create a 6 second animation (a stupid as hell way to do it i know.) each scene comprises different sprites (we shall name them walk1, walk2 and walk3 for the time being)

    now, the whole reason im doing it like this is because im too lazy to figure out how to get the video player to work so that only a single scene is required (i attempted to use it with an mp4 file and it didnt actually show up on the canvas so....moved on pretty quickly from it not gonna lie.)

    i learnt the hard way that unity doesn't support .gif files which is what my original thing was. i honestly have no idea what im doing ahahaha
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Loading a scene is an expensive process. Using it for animations is a bad idea because you can't count on the speed of the loads to be fast enough or consistent enough to make the animation look good. Also, it defeats the entire purpose of a loading screen, because Unity is working to load your animation scenes instead of loading the scene that the loading screen is providing cover for.

    You can make a simple sprite-based animation by using using a script to change which sprite is being displayed as time passes, or (if you want to avoid coding) by Unity's animation system.
     
  5. wagglies

    wagglies

    Joined:
    Jul 9, 2020
    Posts:
    9
    how would you go about using the script to change which sprite is displayed? as stated before its only gonna be a simple animation and literally only has 3 sprites that need to be switched between a total of 16 times. however, what i cant really get is it going in the order needed (12321 etc.)
     
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,134
    Currently in the middle of something but I created a script for handling the animation with comments where needed to explain what the script is doing. Just assign the sprites you want rendered through the script's inspector. If you need more details on what's happening let me know. I've linked the relevant sections of the reference manual.

    https://docs.unity3d.com/ScriptReference/SpriteRenderer.html
    https://docs.unity3d.com/ScriptReference/Sprite.html

    Code (csharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class WalkingAnimation : MonoBehaviour
    5. {
    6.     public SpriteRenderer renderer;  // Component that handles the rendering of the sprite
    7.     public Sprite[] sprites;         // Array that holds all of the sprites to be rendered
    8.  
    9.     private int currentSprite;
    10.  
    11.     public void Start()
    12.     {
    13.         renderer = GetComponent<SpriteRenderer>();
    14.  
    15.         StartCoroutine(PlayAnimation);
    16.     }
    17.  
    18.     public IEnumerator PlayAnimation()
    19.     {
    20.         // Cache the delay (one second divided by 40 FPS)
    21.         WaitForSeconds delay = new WaitForSeconds(1000.0f / 40.0f);
    22.  
    23.         while (true)
    24.         {
    25.             // Pass the sprite you want rendered to the SpriteRenderer
    26.             renderer.sprite = sprites[currentSprite];
    27.  
    28.             // Select the next sprite to be displayed (reset if we've reached the end of the array)
    29.             if (currentSprite = sprites.Length)
    30.                 currentSprite = 0;
    31.             else
    32.                 currentSprite++;
    33.  
    34.             yield return delay;
    35.         }
    36.     }
    37. }
     
    Last edited: Sep 4, 2020
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Scenes also don't load until the next frame after the frame you issue the scene manager load on.

    Just use a sprite animation: emit your three frames of images, mark them as 2D Sprite import, then select all of them at once and drag them into the scene.

    When you do that Unity will auto-create an animation and animator controller to make the three sprites cycle. You can adjust the speed and whatnot by twiddling the animation directly.
     
    Ryiah and PraetorBlue like this.