Skip to content

Instantly share code, notes, and snippets.

@baba-s
Last active March 14, 2024 11:15
Show Gist options
  • Save baba-s/f63879bfba9d677158b192d89b1d1e91 to your computer and use it in GitHub Desktop.
Save baba-s/f63879bfba9d677158b192d89b1d1e91 to your computer and use it in GitHub Desktop.
using Unity.Collections;
using UnityEngine;
[ExecuteAlways]
public class Example : MonoBehaviour
{
[SerializeField] private LineRenderer m_lineRenderer;
[SerializeField] private PolygonCollider2D m_collider2D;
private void Update()
{
if ( m_lineRenderer == null ) return;
if ( m_collider2D == null ) return;
var points = m_collider2D.points;
var transformCache = transform;
var position = transformCache.position;
var lossyScale = transformCache.lossyScale;
var positionCount = points.Length;
var positions = new NativeArray<Vector3>( positionCount, Allocator.Temp );
try
{
for ( var i = 0; i < positionCount; i++ )
{
var point = points[ i ];
positions[ i ] = new
(
point.x * lossyScale.x + position.x,
point.y * lossyScale.y + position.y
);
}
m_lineRenderer.positionCount = positionCount;
m_lineRenderer.SetPositions( positions );
}
finally
{
positions.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment