- Home /
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!
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.
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.
@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.
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.
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.
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());
}
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.
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);"
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.
Answer by jjxtra · Apr 29, 2019 at 02:17 PM
Use "Advanced Polygon Collider" it does this for you and is free asset.
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.
I tried this on Unity 5.5.1. This didn't work for me in the built. It works fine inside the Scene window.
Your answer
![](https://koobas.hobune.stream/wayback/20220613150945im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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