Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by thomaswp · Jun 15, 2015 at 02:03 PM · audioaudioclipresourcesresources.load

Game lags when loading AudioClip from Resources

Hello,

I'm trying to load an AudioClip resource at runtime using Resources.Load<>(). It's an mp3, a few minutes in length. Loading and playing works fine, but when I call the Load function, the game will hit a lag spike of about 300-500ms. So far I have only tested this in the editor.

I have also tried using Resources.LoadAsync() in an attempt to amortize the lag, but Unity only takes 2 frames to load the resource anyway, and the lag is unchanged.

Preloading the audio is an option, but I'd rather avoid it. When I load them at runtime, I don't care if it take an extra 1/2 second before the audio can play, as long as there's not lag.

The code is quite simple, essentially:

 ResourceRequest request = Resources.LoadAsync<AudioClip>("audioclip");

If I leave it in, I get lag. If I comment it out, I don't. I'm not even trying to play it. Admittedly there's a lot of other stuff going on in my code, so there may be an interaction somewhere I'm not aware of, but my guess is there isn't.

Has anyone else run into this problem or found a solution? Any help would be much appreciated.

EDIT: I'm selecting @Sisso's answer because it's pretty comprehensive. For me the specific solution was to mark the assets as "Streaming" in the editor, instead of "Decompress on Load," which is the default option.

Comment
Add comment · Show 5
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 Nerevar · Jun 15, 2015 at 03:34 PM 0
Share

are you 100% the issue comes from loading (or playing?) the audio?

showing us the part of the script where you load and play your audio could be useful.

avatar image thomaswp · Jun 15, 2015 at 03:49 PM 0
Share

Yes, I'm certain it comes from loading (not playing) the audio. It's as simple as commenting/uncommenting the Resources.Load<>() line.

avatar image smallbit · Jun 15, 2015 at 04:34 PM 1
Share

When you have multiple audio/art prefabs etc and you cannot preload them on start than your best choice is to pause the game, or use another break so user will not notice the lag which is inevitable. In my last game when player finishes level I cover screen with fade panel with loading text and some tip to keep player busy, meanwhile I unload all assets from previous level and load new ones. After loading is done I proceed to gameplay. I know is not a perfect solution but don´t give a player to witness any lags. you can break your gameplay in a variety of ways, show some comic, elevator break etc.. depends on your game.

avatar image Nerevar · Jun 16, 2015 at 07:24 AM 0
Share

Also make sure you don't call Resources.Load() in an update().

avatar image thomaswp · Jun 16, 2015 at 12:45 PM 0
Share

@smallbit, that's definitely my fallback plan, thanks. @Nerevar, when else could I load them? If I'm loading at runtime, then Awake() and Start() have passed. Update() is where I check if the BG music should have changed and play new music if it has. I don't load the resource multiple times (repeatedly) in an Update, though, if that's what you mean.

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Sisso · Jun 16, 2015 at 01:17 PM

First you need to have sure that it is the problem (and not a symptom), for example, a lot of use of CPU/Disk/Memory but you only notice when try to use a little more. Could be a lot of stuff, for example, the unpacked audio use so much memory that cause operation system to swap :P

Try to switch the audio file, put some println to triple check if it is not loading multiples times and test into a new fresh project to have sure that is not everything else.

Debuging is a art and take a lot of times, some time is easy to go directly to the workaround :P

Now the Profiler is available in free version, it is a good start.

You can debug the problem mixing between audio bitrate quality (repack mp3), format (switch to ogg or even wav). In general case is a trade off between file size (disk usage), unpackage size (memory usage) and processor usage (unpack process). If you game use to much cpu and no disk, very small compaction is the solution, it use a lot of disk but few cpu to unpack.

Add the asset as reference in scene to pre-load is the easy start. If can't do it (like memory limitations in mobile), the @smallbit solution is a good one.

Comment
Add comment · Show 2 · Share
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 thomaswp · Jun 16, 2015 at 03:36 PM 0
Share

I'm marking this as an answers because it's pretty comprehensive. For me the specific solution was to mark the assets as "Strea$$anonymous$$g" in the editor, ins$$anonymous$$d of "Decompress on Load," which is the default option. I think the decompression was what was causing the lag.

avatar image Sisso · Jun 16, 2015 at 03:55 PM 0
Share

Lol, i completely forgot that. "Decompress on Load" will not do a big spike in resources but will use in a constant rate of disk and cpu with low use of memory. Very useful for main soundtrack in mobiles for example, where you have very few memory and IOS have mp3 unpacker in hardware.

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.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Resource.Load Issue 1 Answer

audio.clip.GetData() returns no data. 0 Answers

How to load a large text asset with minimum to no lag. 2 Answers

Keep playing audio on android even if screen dim or sleep 0 Answers

Unity 5 low audio volume 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