- Home /
How can you set the vertexes of a Sprite's Physics Object programmatically when the texture is imported?
I want to set the Physics Object of a Sprite programmatically, when it is imported.
I am creating a tile-based platform game in 2D using Unity's TileMap feature, and some of the platform surfaces will be curved. Unity already generates an approximate physics object for a Sprite when it is imported, but I am finding that the physics object it generates doesn't follow the exact curvature of the surface depicted in the sprite, so I need to step in and provide my own shape for the physics. However, doing so manually for every such tile sprite I produce, which might number in the hundreds by the time the project is done, would be painfully repetitive and error-prone, and since I know the functions I used to generate the surface tiles' bitmaps, I can easily create a set of vertexes for the physics object using the same functions, if I know the correct place to set the Sprite's physics object data in code.
All of my research so far points to needing to create an editor script that inherits from AssetPostprocessor, and then providing the function:
void OnPostprocessSprites(Texture2D texture, Sprite[] sprites)
The Sprite class provides a function OverridePhysicsShape() that allows you to supply new physics shape data, which would appear to be exactly what I want. However, when I use that function to provide new physics shape data for the Sprite in OnPostprocessSprites(), it seems that the changes don't take. I have used the Sprite's GetPhysicsShape() function to pull the physics shape information back out in the same function call, so I can confirm that 1) my function is being called after the asset is imported, and 2) that my new physics data is being assigned to the Sprite. However, if I then re-open the Sprite using the Sprite Editor in the Inspector and examine the physics objects by switching to the "Edit Physics Shape" mode, the physics shape is still in its default configuration. If I edit the physics object in the Sprite Editor and apply the changes, then the changes persist, so it's clearly not a problem with editing physics objects on Sprites in general.
It seems like changes made in the OnPostprocessSprites() function of an AssetPostprocessor are ignored. I haven't found any kind of "Apply" or "Save Changes" function that I ought to be calling to make the changes persist-- at least, none that was mentioned in the documentation on that function. And I've seen nothing to indicate that there's any more appropriate time to try to set the Sprite's physics object programmatically than during this event. I've run out of ideas for how to get the behavior I'm after.
If I want to set the Physics Object of a Sprite programmatically when it is imported, what should I be doing?
Answer by snarlynarwhal · Jan 14, 2019 at 11:56 PM
Hi did you ever figure this out? I'm currently having the same issue. I just get a uninformative error:
Not allowed to override physics shape on sprite 'mysprite_0'
Answer by ExNinja · Oct 17, 2019 at 03:40 PM
@snarlynarwhal See this: https://issuetracker.unity3d.com/issues/sprite-dot-overridephysicsshape-raises-an-error-when-using-it-in-editor-mode
I'm currently working on this exact issue for the next edition of my book. I'll let you know what I find.