Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by ryrich · Mar 05, 2015 at 01:59 AM · mesh2d gameperformance optimization

FPS drops when adding polygons

I'll spare the details of my game - but essentially, I'm trying to make a JezzBall clone. I have a class that generates the "fill area" as a polygon whenever I finish building a wall.

If you are unfamiliar with JezzBall, basically all I'm doing is adding polygons to my game area - these can be any shape really; rectangles, irregular, regular, you name it.

Here's my issue: every time I add a polygon, my FPS drops around 5-10, each time. As you can see, eventually my game will be unplayable. Actually, the drawing of my walls will slow down over time, which is NOT good! It should be the same speed always.

Here is the function I use to add a polygon to my game area:

 public void GeneratePolygon(Polygon poly)
 {
     GameObject goPoly = new GameObject();
     goPoly.transform.localPosition = new Vector3(goPoly.transform.localPosition.x,
                                      goPoly.transform.localPosition.y, -1f);

     Triangulator tr = new Triangulator(poly.Points);
     int[] indices = tr.Triangulate();

     // Create the Vector3 vertices
     Vector3[] vertices = new Vector3[poly.Points.Length];
     for (int i = 0; i < vertices.Length; i++)
     {
         vertices[i] = new Vector3(poly.Points[i].x, poly.Points[i].y, 0);
     }

     // Create the mesh
     Mesh msh = new Mesh();
     msh.vertices = vertices;
     msh.triangles = indices;
     msh.RecalculateNormals();
     msh.RecalculateBounds();

     // Set up game object with mesh;
     goPoly.isStatic = true;
     goPoly.AddComponent(typeof(MeshRenderer));
     goPoly.renderer.sharedMaterial = Resources.Load("Gray") as Material;
     goPoly.renderer.sortingLayerName = "Foreground";
     goPoly.renderer.sortingOrder = 2;
     MeshFilter filter = goPoly.AddComponent(typeof(MeshFilter)) as MeshFilter;
     filter.mesh = msh;
 }

And here is my Polygon class just in case it is needed:

 public class Polygon
 {
     private Vector2[] _points;
 
     public Vector2[] Points
     {
         get
         {
             return _points;
         }
     }
 
     public Vector2 this [int i]
     {
         get { return _points [i]; }
     }
 
     public int Length
     {
         get { return _points.Length; }
     }
 
     public Polygon()
     {
     }
 
     public Polygon(Vector2[] points)
     {
         _points = points;
     }
 
     public Polygon(List<Vector2> points)
     {
         _points = points.ToArray();
     }
 
     public bool Contains(Vector2 point)
     {
         bool isInside = false;
         for (int i = 0, j = _points.Length - 1; i < _points.Length; j = i++)
         {
             if (((_points [i].y > point.y) != (_points [j].y > point.y)) &&
                 (point.x < (_points [j].x - _points [i].x) * (point.y - _points [i].y) / (_points [j].y - _points [i].y) + _points [i].x))
             {
                 isInside = !isInside;
             }
         }
         return isInside;
     }
 
     public float Area
     {
         get
         {
             float a = _points[0].y * (_points[_points.Length - 1].x - _points[1].x);
             for (int i = 1; i < _points.Length; i++)
             {
                 a += _points[i].y * (_points[i - 1].x - _points[(i + 1) % _points.Length].x);
             }
             return Mathf.Abs(a / 2);
         }
     }
 }

I will not list Triangulator here, as I just found it online (here specifically)

I noticed if I disable my polygon game objects in the inspector (or delete the material) it will increase the FPS back to normal...

The material is just a Gray color "Sprites/Default" shader.

Any ideas?

Comment
Add comment
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

1 Reply

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

Answer by ryrich · Mar 05, 2015 at 11:00 AM

I wouldn't say this is an exact solution, but upgrading to Unity 5 (with no additional code changes) seems to have fixed this issue! Not sure why.

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

2 People are following this question.

avatar image avatar image

Related Questions

Optimize mesh for runtime performance 0 Answers

Will my huge sprites for a 2d game create performance issues? 2 Answers

Performance Drop on Integrated GPU Computers - GPU (Other) appears to be the source of issue. 0 Answers

Unity5 Procedural meshing slower than in Unity4? 1 Answer

Procedural Mesh Editing Performance 1 Answer


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