• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Arshww · Aug 25, 2019 at 10:13 PM · preloader

How to make Image preloader script

Hello, i know this could be a "noob" question, that's because i'm newbie using Unity yet haha.

I am creating a project which uses all images inside an array[] to change a specific image; I mean the image will change to another according some frames per second, the problem is that when it happens the project freezes for about 2-4 seconds, i was thinking about creating an Image preloading script, but i certainly haven't idea about how, i'd love to -please-, receive some help about how could i start doing this.


EDIT: well, i wasn't clear at all, i think, so this is what i have for now:

This script should get all images from a specific folder and put them inside the Frames array, then a Main Sprite will be changing continuously according the FPS and exactly in the array order. Bassicaly it's a "Gif player" that's why the script is called Giffier.

when the player kills the enemy, the FolderName is changed to the next's enemy's folder name but when that happens, the game freezes completely for about 2-4 seconds as I said before, i was thinking so much time about it and noticed that it could be a performance problem due to the Frames[] array content change, but also noticed that it doesn't happens when previusly killed enemies are the same than the current (i.e., Player kills EnemyA and EnemyB, if the next enemy is EnemyA or EnemyB the game doesn't freezes but if a new EnemyC spawns, the game freezes)

 public class Giffier : MonoBehaviour
 {
     public Sprite[] Frames;
     public float FramesPerSecond = 30f;
     public string FolderName; // Current Enemy's folder
 
     void Start ()
     {
         GetSprites ();
     }
 
     void Update ()
     {
         GetSprites ();
         // Get index of frame.
         int index = (int)(Time.time * FramesPerSecond) % Frames.Length;
         // Check if the array isn't null.
         if (Frames [index] != null) {
             // get & update sprite.
             this.gameObject.GetComponent<SpriteRenderer> ().sprite = Frames [index];
         }
     }
 
     public void GetSprites ()
     {
         for (int i = 1; i < Frames.Length; i++) {
             Frames [i] = (Sprite)AssetDatabase.LoadAssetAtPath ("Assets/Graphics/Enemies/" + FolderName + "/Frame-" + i + ".png", typeof(Sprite));
         }
     }
 }


by the way; the Frames[] size is also changed when the enemy is killed with a little function:

 //"Gif" is the Giffier script
 void Update(){
    NextStage();
 }
 public void NextStage()
 {
    if (CurrentEnemyHP <= 0f) 
    {
       CurrentEnemyID = Random.Range (1, 151);
       Gif.FolderName = EnemyDB.GetEnemy (CurrentEnemyID).Name;
       Gif.Frames = new Sprite[EnemyDB.GetEnemy (CurrentEnemyID).FramesQtty];
    }
 }




Comment

People who like this

0 Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Aug 24, 2019 at 10:41 PM 1
Share

Please, edit your question and provide the code causing the freeze. If the images are referenced in the array, they are loaded when the game starts.


You may also want to take a look at atlas (a.k.a spritesheets) https://docs.unity3d.com/Manual/class-SpriteAtlas.html

avatar image Arshww Hellium · Aug 25, 2019 at 09:53 PM 0
Share

Hey, thank you for helping me, i edited the topic and i think it's better now, any tip? i'll read about atlas too it will be useful

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

108 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Current scene execution stops before next scene is fully loaded! 1 Answer

how to preload assets with progress bar 1 Answer

Error BCW0015 and BCW0023 but they both work, how to solve it? 1 Answer

How to preload language? 0 Answers

How to Preload entire web game with a Loading progressbar? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges