Search Unity

Question How to increase Spriteshape perfomance when editing on runtime?

Discussion in '2D' started by ThomasSoto, Dec 31, 2020.

  1. ThomasSoto

    ThomasSoto

    Joined:
    Aug 7, 2013
    Posts:
    26
    Hi, I'm making a 2D endless runner for mobile where around every 5 seconds the spriteshape is edited to remove the points the player no longer sees and adds new points for the upcoming terrain. The problem I'm having is that whenever the new points are generated frames drop drastically.

    I've been researching and found that there is a way to use Burst to increase performance but I can't find any resources that explain how to do so. Alternatively, I also thought there might be a way to disable the spriteshape from updating unless I call the BakeMesh or RefreshSpriteShape methods to avoid it from doing any calculations until all the points required where added but I didn't find any way to make it work.

    Any suggestions on what direction I should take? Below is the simplified version of the script I made

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.U2D;
    5.  
    6. public class TerrainGenerator : MonoBehaviour {
    7.  
    8.     public SpriteShapeController controller;
    9.  
    10.     public float bleedThickness = 0;
    11.  
    12.     public void GenerateSection (LevelLoader.SectionSettings settings, List<Vector2> points) {
    13.  
    14.         RemoveEdges ();
    15.  
    16.         for (int i = 0; i < points.Count; i++) {
    17.             controller.spline.InsertPointAt (controller.spline.GetPointCount (), points[i] - Vector2.up * transform.position.y);
    18.         }
    19.         CreateEdges ();
    20.     }
    21.  
    22.     void RemoveEdges () {
    23.         // Remove Start
    24.         controller.spline.RemovePointAt (0);
    25.  
    26.         // Remove End
    27.         controller.spline.RemovePointAt (controller.spline.GetPointCount () - 1);
    28.     }
    29.  
    30.     void CreateEdges () {
    31.         // Create Start
    32.         float xPosStart = controller.spline.GetPosition (0).x;
    33.         controller.spline.InsertPointAt (0, new Vector2 (xPosStart, bleedThickness));
    34.         controller.spline.SetTangentMode (0, ShapeTangentMode.Linear);
    35.         controller.spline.SetTangentMode (1, ShapeTangentMode.Linear);
    36.  
    37.         // Create End
    38.         int endIndex = controller.spline.GetPointCount ();
    39.         float xPosEnd = controller.spline.GetPosition (endIndex - 1).x;
    40.         controller.spline.InsertPointAt (endIndex, new Vector2 (xPosEnd, bleedThickness));
    41.         controller.spline.SetTangentMode (endIndex, ShapeTangentMode.Linear);
    42.         controller.spline.SetTangentMode (endIndex - 1, ShapeTangentMode.Linear);
    43.     }
    44. }
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,513
    Here's the original thread for when Burst performance introduced: https://forum.unity.com/threads/spriteshape-3-0-6-is-out.771461/

    Basically you go to Window > Package Manager and install the Burst package.

    Beyond that, you could bake different shapes into pieces that connect together predictably so that they can be rearranged for the upcoming terrain.
     
  3. ThomasSoto

    ThomasSoto

    Joined:
    Aug 7, 2013
    Posts:
    26
    I installed the Burst package and performance was better but there is still a reasonable spike on mobile. I can't have prebaked shapes since they are randomnly generated at the moment they are going to be spawned.