I want to ‘Trim’ the sheets from code for each sub-image within the sheet, but how do I do this using C# script editor script?
I don’t see it from the docs.
I am just trying to batch Trim my sprites from code instead of painstakingly clicking each individual sprite in the spritesheet using the editor and hitting Trim command because theres no multi-select option for some awful reason.
You may be able to save yourself a lot of time by setting your texture’s MeshType to Tight instead of Full Rect. That will automatically trim it for most practical purposes. MySprite.rect will still be its original (large) size, but MySprite.textureRect will be as if it has been trimmed. You can confirm this by turning on wireframe mode and examining your sprite. This may be sufficient for your use case and save you the time of writing a spritesheet postprocessor that interacts with the TextureImporter API.
https://docs.unity3d.com/Manual/TextureTypes.html#Sprite
1 Like
Unfortunately, It doesn’t work with sizes under 32x32 within the same sheet. I need to figure out how to do what you say and make a postprocessing script to take the raw texture importer data from the sheet and automatically trim non-transparent pixels in chunks as is done within the sprite editor using the Trim button manually.
However, barring that, is there a way to REBIND the T (its shift-T, BECAUSE REASONS??? sheesh!), I want it rebound as lower case t or something. Makes my life easier when doing it 100 times per sheet, I can atleast use the project view, scroll down each sliced/manually trimmed image within the same sheet and just tab two keys until I figure out what part of the API is used to do it automatically.
I mean, its my only option for now.
edit: found the solution for shortcuts here here:
High-level overview of steps that a spritesheet processor must do:
Setup Trigger
Have some kind of trigger: below gives you a custom Menu item on the top bar.
[MenuItem("My Menu Items/Spritesheet Post Process")]
public static void DoPostprocessing()
{
// do following steps
}
Open Texture & Importer
Open the texture in the TextureImporter for asset. You can get selected object through Selection.activeObject, then ask the AssetDatabase for its path.
Texture 2D tex = AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath);```
**Replace SpriteMetaData**
Next you need to replace SpriteMetaData[ ] on textureImporter.spritesheet. On this struct, the rect and likely pivot must change while you preserve the other properties.
Pivot note: something important to know is that the pivot is within 0...1 rect-normalized space. Other places in the API, .pivot returns pixel-space, so there's the opportunity for confusion. If you want to preserve the exact pixel position of a manually-placed pivot, you may need to convert it to pixel coordinates then back to rect-normalized coordinates. However, if you're using center or an edge then it's probably OK b/c center stays (0.5, 0.5) no matter how the rect changes.
**Emulate Trim feature**
Because this is a custom script, you have the opportunity to improve upon how Trim works natively. For example, it may be nice to have an alphaCutoff level... at least that's a feature I've wanted before.
From the texture, tex.GetPixel() / tex.GetPixels() using the current rect of its pixel coordinates. Scan those pixels for ones which fall below the alphaCutoff. As you scan, note the minX, maxX, minY, maxY of valid, above-cutoff pixels. Use these mins/max to create a new, smaller rect.
Create a new SpriteMetaData with new and preserved properties and assign it to the importer.spritesheet.
**Save the new Texture Data**
```importer.SaveAndReimport()```
**Final Note**
Test on a copied test texture, so there's no destructive change while ensuring everything works correctly.
1 Like
Thanks for all of the info, guidance, and help. Should I make a script like this I will post it in this thread.
Right now I’ve been content just tapping lowercase* rebinding.
edit* spelling