Atlas Creation From Custom Editor Script

This is the place to find out how to accomplish something specific with SM2.

Atlas Creation From Custom Editor Script

Postby gmayer » Thu May 31, 2012 3:49 pm

Hello. Apologies in advance if this has been addressed, though I've done some searching and did not find this case covered.

My situation is the following:

In our project we have hundreds of separate Materials, linked to Textures, that we use to procedurally build up images in the game made up of many layers, based on user preferences.
I want to convert this system to use PackedSprites, as the Textures are mostly empty space, but nevertheless are of large dimensions, so that they all stack up nicely.

I have an Editor script that does the following:

for each Material in a folder:
- load the Material (all have already been named strictly)
- fetch the Texture associated with the Material, and make a copy of it (I want to build up my process to be non-destructive to the old system)
- create a new empty Material
- create a new GameObject, and attach a PackedSprite script to it
- set the packedSprite.staticTexGUID to the AssetDatabase.AssetPathToGUID of the new Texture copy. (I tried just setting the staticTexPath string but it didn't seem to work)
- attach the empty Material to the MeshRenderer Component's .sharedMaterial

* build the Sprite Atlas for this single PackedSprite

- save out a prefab based on the new GameObject

This is almost working.
The step where I try to build the Sprite Atlas for a single GameObject and it's attached PackedSprite Component is not quite working.

Here is how I tried to do it:
I added a static function to the BuildAtlases class,

BuildSpriteAtlasForSpecificPackedSprite( PackedSprite ps, string _atlasFolder, int _maxAtlasSize, int _padding, bool _trim, bool _forceSquare )

I pass in the values normally set in user prefs (and am careful not to clobber the user prefs).

In my static function I instantiate an instance of BuildAtlases with new -> is this OK??
Then I call .OnWizardCreate directly, and based on a flag I set, I just manually add my single PackedSprite to the "sprite" ArrayList rather than let it do FindPackableSprites(sprites).
Then I basically let the rest go as usual - the intent was to leverage as much of the existing code as possible. I don't mind if it takes a while, as it's not a game-runtime process.

The result is mixed - usually one or two Atlases seem to build, before Unity crashes hard.

I don't know anything about the ScriptableWizard class - is what I'm doing not safe?
This felt like a bit of a hack, but again, I wanted to reuse as much existing code as possible.

Does anyone recommend a better/safer way to this?

Also, I realize that I will be getting many many small SpriteAtlases (each with 1 Texture in them) - this is OK, as we want to load as little as possible, based on user preferences - memory is more of a concern than performance in this case.

Thanks!
Greg
gmayer
 
Posts: 5
Joined: Tue May 01, 2012 11:33 pm

Re: Atlas Creation From Custom Editor Script

Postby Brady » Sun Jun 03, 2012 4:21 am

Generally you wouldn't want to build an atlas for a specific sprite since the idea behind an atlas is that it serves multiple sprites. However, if in your case the sprites in question each always have their own atlas, then you're just as safe building for a specific material rather than for a specific sprite. In such a case, you could leverage the existing code which builds atlases for a specific set of selected materials. Look at BuildMaterialAtlas() and see where the current selection is retrieved to get the list of selected materials. Just populate this list with your own code instead of using a selection.

Additionally, the crash issue you're having may be due to how Unity handles texture memory allocation. When dealing with LOTS of textures, Unity needs editor scripts to operate somewhat asynchronously so that it can free the previously used texture memory since it doesn't appear Unity has a way to do this otherwise. For an asynchronous version of the atlas builder, see the Add-ons forum for a script that contains that code. You might be able to modify that instead, or otherwise incorporate the asynchronicity thereof into your own code.
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Re: Atlas Creation From Custom Editor Script

Postby gmayer » Tue Jun 05, 2012 11:30 pm

Thanks for the reply Brady.

I did actually have a look at the build-for-selected Materials code before hand, but I don't think that would work because in my case the GameObject has a brand new material attached to it, but it's Material's Texture still points to nothing because it's never had an Atlas built yet.

Anyway, I've changed my workflow for now such that the Atlas are no longer created automatically, and it's working fine, except that I do find that if I try to create too many Sprite Atlases at once, Unity crashes - a scene with 80 GameObjects, each needing an Atlas generated, crashes Unity with a memory allocation failed dialog.

Are there some known limits or rules-of-thumb for how many Atlases one can try and create in one shot?

Thanks
Greg
gmayer
 
Posts: 5
Joined: Tue May 01, 2012 11:33 pm

Re: Atlas Creation From Custom Editor Script

Postby Brady » Fri Jun 08, 2012 8:45 am

Is that still the case even with the asynchronous atlas builder I mentioned that is in the Add-ons forum? The asynchronicity of that should help.
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Re: Atlas Creation From Custom Editor Script

Postby gmayer » Fri Jun 08, 2012 3:59 pm

Hi Brady
Unfortunately it is.
I tried the async builder and it was no better.

I had a problem with memory growing in one of my own pipeline scripts, and the key was to use:

EditorUtility.UnloadUnusedAssets();

instead of

Resources.UnloadUnusedAssets();

the former being a synchronous call that blocked until it was done.

I called this after I would DestroyImmediate some temp objects, and I was able to automate texture copy, reimport, and material and GameObject creation of hundreds of objects in one shot.

I did a quick test to change your calls in BuildAtlases to EditorUtility.UnloadUnusedAssets() but it didn't seem to help - I assume some key objects still held references, but I haven't had time to look into it further.

If you can update the AtlasBuilding to not grow memory as it does that would be awesome, because doing the atlas builds in small chunks is currently a slow manual step for us in an otherwise automated process.

cheers
Greg
gmayer
 
Posts: 5
Joined: Tue May 01, 2012 11:33 pm

Re: Atlas Creation From Custom Editor Script

Postby GabrielR » Wed Jun 13, 2012 9:44 am

I'm having the same crash, but I can't even build a single atlas using the usual build atlases script. Crashes on out of memory after allocating 3.4gigs of ram. I increased my source image file size to 1536 x 2048 so I can support iphone retina. I have a lot of empty space in those images because I use the TRIM function when building the atlas and need the texture offsets. Changing that is a massive job.

Brady, you thing the asynchronous script would help with my issue? Or any suggestions on how to modify Build Atlas to improve the situation?
GabrielR
 
Posts: 10
Joined: Mon Feb 14, 2011 9:59 pm

Re: Atlas Creation From Custom Editor Script

Postby GabrielR » Wed Jun 13, 2012 10:21 am

Ok, the asynchronous script seems to solve that issue. Just need to figure out how to tweak it so I can build a select atlas.
GabrielR
 
Posts: 10
Joined: Mon Feb 14, 2011 9:59 pm

Re: Atlas Creation From Custom Editor Script

Postby Brady » Tue Jun 19, 2012 5:28 am

@gmayer
That is strange... Everyone I've seen who has had that issue is able to get around the problem using the asynchronous atlas builder. Have you tried turning image trimming off just to see what happens? I wonder if perhaps it's not letting go of the trimmed copies of the textures?
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Re: Atlas Creation From Custom Editor Script

Postby gmayer » Tue Jun 19, 2012 2:27 pm

Hi Brady
I have not tried that yet - like GabrielR, the images are mostly empty space, made so large in part so that they all stack up nicely.
I can give that a shot and see if it helps.
Thx
greg
gmayer
 
Posts: 5
Joined: Tue May 01, 2012 11:33 pm

1

Postby czwen » Fri Dec 02, 2016 1:48 pm

ugg boots
uggs
ugg outlet
canada goose jackets
canada goose sale
canada goose outlet
canada goose coats
uggs canada
uggs
ugg canada
ugg canada sale
gucci outlet online
michael kors bags
christian louboutin shoes
christian louboutin sale
christian louboutin outlet
louboutin outlet
coach outlet online
coach factory outlet online
christian louboutin shoes
christian louboutin sale
coach outlet online
coach factory outlet online
burberry outlet online
burberry handbags
oakley vault outlet store
oakley vault outlet
cheap rolex watches
cheap rolex replica watches
gucci outlet online
gucci handbags outlet
kd shoes
kd 8 shoes
ugg boots outlet
uggs outlet
timberland boots outlet
birkenstock outlet
birkenstock sale
celine outlet online
celine handbags
michael kors outlet online
michael kors outlet store
mcm outlet online
true religion jeans outlet
true religion outlet store
polo ralph lauren outlet
polo ralph lauren
uggs on sale
uggs outlet
coach outlet online
coach factory outlet online
cheap jordan shoes
longchamp bags
cheap ugg boots
uggs outlet
ugg boots outlet
longchamp outlet store
cheap oakley sunglasses
oakley sunglasses cheap
oakley sunglasses outlet
discount oakley sunglasses
montblanc pens
canada goose jackets
canada goose sale
ysl outlet store
ysl handbags
ray ban sunglasses discount
ray ban sunglasses outlet
discount oakley sunglasses
oakley sunglasses wholesale
cheap uggs
canada goose jackets
ray bans
polo ralph lauren outlet
polo ralph lauren
cheap ray ban sunglasses
oakley sunglasses cheap
louis vuitton outlet online
jordan retro
michael kors bags
michael kors handbags clearance
louis vuitton bags
authentic louis vuitton handbags
michael kors outlet online
michael kors outlet store
nfl jerseys wholesale
cheap jerseys
louis vuitton outlet online
true religion jeans outlet
true religion outlet online
ugg
coach outlet online
coach outlet store
louis vuitton factory outlet
louis vuitton outlet online
moncler uk
moncler pas cher
ugg pas cher
canada goose jackets
canada goose jackets uk
zhi20161202
czwen
 
Posts: 64
Joined: Fri Dec 02, 2016 8:57 am

Next

Return to SM2 How to...

Who is online

Users browsing this forum: No registered users and 4 guests

cron