mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-20 07:20:08 +00:00
优化SerieLabel引起的性能问题
This commit is contained in:
@@ -289,7 +289,8 @@ namespace XCharts
|
||||
{
|
||||
var labelObject = ChartHelper.AddObject(s_SerieLabelObjectName, transform, chartAnchorMin,
|
||||
chartAnchorMax, chartPivot, new Vector2(chartWidth, chartHeight));
|
||||
ChartHelper.DestroyAllChildren(labelObject.transform);
|
||||
//ChartHelper.DestroyAllChildren(labelObject.transform);
|
||||
SerieLabelPool.ReleaseAll(labelObject.transform);
|
||||
int count = 0;
|
||||
for (int i = 0; i < m_Series.Count; i++)
|
||||
{
|
||||
@@ -297,8 +298,8 @@ namespace XCharts
|
||||
for (int j = 0; j < serie.data.Count; j++)
|
||||
{
|
||||
var serieData = serie.data[j];
|
||||
if (!serie.label.show && j > 100) continue;
|
||||
var textName = s_SerieLabelObjectName + "_" + i + "_" + j + "_" + serieData.name;
|
||||
if (!serie.label.show && j > 100) continue;
|
||||
var textName = ChartCached.GetSerieLabelName(s_SerieLabelObjectName, i, j);
|
||||
var color = Color.grey;
|
||||
if (serie.type == SerieType.Pie)
|
||||
{
|
||||
@@ -310,14 +311,10 @@ namespace XCharts
|
||||
color = serie.label.color != Color.clear ? serie.label.color :
|
||||
(Color)m_ThemeInfo.GetColor(i);
|
||||
}
|
||||
var backgroundColor = serie.label.backgroundColor;
|
||||
var labelObj = ChartHelper.AddSerieLabel(textName, labelObject.transform, m_ThemeInfo.font,
|
||||
color, backgroundColor, serie.label.fontSize, serie.label.fontStyle, serie.label.rotate,
|
||||
serie.label.backgroundWidth, serie.label.backgroundHeight);
|
||||
|
||||
var iconObj = ChartHelper.AddIcon("Icon", labelObj.transform, serieData.iconWidth, serieData.iconHeight);
|
||||
var labelObj = SerieLabelPool.Get(textName, labelObject.transform, serie.label, m_ThemeInfo.font, color, serieData);
|
||||
var iconObj = labelObj.transform.Find("Icon").gameObject;
|
||||
serieData.SetIconObj(iconObj);
|
||||
|
||||
|
||||
var isAutoSize = serie.label.backgroundWidth == 0 || serie.label.backgroundHeight == 0;
|
||||
serieData.InitLabel(labelObj, isAutoSize, serie.label.paddingLeftRight, serie.label.paddingTopBottom);
|
||||
serieData.SetLabelActive(false);
|
||||
@@ -328,6 +325,7 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void InitTooltip()
|
||||
{
|
||||
var tooltipObject = ChartHelper.AddObject("tooltip", transform, chartAnchorMin,
|
||||
|
||||
59
Runtime/Internal/SerieLabelPool.cs
Normal file
59
Runtime/Internal/SerieLabelPool.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
/******************************************/
|
||||
/* */
|
||||
/* Copyright (c) 2018 monitor1394 */
|
||||
/* https://github.com/monitor1394 */
|
||||
/* */
|
||||
/******************************************/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
internal static class SerieLabelPool
|
||||
{
|
||||
private static readonly Stack<GameObject> m_Stack = new Stack<GameObject>(200);
|
||||
|
||||
public static GameObject Get(string name, Transform parent, SerieLabel label, Font font, Color color, SerieData serieData)
|
||||
{
|
||||
GameObject element;
|
||||
if (m_Stack.Count == 0)
|
||||
{
|
||||
element = ChartHelper.AddSerieLabel(name, parent, font,
|
||||
color, label.backgroundColor, label.fontSize, label.fontStyle, label.rotate,
|
||||
label.backgroundWidth, label.backgroundHeight);
|
||||
ChartHelper.AddIcon("Icon", element.transform, serieData.iconWidth, serieData.iconHeight);
|
||||
}
|
||||
else
|
||||
{
|
||||
element = m_Stack.Pop();
|
||||
element.name = name;
|
||||
element.transform.SetParent(parent);
|
||||
ChartHelper.SetActive(element, true);
|
||||
}
|
||||
return element;
|
||||
}
|
||||
|
||||
public static void Release(GameObject element)
|
||||
{
|
||||
ChartHelper.SetActive(element, false);
|
||||
//if (m_Stack.Count > 0 && ReferenceEquals(m_Stack.Peek(), element))
|
||||
// Debug.LogError("Internal error. Trying to destroy object that is already released to pool." + element.name);
|
||||
m_Stack.Push(element);
|
||||
}
|
||||
|
||||
public static void ReleaseAll(Transform parent)
|
||||
{
|
||||
int count = parent.childCount;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Release(parent.GetChild(i).gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
public static void ClearAll()
|
||||
{
|
||||
m_Stack.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Internal/SerieLabelPool.cs.meta
Normal file
11
Runtime/Internal/SerieLabelPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 340f267fa46e74d0bbbb0b75a20bd708
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user