Last active
March 14, 2024 11:15
-
-
Save baba-s/f63879bfba9d677158b192d89b1d1e91 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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