Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamic CurvySpline and memory allocation
Hi Jake, 

I am using CurvySpline to plot a character travel path.
Ie: When player touch the screen, the character will move to that location along the spline.

I wanted to avoid any in game memory allocation whenever possible. 
Looking at the CurvySplineSegment.Add(CurvySplineSegment, bool), the first thing it does is new GameObject()

Do you have any suggestion what I can do to avoid that?

The distance the character can travel is limited.
So I can pre-allocate 10 or 20 control points at GameObject.Start().
When a spline is require, I just bring-in (or enable) the number of control points and set their position and up-vector.  
When the character has reach his destination, I'll just reparent (or disable) the control points.
The game is targeted on mobile devices.  



Have you profiled this with Unity 4.6 (or 5 Beta) to be sure this is still a problem? UT said they greatly improved performance on this. I'll run some tests with 5.0 by myself and probably add pooling if this still is an issue.

Otherwise, pre-generating them and enabling them on demand is an option, but to be honest I doubt that the current version can handle this properly because the spline sorts it's CPs by hierarchy order (I changed this for 2.0).

Also, without having it profiled I would guess that the recalculation the spline does when adding CPs eats more time than instantiating a GameObject. Though you can add multiple points in a row and then refresh the spline once.

Another idea that you might want to consider: What about pregenerate a spline with 20 CPs and set all "unused" CPs to the same position as the last "enabled" one? Or, probably even easier, ignore them completely and only use (render, travel etc..) the range you want (say from TF=0 to TF=0.5, if you have 10 of 20 CPs in use)

Let me know if that helps, otherwise I'll add pooling to the current 2.0 dev version and give you access to it.

Good suggestion.  But it will take me a while to get this confirmed.  Still need to learn how Unity profiler works, and I have figure out how to profile it on the device.  An on top of that, learning the differences between U4.6 and U5.

I'll get back to you once I have some result.
So much to do so little time. [img]images/smilies/confused.gif[/img]


(11-28-2014, 11:31 PM)'kimc' Wrote: So much to do so little time. [img]images/smilies/confused.gif[/img]

No kidding, this is so true!

Hi Jake, 

Did a quick test in Unity 5 beta13. 
I am doing a few things wrong in the test code.
1. Adding one Vector3 at a time to CurvySpline (which triggers 1xRefreshImmediately() per add)
2. I have not included the code to update the Up vector of the control points.
3. FollowSpline doesn't starts from the beginning when new controlpoints are added. (Don't know how to do that yet).

Anyway, with the test code, whenever you mouseUp:
1. CurvySpline.Clear()
2. Individually adding (or remove) a Vector3 (between 4~20 control points)
3. Then GameObject with FollowSpline will travel there.

Looking at U5 profiler (first time), the spike in the CPU usage is when I mouseUp (probably when I'm calling multiple RefreshImmediate() per frame).
I'm not worry about the CPU utilisation at this stage, but it give you a good indicator to memory allocation in the Memory usage graph below.
The total Total memory increases a bit whenever i do a mouseUp.
If I let the code run a bit more (not shown here), the Total Allocated (Memory) drops even without GC.  It's kind of weird. 
Anyway, I don't know enough to be able to draw any conclusion.  But I know memory usage does increase whenever I dynamically add CPs to the spline I can't say how much of this will cause an issue for my game (and especially on mobile platform), so I'm going to park this aside, until I progress other parts of the game.

I have send you a copy of the test code to (info@fluffy...), if you have time to look into it.

BTW, do you have any good suggestion how I can add a list of position and Up-vector to CurvySpline?
Using CurvyLineSegement via CurvySpline.Add(CurvySpline Segment, bool)?

[Image: qrFIAQO.png?1]


Thanks, I'll profile it and if this still is an issue in 5.0 I'll add a pooling solution.

If you add several points in a row, use CurvySpline.Add(CurvySplineSegment,false) like you suggested, then refresh the spline manually once (or add the last CP with refresh=true).

Possibly Related Threads…
Thread Author Replies Views Last Post
  Disable rebuild of meshes in dynamic mode Zilk1 1 8 10-04-2023, 09:50 AM
Last Post: _Aka_
  I have a question about curvySpline haifeng.huang 12 32 07-14-2023, 09:34 AM
Last Post: _Aka_
  "Deadloop in CurvySPline.Refresh" spam Valkymaera 3 11 05-30-2023, 10:56 AM
Last Post: _Aka_
  Is curvyspline a good solution for a 2D rail grinding game? bazz_boyy1 2 8 03-01-2022, 11:49 AM
Last Post: _Aka_

Forum Jump: