Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by TruffelsAndOranges · Nov 19, 2014 at 12:43 PM · colorsanti-aliasingfill

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vptb · Nov 19, 2014 at 01:14 PM 0
Share

You want to see the collider filled with a color on the editor, or inside the game?

avatar image TruffelsAndOranges · Nov 19, 2014 at 02:30 PM 0
Share

I would like to see the filled polygon in both the editor and game! :)

3 Replies

· Add your reply
  • Sort: 
avatar image
20
Best Answer

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


Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nullgobz · Aug 30, 2015 at 04:38 PM 0
Share

Thank you! Is it important to set the mesh uv's?

avatar image slimsterstanley · Mar 07, 2016 at 10:45 AM 0
Share

$$anonymous$$an......You just saved my life...

avatar image VergilUa · Apr 07, 2016 at 03:35 PM 0
Share

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.

avatar image dNazarik · Oct 30, 2016 at 12:23 PM 0
Share

@ViicEsquivel, how to fill with a texture?

avatar image Robysoft · Dec 02, 2016 at 01:07 AM 0
Share

Is there anyway to apply this to a circle collider 2d?

avatar image
5

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();
     }
 }
 

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image XrenynTheGameMage · Aug 07, 2018 at 06:22 AM 0
Share

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!

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

FullScreenMovieScalingMode 0 Answers

Alternative Slider Method 1 Answer

Change all color materials from a model 1 Answer

Vertex colors as diffuse in lightmapping. 0 Answers

Why do my mesh colors blend sometimes and sometimes not? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges