- Home /
How to fill polygon collider with a solid color?
Is there any way to fill a polygon collider with a solid color, using a sprite renderer or similar? Both ingame and ineditor.
It would be nice if there is some efficient way of doing this. And even better if there was anti aliased filling.
You want to see the collider filled with a color on the editor, or inside the game?
I would like to see the filled polygon in both the editor and game! :)
Answer by ViicEsquivel · Jan 08, 2015 at 07:55 AM
I figured it out. This is what I did:
[RequireComponent(typeof(PolygonCollider2D))]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[ExecuteInEditMode]
public class ColliderToMesh : MonoBehaviour {
PolygonCollider2D pc2 ;
void Start () {
pc2 = gameObject.GetComponent<PolygonCollider2D>();
//Render thing
int pointCount = 0;
pointCount = pc2.GetTotalPointCount();
MeshFilter mf = GetComponent<MeshFilter>();
Mesh mesh = new Mesh();
Vector2[] points = pc2.points;
Vector3[] vertices = new Vector3[pointCount];
Vector2[] uv = new Vector2[pointCount];
for(int j=0; j<pointCount; j++){
Vector2 actual = points[j];
vertices[j] = new Vector3(actual.x, actual.y, 0);
uv[j] = actual;
}
Triangulator tr = new Triangulator(points);
int [] triangles = tr.Triangulate();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = uv;
mf.mesh = mesh;
//Render thing
}
}
The Triangulator class is in this link: http://wiki.unity3d.com/index.php?title=Triangulator
Or you can put it all in the update and see it changing as you edit the collider.
#if UNITY_EDITOR
void Update(){
if (Application.isPlaying)
return;
if(pc2 != null){
//AllTheRenderThingFromTheStart ();
}
}
#endif
Thank you, you saved me a hour or two to figure this out.
Since OP asked for solid color setting uv isn't important. But for those who'll want to put a material on top of mesh - make sure to set uv's to something like:
mesh.uv = points;
That worked for me.
Answer by pakfront · Dec 27, 2016 at 06:34 PM
The Answer above help tremendously. Here is my addition, which adds an optional outline with a LineRenderer.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(PolygonCollider2D))]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(LineRenderer))]
public class MeshFromPolygonCollider2D : MonoBehaviour
{
// Based onh ttp://answers.unity3d.com/questions/835675/how-to-fill-polygon-collider-with-a-solid-color.html
public enum Interior { None, Filled }
public enum Outline { None, Open, Closed }
public Interior interior = Interior.Filled;
public Outline outline = Outline.Closed;
public PolygonCollider2D polygonCollider2D;
public MeshFilter meshFilter;
public MeshRenderer meshRenderer;
public LineRenderer lineRenderer;
public bool isWorldSpaceUV;
public bool isOutlineClosed = true;
private void Start()
{
Init();
}
public void Init()
{
CreateMesh();
CreateLine();
}
void CreateMesh() {
if (polygonCollider2D == null) polygonCollider2D = gameObject.GetComponent<PolygonCollider2D>();
if (meshFilter == null) meshFilter = GetComponent<MeshFilter>();
if (meshRenderer == null) meshRenderer = GetComponent<MeshRenderer>();
if ((meshFilter == null || meshRenderer == null) && interior == Interior.None) return;
if (meshFilter == null)
{
Debug.LogError(this + " has null meshFilter");
return;
}
if (meshRenderer == null)
{
Debug.LogError(this + " has null meshRenderer");
return;
}
if (interior == Interior.None)
{
meshRenderer.enabled = false;
return;
} else
{
meshRenderer.enabled = true;
}
//Render thing
int pointCount = 0;
pointCount = polygonCollider2D.GetTotalPointCount();
Mesh mesh = new Mesh();
Vector2[] points = polygonCollider2D.points;
Vector3[] vertices = new Vector3[pointCount];
Vector2[] uv = new Vector2[pointCount];
for (int j = 0; j < pointCount; j++)
{
Vector2 actual = points[j];
vertices[j] = new Vector3(actual.x, actual.y, 0);
if (isWorldSpaceUV)
{
uv[j] = actual;
}
else
{
uv[j] = new Vector2(actual.x / polygonCollider2D.bounds.size.x, actual.y / polygonCollider2D.bounds.size.y);
}
}
Triangulator tr = new Triangulator(points);
int[] triangles = tr.Triangulate();
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
meshFilter.mesh = mesh;
}
void CreateLine()
{
if (polygonCollider2D == null) polygonCollider2D = gameObject.GetComponent<PolygonCollider2D>();
if (lineRenderer == null) lineRenderer = GetComponent<LineRenderer>();
if (lineRenderer == null && outline == Outline.None) return;
if (lineRenderer == null)
{
Debug.LogError(this + " has null lineRenderer");
return;
}
if (outline == Outline.None)
{
lineRenderer.enabled = false;
return;
} else
{
lineRenderer.enabled = true;
}
//Render thing
int pointCount = 0;
pointCount = polygonCollider2D.GetTotalPointCount();
if (pointCount < 1) return;
if (outline == Outline.Closed) pointCount++;
Vector2[] points = polygonCollider2D.points;
lineRenderer.numPositions = pointCount;
for (int j = 0; j < polygonCollider2D.GetTotalPointCount(); j++)
{
Vector2 actual = points[j];
lineRenderer.SetPosition(j, new Vector3(actual.x, actual.y, 0));
}
if (outline == Outline.Closed)
{
Vector2 actual = points[0];
lineRenderer.SetPosition(pointCount-1, new Vector3(actual.x, actual.y, 0));
}
}
}
and then if you want it to automatically redraw when using the edit collider button in the Editor
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(MeshFromPolygonCollider2D))]
public class MeshFromPolygonCollider2DEditor : Editor
{
public void OnSceneGUI()
{
// enable update when editing mesh collider in Editor
MeshFromPolygonCollider2D myTarget = (MeshFromPolygonCollider2D)target;
myTarget.Init();
}
}
Yep, the Unity community is truly the best one I've seen so far. Thank you extremely much, I couldn't make my game without you two awsome people, ViicEsquivel and pakfront. These scripts are so good they shoudl become a built-in Unity feature!
Answer by TruffelsAndOranges · Dec 14, 2014 at 11:35 AM
A friend of mine solved this by using a Polygon Collider 2D with a Mesh Renderer, Mesh Filter, a custom sprite shader and a script that creates a mesh for the Polygon Collider 2D. The solution is more complicated than what can be described here, but I'll try to make a blog entry about it later.
Your answer
Follow this Question
Related Questions
How can I fill the screen with cubes? 1 Answer
How do I implement a fill mechanism in a drawing game 0 Answers
Change color to multiple Materials in one gameObject? 3 Answers
Text - Negative Color from Background 0 Answers
i'm trying to code a premise, if 3 objects are the same color then it will destroy the wall 1 Answer