Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Wufer · Apr 01, 2018 at 10:26 PM · editor-scriptingspritesimporting assets

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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'

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

90 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Modify sprite geometry using AssetPostprocessor 0 Answers

Import a DLL with C# code in editor 2 Answers

Change compression type to every sprite 1 Answer

Blurry sprites have same import settings as crisp ones 0 Answers

does the PSD importer come with source code ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges