在游戏过程中绘制多边形对撞机
paint polygon collider during gameplay
我正在开发一个 2D 游戏,其中一些对象具有三角形视觉,使用多边形对撞机完成
是否可以绘制此对撞机,使其在游戏过程中可见?
假设你的意思是 PolygonCollider2D
并且它总是以正确的顺序有 3 个点,我想你可以做类似
using System.Linq;
...
[RequireComponent(typeof(MeshRenderer), typeof(MeshFilter), typeof(PolygonCollider2D))]
public class MeshCreator : MonoBehaviour
{
void Awake ()
{
var points = GetComponent<PolygonCollider2D>().points;
var meshFilter = GetComponent<MeshFilter>();
var mesh = new Mesh();
// Just a shorthand for something like
//var list = new List<Vector3>();
//foreach(var p in points)
//{
// list.Add(p);
//}
//mesh.vertices = list.ToArray();
mesh.vertices = points.Select(p -> (Vector3) p).ToArray();
// Here create two triangles (each 3 vertices)
// just because I don't know if you have the points in the correct order
mesh.triangles = new int[]{0,1,2,0,2,1};
meshFilter.mesh = mesh;
}
}
并将此组件放在 PolygonCollider2D
旁边。然后它将用三角形替换此对象的网格。
你可以在MeshRenderer
中预先设置material。
要获得此重叠区域,最简单的解决方案是半透明 materials。否则你可能需要一个特殊的着色器。
我正在开发一个 2D 游戏,其中一些对象具有三角形视觉,使用多边形对撞机完成
是否可以绘制此对撞机,使其在游戏过程中可见?
假设你的意思是 PolygonCollider2D
并且它总是以正确的顺序有 3 个点,我想你可以做类似
using System.Linq;
...
[RequireComponent(typeof(MeshRenderer), typeof(MeshFilter), typeof(PolygonCollider2D))]
public class MeshCreator : MonoBehaviour
{
void Awake ()
{
var points = GetComponent<PolygonCollider2D>().points;
var meshFilter = GetComponent<MeshFilter>();
var mesh = new Mesh();
// Just a shorthand for something like
//var list = new List<Vector3>();
//foreach(var p in points)
//{
// list.Add(p);
//}
//mesh.vertices = list.ToArray();
mesh.vertices = points.Select(p -> (Vector3) p).ToArray();
// Here create two triangles (each 3 vertices)
// just because I don't know if you have the points in the correct order
mesh.triangles = new int[]{0,1,2,0,2,1};
meshFilter.mesh = mesh;
}
}
并将此组件放在 PolygonCollider2D
旁边。然后它将用三角形替换此对象的网格。
你可以在MeshRenderer
中预先设置material。
要获得此重叠区域,最简单的解决方案是半透明 materials。否则你可能需要一个特殊的着色器。