Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
5
Question by ChaseKeeling · Jun 06, 2014 at 09:37 PM · polygon collider 2d

Refreshing the Polygon Collider (2D) upon sprite change?

Upon about an hour of research on the topic, I cannot find an answer. Currently, I am changing the sprite of a Sprite Renderer (Changing a weapon image in-hand). The Polygon Collider 2D does not update to the new sprite and shows the vertices of the initial sprite. Is there a way for me to refresh the the Polygon Collider to fit the newly attached sprite?

Warm Regards!

Comment
Add comment · Show 1
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 CodeFighter · Dec 13, 2015 at 04:11 PM 0
Share

Thank you! It helped me in Unity 5.3

7 Replies

· Add your reply
  • Sort: 
avatar image
14
Best Answer

Answer by ChaseKeeling · Jun 08, 2014 at 09:31 PM

FIXED: Temporary Solution

After changing the sprite through the GetComponent method,

 GetComponent<SpriteRenderer>().sprite = NewSprite;

I destroyed and then programmatically reinitialized the PolygonCollider.

 Destroy(GetComponent<PolygonCollider2D>());
 gameObject.AddComponent<PolygonCollider2D>();

Doing this completely resets the PolygonCollider's shape (hence deleting and adding a new instance). I understand this may be classified "bad coding practice" towards computing memory and resources, but in my defense it is not being used for animations; It is solely being used when a user changes their in-hand item (which isn't called very much).

Please let me know if anyone finds a better solution.

Comment
Add comment · Show 4 · 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 thexdd · Jul 11, 2015 at 11:15 PM 0
Share

it used to work in Unity 4.6, but it seems to be not working anymore in Unity 5 for me.. If anyone has got a solution, would appreciate it.

avatar image Chris Ramer thexdd · Dec 22, 2015 at 10:36 PM 0
Share

@thexdd I am using Unity 5.3.0f1 right now and it still doesn't work. Ever since I've been using Unity (since the early days of 4.x), the colliders have not update on the spot.

I used to do what @Chase$$anonymous$$eeling did, but I, too have deemed it as "bad coding practice." It's inefficient. I am hoping there is another, more practical way to do it.

avatar image Kabu4ce1 · Apr 29, 2019 at 11:24 AM 0
Share

I know this is an old thread, but just my 2 cents: You could cache the colliders by sprite/spritename into a dictionary, and create/enable/disable them when needed. Would get rid of the unecessary destructions and creations.

avatar image Derekloffin · Feb 28, 2020 at 04:57 AM 0
Share

The best I have found of doing this is manually recording the polygon collider info for the sprite in question, then setting it directly. This would mean recording all the points and all the path info to some data structure, probably with your sprite in there as well, then when you assign the sprite, you also assign the points and path. It's not a great method as it is very labor intensive, but it would work.

Depending on the number of objects in question, you could also build prefabs for everyone and just assign them as needed, although obviously with large numbers that could clumsy to manage.

$$anonymous$$y self I'm thinking of going the far easier route of using circle and box colliders as necessary and assigning based on that as they are far easier to do the equivalent thing.

avatar image
15

Answer by LCStark · Sep 07, 2018 at 04:38 PM

I didn't find any way to automatically refresh the PolygonCollider2D, but you can do it manually:

 polygonCollider = GetComponent<PolygonCollider2D>();
 sprite = GetComponent<SpriteRenderer>().sprite;

 for (int i = 0; i < polygonCollider.pathCount; i++) polygonCollider.SetPath(i, null);
 polygonCollider.pathCount = sprite.GetPhysicsShapeCount();
 
 List<Vector2> path = new List<Vector2>();
 for (int i = 0; i < polygonCollider.pathCount; i++) {
   path.Clear();
   sprite.GetPhysicsShape(i, path);
   polygonCollider.SetPath(i, path.ToArray());
 }

Comment
Add comment · Show 3 · 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 nnikolas · Dec 24, 2018 at 11:11 PM 0
Share

Correct answer for 2017.3.0 and up. For it to work, option "Generate physics shape" (on sprite import settings) should be set, or sprite should have "Custom physics shape" drawn in Sprite editor. Sprite texture doesn't need to be read/write enabled.

avatar image MihkelT · Jul 15, 2020 at 11:06 AM 1
Share

Seems to work great. Using unity 2019.11.1. I removed this part because it didn't compile and value can't set to null. " for (int i = 0; i < polygonCollider.pathCount; i++) polygonCollider.SetPath(i, null);"

avatar image paul-masri-stone · Feb 22, 2021 at 04:59 PM 0
Share

@LCStark As @MihkelT says, you can't set path to null (anymore). Instead replace line 3 with polygonCollider.pathCount = 0; This has the effect of removing any existing paths.

avatar image
4

Answer by NinjaISV · Jan 31, 2021 at 09:40 PM

In case anyone is looking for a decent solution in 2021:

 using System.Collections.Generic;
 using UnityEngine;
 
 public static class Collider2DExtensions {
     public static void TryUpdateShapeToAttachedSprite (this PolygonCollider2D collider) {
         collider.UpdateShapeToSprite(collider.GetComponent<SpriteRenderer>().sprite);
     }
 
     public static void UpdateShapeToSprite (this PolygonCollider2D collider, Sprite sprite) { 
         // ensure both valid
         if (collider != null && sprite != null) {
             // update count
             collider.pathCount = sprite.GetPhysicsShapeCount();
                 
             // new paths variable
             List<Vector2> path = new List<Vector2>();
 
             // loop path count
             for (int i = 0; i < collider.pathCount; i++) {
                 // clear
                 path.Clear();
                 // get shape
                 sprite.GetPhysicsShape(i, path);
                 // set path
                 collider.SetPath(i, path.ToArray());
                 }
         }
     }
 }

Just create a new script called Collider2DExtensions and paste this inside. Now you can call collider.TryUpdateShapeToAttachedSprite(); on any of your polygon collider components to automatically try to update to the attached sprite renderer.

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
2

Answer by jjxtra · Apr 29, 2019 at 02:17 PM

Use "Advanced Polygon Collider" it does this for you and is free asset.

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
1

Answer by LatchGameDev · Nov 28, 2017 at 06:37 PM

So I found a solution that works for refreshing colliders that I've testing in 5.6.3f1 I know this thread is from 2014/2015 but it might help someone since this is the first result in google.

     IEnumerator RefreshCollider(Collider2D col)
     {
         // Remember to call this with StartCoroutine
         // This will make a collider that will trigger 
         // it's OnEnterState again.
 
         col.enabled = false;
 
         // Wait a frame so the collider can update 
         // it's status to false 
         yield return null;
 
         // Enable
         col.enabled = true;
 
         yield return null;
 
         // Force an update to the collider logic by nudging the 
         // the transform but will ultimately not move the object
         col.transform.localPosition += new Vector3(0.01f, 0, 0);
         col.transform.localPosition += new Vector3(-0.01f, 0, 0);
     }

As for the OP's post this may or may not work. But as another user suggested. Just make the weapons prefabs and spawn those in to replace the other weapon.

Comment
Add comment · Show 1 · 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 Santosh_Nair · Jul 06, 2018 at 09:05 AM 0
Share

I tried this on Unity 5.5.1. This didn't work for me in the built. It works fine inside the Scene window.

  • 1
  • 2
  • ›

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

34 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

Related Questions

PolygonCollider2D shape problem on iOS 0 Answers

Raycasting not working on all sides? 1 Answer

Efficient generation of 2d polygon collider 0 Answers

Why Polygon Collider 2D changes when build? 0 Answers

Polygon Collider gone crazy 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