Does the game pause at the point where the an object or texture becomes visible onscreen or during any other event that might affect a texture? It’s a bit hard to narrow this down without some more information.
Well, as you can imagine, it’s hard to say what’s becoming visible in a level full of stuff. But let’s assume it is and it’s something on my side. For me to track it down or think it through, what does Texture.AwakeFromLoad do? Does it “awake” when it becomes visible, or is it like the MonoBehavior awake and gets called on load after construction? Is there any way to force it to become visible/awake if that’s what it is?
We load a scene and once characters became visible, iPhone starts getting laggy
Our scene was pretty simple, just textured plane as background and 6 animated human characters at right of the screen, but at start those characters was so far, so it was not visible. Once we start rolling camera to right and characters gets appear on screen iPhone start lagging there.
While reproducing this problem on Unity Mac with profiler we saw the huge spikes at the moment they appear on screen. Most time consuming method was Texture.AwakeFromLoad
Well, it was pretty challenge to understand what’s going on and how to solve it, because official doc for this don’t give any clue and different options like “Update when off-screen” is not help there since its for skin/animation.
So, I found the cheat/workaround for this… when scene was load in Awake/Start phase, we get rendering component for every model and then call any texture method. I guess, that during calling “almost any” texture methods while it’s not loaded to memory yet, cause to load it and install it VRAM. After this we noticed more smooth framerate for this scene.
Here is piece of code to see what we did:
//... loop for every loaded character game object as "contestant" variable
{
//....
skinRender = contestant.GetComponentInChildren<SkinnedMeshRenderer> ();
if (skinRender != null) {
skinRender.updateWhenOffscreen = false;
if (skinRender.sharedMaterial.mainTexture != null)
{
int tw = skinRender.sharedMaterial.mainTexture.width;
//Debug.Log("PA tex w="+skinRender.sharedMaterial.mainTexture.width);
}
}
//....
} // end of loop/iterator for each model
I don’t know if it’s bug or feature, but for Unity iOS, can be really good to have more fine controls for things like this,
since performance sometimes is critical there…
Vyacheslav is basically right - AwakeFromLoad for Texture can upload it to gpu making it stall. Though - in unity3.x we are making sure we are loading all assets during loading so tricks like that shouldn’t be needed. If you don’t create/change textures at runtime or load them through asset bundles you shouldn’t really see spikes like that
Hello Alexey.
This is for which platfform ? Because I see for Unity 3.1 for iPhone/iPad it’s not the case
I just faced this problem again with our iPad project on same scene, but this time characters has many materials/textures and this time, the stall is much more noticeable. The trick in my code make it smooth again.
We load characters as prefabs from resources (using Resources.Load) so may be this is the case.
Alexey,
Thanks for the note, and thanks hunterua for your note too.
We definitely have this problem still in the latest 3.1 version while running in the editor on Mac as well as the iOS devices. We’re needing to put a model with all the textures on it at the feet of the player on start to force everything to load.
Yeah, Resources.Load falls into same category (after loading we need to upload them to gpu - and this can happen at any time)
If you have Texture.AwakeFromLoad spikes for textures that were on scene - file a bug
Thanks.
Ok, I will if we found something. In my case all models are loaded from resources on each scene because the models/characters depends on player decisions/gameplay, so we don’t have predefined characters models on scene.
Alexey, may be there is more “elegant” solution for Unity iOS (other than I found) for preloading textures during Awake/Start when we instantiate models loaded from resources ?
Unfortunately resource loading / allocation patterns in the Editor might be very different comparing with the code running on device. I wouldn’t trust Editor Profiler on Texture.AwakeFromLoad.
However if you see and hiccups that you suspect because of loading resources - then:
first try to have empty Main scene (index 0) which is loaded first and then kicks loading of your actual scene
if that doesn’t help with hiccups - please submit a repro case project via BugReporter - we definitely need them to hunt down problems like that.
Problem with Texture.AwakeFromLoad is still noticable with our project. Currently building off of 3.3.0f4. Noticable in the Profiler on first time Instantiatons.