优化性能,降低GC

This commit is contained in:
monitor1394
2019-07-23 21:43:01 +08:00
parent d0b51d9297
commit cb6d0765ca
12 changed files with 276 additions and 161 deletions

View File

@@ -0,0 +1,31 @@
using System.Collections.Generic;
using UnityEngine;
namespace XCharts
{
public static class ChartCached
{
private static Dictionary<float, string> s_ValueToF1Str = new Dictionary<float, string>(1000);
private static Dictionary<float, string> s_ValueToF2Str = new Dictionary<float, string>(1000);
private static Dictionary<float, string> s_ValueToStr = new Dictionary<float, string>(1000);
public static string FloatToStr(float value, int f = 0)
{
Dictionary<float, string> valueDic;
if (f == 1) valueDic = s_ValueToF1Str;
else if (f == 2) valueDic = s_ValueToF2Str;
else valueDic = s_ValueToStr;
if (valueDic.ContainsKey(value))
{
return valueDic[value];
}
else
{
if (f == 1) valueDic[value] = value.ToString("f1");
else if (f == 2) valueDic[value] = value.ToString("f2");
else valueDic[value] = value.ToString();
return valueDic[value];
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ea8a302478efd4de2a300f503267d966
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -325,8 +325,7 @@ namespace XCharts
}
}
public static Vector3[] GetBezierList(Vector3 sp, Vector3 ep, float k = 2.0f)
public static void GetBezierList(ref List<Vector3> posList,Vector3 sp, Vector3 ep, float k = 2.0f)
{
Vector3 dir = (ep - sp).normalized;
float dist = Vector3.Distance(sp, ep);
@@ -335,10 +334,10 @@ namespace XCharts
cp1.y = sp.y;
cp2.y = ep.y;
int segment = (int)(dist / 0.3f);
return GetBezierList2(sp, ep, segment, cp1, cp2);
GetBezierList2(ref posList,sp, ep, segment, cp1, cp2);
}
public static Vector3[] GetBezierListVertical(Vector3 sp, Vector3 ep, float k = 2.0f)
public static void GetBezierListVertical(ref List<Vector3> posList,Vector3 sp, Vector3 ep, float k = 2.0f)
{
Vector3 dir = (ep - sp).normalized;
float dist = Vector3.Distance(sp, ep);
@@ -347,7 +346,7 @@ namespace XCharts
cp1.y = sp.y;
cp2.y = ep.y;
int segment = (int)(dist / 0.3f);
return GetBezierList2(sp, ep, segment, cp2, cp1);
GetBezierList2(ref posList,sp, ep, segment, cp2, cp1);
}
public static List<Vector3> GetBezierList(Vector3 sp, Vector3 ep, int segment, Vector3 cp)
@@ -361,16 +360,18 @@ namespace XCharts
return list;
}
public static Vector3[] GetBezierList2(Vector3 sp, Vector3 ep, int segment, Vector3 cp,
public static void GetBezierList2(ref List<Vector3> posList,Vector3 sp, Vector3 ep, int segment, Vector3 cp,
Vector3 cp2)
{
Vector3[] list = new Vector3[segment + 1];
posList.Clear();
if(posList.Capacity < segment + 1){
posList.Capacity = segment +1;
}
for (int i = 0; i < segment; i++)
{
list[i] = (GetBezier2(i / (float)segment, sp, cp, cp2, ep));
posList.Add((GetBezier2(i / (float)segment, sp, cp, cp2, ep))) ;
}
list[segment] = ep;
return list;
posList.Add(ep);
}
public static Vector3 GetBezier(float t, Vector3 sp, Vector3 cp, Vector3 ep)