here is a runtime SpriteRenderer and a Polygon2DCollider, and I want change the sprite at the renderer, but the points of collider is the old sprite points, How to update it?
I have tried to disable and enable the component, but it was not updated.
So only one way to update the component, just destory the component and add component again.
Did anyone have some another way to update the collider?
The PolygonCollider2D only does that as a workflow improvement when you first add it. It wouldn’t make sense to recalculate this and loose all your potential edits when it is disabled/enabled.
I presume you have already generated/edited the “physic shape” on your Sprite(s)? This makes it faster when the PolygonCollider2D or Tile assets are generated from it.
If so then you can simply update the PolygonCollider2D yourself by assigning the Sprite physics shapes to the collider like so:
using System.Collections.Generic;
using UnityEngine;
public class ScratchPad : MonoBehaviour
{
private readonly List<Vector2> m_PhysicsShapePath = new();
private void Start()
{
// Fetch the components.
var sprite = GetComponent<SpriteRenderer>().sprite;
var polygonCollider = GetComponent<PolygonCollider2D>();
// We don't want the collider updating as we make changes.
polygonCollider.enabled = false;
// A sprite can have multiple physics shape paths (for holes etc).
var shapeCount = sprite.GetPhysicsShapeCount();
polygonCollider.pathCount = shapeCount;
// Fetch each path and set the polygon collider.
for (var i = 0; i < shapeCount; ++i)
{
sprite.GetPhysicsShape(i, m_PhysicsShapePath);
polygonCollider.SetPath(i, m_PhysicsShapePath);
}
// Now we've changed everything, enable it.
polygonCollider.enabled = true;
}
}
If you know there’s only a single path then it’s as simple as:
using System.Collections.Generic;
using UnityEngine;
public class ScratchPad : MonoBehaviour
{
private readonly List<Vector2> m_PhysicsShapePath = new();
private void Start()
{
var sprite = GetComponent<SpriteRenderer>().sprite;
var polygonCollider = GetComponent<PolygonCollider2D>();
// Copy the sprite physics shape the the collider.
sprite.GetPhysicsShape(0, m_PhysicsShapePath);
polygonCollider.SetPath(0, m_PhysicsShapePath);
}
}
Because the sprite was created by runtime, so I can not edit physic shape at runtime.
But thanks for your answer
I mean it just requires a list of Vector2 points. So all you need is to work out those points. How you determine that from your runtime generated sprites is the tricky part of course.
yes.Because the collider will generate the points when I add the component.But I can not regenerate, it’s really depressed
Stating that you’re creating sprites at runtime is an important piece of information you should’ve really mentioned when originally asking your question TBH. Please try to provide some detail on exactly what you’re doing here if you still need help, especially on how these Sprites are being created etc otherwise it becomes a guessing game.
This won’t work either if it’s not a sprite with read/write access which is why this isn’t done by default at runtime because every texture would need to be read/write and results in it being in memory which is a waste. The way you said this sounds like it’s what you’re doing though?
If you’re using Sprite.Create, did you look at the docs that show you how to do this?