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
1
Question by invicticide · Dec 24, 2013 at 12:48 AM · editormaterialstexturesloadingdependencies

Editor doesn't load (some) dependencies with the scene, but standalone does? O_o

I'm working on a sprite-based game. The player's avatar can perform various animations, each of which is represented on a sprite sheet. Each sprite sheet is on a separate texture and a separate material. The avatar has a quad, and I change the material assigned to that quad based on what animation should be playing at the moment.

(Yes, I know this is non-optimal for render performance, and I am aware of texture atlassing and existing sprite tools like 2D Toolkit. For various reasons, those options are not currently on the table for this specific project.)

Here's the problem:

  • Start Unity and load the project (it's important that it's a fresh start)

  • Run the project in the editor

  • Perform a given animation for the first time = significant hitch

  • Perform the same animation any time thereafter = no hitch for that specific animation

  • Once all animations have been performed once, there are no further hitches and performance is quite stable

Two curious things:

  • Running the game the second and subsequent times in the same Unity editor session produces no hitches at all, at any time

  • Standalone builds have no problems whatsoever, not even on first run

I had to add some custom profiling (using Profiler.BeginSample/EndSample calls) to narrow it down, and this code:

 Profiler.BeginSample("SetAnimation (Change Material)");
 renderer.material = (m_animation.material != null ? m_animation.material : m_defaultMaterial);
 renderer.material.SetColor("_Color", Color.white);
 Profiler.EndSample();

...results in the following in the profiler:

 Player.Update (817.38 ms, self 0.09 ms)
     SetAnimation (Change Material) (817.17 ms, self 0.12 ms)
         Loading.ReadObject (817.05 ms, self 766.80 ms)

It appears that in the editor only Unity is loading the material and texture for each animation just-in-time, rather than doing this at the time the scene is loaded (either in editor mode or in play mode).

My understanding is that Unity will load all dependent assets at scene load time (barring the use of LoadLevelAsync, which is not the case here), where "dependent assets" means anything the scene refers to, and anything those things refer to, and on down the chain. In this case, all these textures should exist in the dependency chain like so:

  • Scene

  • GameStates manager instance (in the scene)

  • ShellState prefab reference (instanced by GameStates)

  • PlayState prefab instance (instanced by ShellState)

  • Player prefab instance (instanced and owned by PlayState)

  • Avatar prefab instance (instanced and owned by Player)

  • Sprite prefab instance (instanced and owned by Avatar)

  • Animation class instances (properties of Sprite, serialized non-MonoBehaviour data class)

  • Material instance (property of each Animation)

  • Material's texture reference (set in the Material just like with any standard Material)

So am I just misinformed about how Unity loads assets? The behavior definitely appears to be different between editor and standalone. Is there something I should be doing to ensure these textures are loaded with the scene in the editor? (I mean, other than a gross hack where I loop through and assign each texture at startup to get them cached or whatever is really going on behind the scenes, here.)

Comment
Add comment
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

1 Reply

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

Answer by invicticide · Dec 30, 2013 at 11:05 PM

So I guess nobody has any clue, but for the sake of anyone who lands on this question in the future, here's a couple relevant forum threads I found:

  • Preloading/uploading textures to vram prior to first render

  • High fps and periodic lags

I worked around the issue by adding a bunch of quads to the scene, one for each of the animation sheet materials, and giving them a simple MonoBehaviour that destroys them on first Update so they're not sitting around adding render time during gameplay.

This rather gross hack appears to force all those materials into VRAM when I load the scene (it noticeable elongated the scene load time, which is to be expected since the materials are quite large). Consequently, my gameplay hitches appear to be resolved.

This behavior seems rather silly, but there you go. :|

Comment
Add comment · 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

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

18 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

Related Questions

Does loading a Resource load that resource's dependencies? 1 Answer

Texture swapped out works in editor but not in build 1 Answer

Unity Ads service does not load in editor 0 Answers

Changes made during play mode, remain in editor after the game stops 1 Answer

Using one material for multiple textures 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