playMaker

Author Topic: Late binding sprite atlas sprites  (Read 4784 times)

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Late binding sprite atlas sprites
« on: December 23, 2019, 05:49:03 AM »
Hello guys,

i started working on a bit of an optimization for my game, so i split my atlas to multiple ones which will be loaded during runtime and not included in build.

I have them loaded using resource.load, but there's something else to be done. This is called Late Binding (https://docs.unity3d.com/Manual/LateBinding.html).

Ok, so i get property from the SpriteAtlas FsmVar (SpriteAtlas.spriteCount) to get the number of sprites.

When i get the number of sprites, i need to create an array of fixed length which will hold all the sprites and fill it with SpriteAtlas.GetSprites (https://docs.unity3d.com/2017.4/Documentation/ScriptReference/U2D.SpriteAtlas.GetSprites.html)

Ok, i made an array variable, as Array Type i set Object, and for Object type i set Sprite.

I made a script with a simple Sprite array and now i have to call the static method UnityEngine.U2D.SpriteAtlas.GetSprites to fill the array. But when i select the Type Array in Call Static Method Action i get:

Error editing field: Parameters
Unknown FsmArray Type: Use ArrayEditorAttribute to specify a VariableType.

Along with this:

Code: [Select]
System.ArrayTypeMismatchException: Unknown FsmArray Type: Use ArrayEditorAttribute to specify a VariableType.
  at HutongGames.PlayMakerEditor.ActionEditor.EditFsmArray (UnityEngine.GUIContent label, System.Object fieldValue, System.Object[] attributes) [0x00071] in C:\Projects\Playmaker_1.9.1\Projects\Playmaker.source.unity\Assets\PlayMaker\Editor\Classes\ActionEditor.cs:2719
  at HutongGames.PlayMakerEditor.ActionEditor.GUIForFieldTypes (System.String labelText, System.Type type, System.Object fieldValue, System.Object[] attributes) [0x004bb] in C:\Projects\Playmaker_1.9.1\Projects\Playmaker.source.unity\Assets\PlayMaker\Editor\Classes\ActionEditor.cs:1073
  at HutongGames.PlayMakerEditor.ActionEditor.EditFsmVar (UnityEngine.GUIContent label, System.Object fieldValue, System.Object[] attributes) [0x001ed] in C:\Projects\Playmaker_1.9.1\Projects\Playmaker.source.unity\Assets\PlayMaker\Editor\Classes\ActionEditor.cs:1761
  at HutongGames.PlayMakerEditor.ActionEditor.GUIForFieldTypes (System.String labelText, System.Type type, System.Object fieldValue, System.Object[] attributes) [0x001a9] in C:\Projects\Playmaker_1.9.1\Projects\Playmaker.source.unity\Assets\PlayMaker\Editor\Classes\ActionEditor.cs:934
  at HutongGames.PlayMakerEditor.ActionEditor.EditArray (UnityEngine.GUIContent label, System.Type elementType, System.Array array, System.Object[] attributes) [0x001c8] in C:\Projects\Playmaker_1.9.1\Projects\Playmaker.source.unity\Assets\PlayMaker\Editor\Classes\ActionEditor.cs:1254
  at HutongGames.PlayMakerEditor.ActionEditor.EditArray (UnityEngine.GUIContent label, System.Type elementType, System.Object fieldValue, System.Object[] attributes) [0x00014] in C:\Projects\Playmaker_1.9.1\Projects\Playmaker.source.unity\Assets\PlayMaker\Editor\Classes\ActionEditor.cs:1159
  at HutongGames.PlayMakerEditor.ActionEditor.GUIForFieldTypes (System.String labelText, System.Type type, System.Object fieldValue, System.Object[] attributes) [0x0049d] in C:\Projects\Playmaker_1.9.1\Projects\Playmaker.source.unity\Assets\PlayMaker\Editor\Classes\ActionEditor.cs:1069
  at HutongGames.PlayMakerEditor.ActionEditor.EditField (System.Object obj, System.Reflection.FieldInfo field, System.String labelText, System.Type fieldType, System.Object fieldValue, System.Object[] attributes) [0x0000c] in C:\Projects\Playmaker_1.9.1\Projects\Playmaker.source.unity\Assets\PlayMaker\Editor\Classes\ActionEditor.cs:582

Any ideas on how should i proceed on this one?

Pictures might be helfpul too.





This is the static method: public int GetSprites(Sprite[] sprites);
« Last Edit: December 23, 2019, 06:51:53 AM by krmko »
Available for Playmaker work

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: Late binding sprite atlas sprites
« Reply #1 on: December 23, 2019, 06:23:15 AM »
A small addition, i omitted the array script and made an array in the variables and the resized it with Array Resize action to fit the number of sprites from the atlas, but the next step where i have to provide the parameters throws the error i posted.
Available for Playmaker work

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Late binding sprite atlas sprites
« Reply #2 on: December 24, 2019, 01:39:11 AM »
Hi,

 All your sprites used in the scene you are going to published or prefabs you have in Resources, however you are going to atlas them, will be included in build. I think you mean that they don't get all loaded when the game starts, right?

I think you may overthink this.

if you have Resources, it's already great, now organize your atlas per Resources or per group ( per level or something reflecting the progression of your gameplay).

So I am not entitly sure about what you are trying to achieve here. if you store all your atlas in an array, they will be references, and Unity will have to load all of them which goes against the premise right?

Unless I am missing something?

Bye,

 Jean


Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: Late binding sprite atlas sprites
« Reply #3 on: December 24, 2019, 05:10:05 AM »
I included only one atlas in the build, the one necessary for the main menu and the first level. The other atlases (atlas for level 2, level 3 and so on) are NOT included in the build and i use resources.load to load them when they are needed so i can cut down my initial loading time. I have 500mb of textures which i can split into multiple parts so i need to load only 100mb on game start instead of 500mb and load the rest as needed in cutscenes between levels.

The thing is, loading the sprite atlas resource is not enough. You may load the sprite atlas as a resource on runtime, but it will do nothing, sprites will have white squares instead of texture.

So, this is what the sprite atlas API tells:

If ‘Include in Build’ is disabled, Unity still packs the Sprite Atlas into a *.spriteatlas file in the Project’s Assets folder. However, Sprites which reference Textures in an disabled Sprite Atlas appear invisible as the reference Texture is not available or loaded. Unity does not include the disabled Sprite Atlas in the Project’s published build, and does not automatically load it at run time. To do so, a script is required to load the Sprite Atlas via Late Binding.

Late Binding is the name of a process in which you load or swap in the desired Sprite Atlas at run time via the Sprite Atlas API. This is necessary when the build does not automatically load a prepared Sprite Atlas at run time (for example, when no Sprite Atlases are included in the build.)

So, actually, the sprite atlas loaded using resources.load is just a reference list that needs to be populated using Late Binding:

To retrieve the contents of a Sprite Atlas at run time, follow these steps:

Create a custom component that takes SpriteAtlasas a public variable.
Assign a Sprite Atlas to the field.
Enter the Editor’s Play Mode.
Access the variable and call the property .GetSprites to retrieve the array of Sprites packed in the selected Atlas.


I don't know the inner workings of Sprite Atlas, but i guess the included in the build Atlas makes a fixed array that connects references from Sprite Atlas to actual sprites, thus that's why an array has to be made and populated.

Anyway, as i said, resource.load does jackshit without binding, you can try it out yourself. It's quite dumb, i expected that once loaded from resources, it will take care of itself.
« Last Edit: December 24, 2019, 05:26:59 AM by krmko »
Available for Playmaker work

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Late binding sprite atlas sprites
« Reply #4 on: December 24, 2019, 08:04:28 AM »
Hi,

 ok, but if you are referencing a prefab inside a Resources folder, then this prefab is loaded when the app starts, because you are referencing it somehow in the scene ( in the array)

 for a resource prefab to NOT be loaded when the game starts, it must never be referenced, and only loaded via the Resources Api using a string as the pointer to where this prefab is within the Resources.


You basically don't need to do anything here. You just make a prefab using a sprite from an atlas that is NOT used in any other components in any scene you will build.

but I personnaly don't know how you could check if the atlas is not loaded anyway somehow, unless you have some way to monitor memory usage on the build itself as it runs.

 your best bet is to use ResourcesLoadAll and store your sprites into an FsmArray, this way you ensure that your sprites are loaded when you need them, and only when you need them.

Bye,

 Jean

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: Late binding sprite atlas sprites
« Reply #5 on: December 26, 2019, 02:47:58 PM »
I don't really get what you're telling me haha. I'll write a script for late binding and see what happens, but can you tell me do you have any idea why i get the error when i select the array of sprites in call static method action?
Available for Playmaker work

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Late binding sprite atlas sprites
« Reply #6 on: December 27, 2019, 04:29:48 AM »
Hi,

 your links are broken.

Bye,

 Jean

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: Late binding sprite atlas sprites
« Reply #7 on: December 27, 2019, 05:33:22 AM »
Links to images?


Available for Playmaker work

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Late binding sprite atlas sprites
« Reply #8 on: December 27, 2019, 07:06:44 AM »
Hi,

 you'll need to create a custom action for this,

but you might want to just have them in your Resources folder:

https://hutonggames.com/playmakerforum/index.php?topic=1671.msg7320#msg7320

Bye,

 Jean

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: Late binding sprite atlas sprites
« Reply #9 on: December 27, 2019, 08:01:46 AM »
Ah, now i get it. Ok, thanks :)
Available for Playmaker work