mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-22 17:00:08 +00:00
重构代码
This commit is contained in:
39
Runtime/Internal/Pools/AxisPool.cs
Normal file
39
Runtime/Internal/Pools/AxisPool.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
/******************************************/
|
||||
/* */
|
||||
/* Copyright (c) 2018 monitor1394 */
|
||||
/* https://github.com/monitor1394 */
|
||||
/* */
|
||||
/******************************************/
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
internal static class XAxisPool
|
||||
{
|
||||
private static readonly ObjectPool<XAxis> s_ListPool = new ObjectPool<XAxis>(null, null);
|
||||
|
||||
public static XAxis Get()
|
||||
{
|
||||
return s_ListPool.Get();
|
||||
}
|
||||
|
||||
public static void Release(XAxis toRelease)
|
||||
{
|
||||
s_ListPool.Release(toRelease);
|
||||
}
|
||||
}
|
||||
|
||||
internal static class YAxisPool
|
||||
{
|
||||
private static readonly ObjectPool<YAxis> s_ListPool = new ObjectPool<YAxis>(null, null);
|
||||
|
||||
public static YAxis Get()
|
||||
{
|
||||
return s_ListPool.Get();
|
||||
}
|
||||
|
||||
public static void Release(YAxis toRelease)
|
||||
{
|
||||
s_ListPool.Release(toRelease);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Internal/Pools/AxisPool.cs.meta
Normal file
11
Runtime/Internal/Pools/AxisPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3a709ca44e9a445bd86bde1bbfae80de
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
43
Runtime/Internal/Pools/ListPool.cs
Normal file
43
Runtime/Internal/Pools/ListPool.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
/******************************************/
|
||||
/* */
|
||||
/* Copyright (c) 2018 monitor1394 */
|
||||
/* https://github.com/monitor1394 */
|
||||
/* */
|
||||
/******************************************/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
internal static class ListPool<T>
|
||||
{
|
||||
private static readonly ObjectPool<List<T>> s_ListPool = new ObjectPool<List<T>>(OnGet, OnClear);
|
||||
static void OnGet(List<T> l)
|
||||
{
|
||||
if (l.Capacity < 50)
|
||||
{
|
||||
l.Capacity = 50;
|
||||
}
|
||||
}
|
||||
static void OnClear(List<T> l)
|
||||
{
|
||||
l.Clear();
|
||||
}
|
||||
|
||||
public static List<T> Get()
|
||||
{
|
||||
return s_ListPool.Get();
|
||||
}
|
||||
|
||||
public static void Release(List<T> toRelease)
|
||||
{
|
||||
s_ListPool.Release(toRelease);
|
||||
}
|
||||
|
||||
public static void ClearAll()
|
||||
{
|
||||
s_ListPool.ClearAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Internal/Pools/ListPool.cs.meta
Normal file
11
Runtime/Internal/Pools/ListPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 02c30457469c746dc96f00f24cb6e1c6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
61
Runtime/Internal/Pools/ObjectPool.cs
Normal file
61
Runtime/Internal/Pools/ObjectPool.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
/******************************************/
|
||||
/* */
|
||||
/* Copyright (c) 2018 monitor1394 */
|
||||
/* https://github.com/monitor1394 */
|
||||
/* */
|
||||
/******************************************/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
internal class ObjectPool<T> where T : new()
|
||||
{
|
||||
private readonly Stack<T> m_Stack = new Stack<T>();
|
||||
private readonly UnityAction<T> m_ActionOnGet;
|
||||
private readonly UnityAction<T> m_ActionOnRelease;
|
||||
|
||||
public int countAll { get; private set; }
|
||||
public int countActive { get { return countAll - countInactive; } }
|
||||
public int countInactive { get { return m_Stack.Count; } }
|
||||
|
||||
public ObjectPool(UnityAction<T> actionOnGet, UnityAction<T> actionOnRelease)
|
||||
{
|
||||
m_ActionOnGet = actionOnGet;
|
||||
m_ActionOnRelease = actionOnRelease;
|
||||
}
|
||||
|
||||
public T Get()
|
||||
{
|
||||
T element;
|
||||
if (m_Stack.Count == 0)
|
||||
{
|
||||
element = new T();
|
||||
countAll++;
|
||||
}
|
||||
else
|
||||
{
|
||||
element = m_Stack.Pop();
|
||||
}
|
||||
if (m_ActionOnGet != null)
|
||||
m_ActionOnGet(element);
|
||||
return element;
|
||||
}
|
||||
|
||||
public void Release(T element)
|
||||
{
|
||||
if (m_Stack.Count > 0 && ReferenceEquals(m_Stack.Peek(), element))
|
||||
Debug.LogError("Internal error. Trying to destroy object that is already released to pool.");
|
||||
if (m_ActionOnRelease != null)
|
||||
m_ActionOnRelease(element);
|
||||
m_Stack.Push(element);
|
||||
}
|
||||
|
||||
public void ClearAll()
|
||||
{
|
||||
m_Stack.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Internal/Pools/ObjectPool.cs.meta
Normal file
11
Runtime/Internal/Pools/ObjectPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 09e67988253cb4f568b82d52b4113797
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
35
Runtime/Internal/Pools/SerieDataPool.cs
Normal file
35
Runtime/Internal/Pools/SerieDataPool.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
/******************************************/
|
||||
/* */
|
||||
/* Copyright (c) 2018 monitor1394 */
|
||||
/* https://github.com/monitor1394 */
|
||||
/* */
|
||||
/******************************************/
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
internal static class SerieDataPool
|
||||
{
|
||||
private static readonly ObjectPool<SerieData> s_ListPool = new ObjectPool<SerieData>(null, OnClear);
|
||||
|
||||
static void OnGet(SerieData serieData)
|
||||
{
|
||||
}
|
||||
|
||||
static void OnClear(SerieData serieData)
|
||||
{
|
||||
serieData.Clear();
|
||||
}
|
||||
|
||||
public static SerieData Get()
|
||||
{
|
||||
return s_ListPool.Get();
|
||||
}
|
||||
|
||||
public static void Release(SerieData toRelease)
|
||||
{
|
||||
s_ListPool.Release(toRelease);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Internal/Pools/SerieDataPool.cs.meta
Normal file
11
Runtime/Internal/Pools/SerieDataPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: faf4da15b01d74648bd13f73125e27bd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
85
Runtime/Internal/Pools/SerieLabelPool.cs
Normal file
85
Runtime/Internal/Pools/SerieLabelPool.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
/******************************************/
|
||||
/* */
|
||||
/* Copyright (c) 2018 monitor1394 */
|
||||
/* https://github.com/monitor1394 */
|
||||
/* */
|
||||
/******************************************/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
internal static class SerieLabelPool
|
||||
{
|
||||
private static readonly Stack<GameObject> m_Stack = new Stack<GameObject>(200);
|
||||
private static Dictionary<int, bool> m_ReleaseDic = new Dictionary<int, bool>(1000);
|
||||
|
||||
public static GameObject Get(string name, Transform parent, SerieLabel label, Font font, Color color,
|
||||
float iconWidth, float iconHeight)
|
||||
{
|
||||
GameObject element;
|
||||
if (m_Stack.Count == 0 || !Application.isPlaying)
|
||||
{
|
||||
element = CreateSerieLabel(name, parent, label, font, color, iconWidth, iconHeight);
|
||||
}
|
||||
else
|
||||
{
|
||||
element = m_Stack.Pop();
|
||||
if (element == null)
|
||||
{
|
||||
element = CreateSerieLabel(name, parent, label, font, color, iconWidth, iconHeight);
|
||||
}
|
||||
m_ReleaseDic.Remove(element.GetInstanceID());
|
||||
element.name = name;
|
||||
element.transform.SetParent(parent);
|
||||
element.transform.localEulerAngles = new Vector3(0, 0, label.rotate);
|
||||
var text = element.GetComponentInChildren<Text>();
|
||||
text.color = color;
|
||||
text.font = font;
|
||||
text.fontSize = label.fontSize;
|
||||
text.fontStyle = label.fontStyle;
|
||||
ChartHelper.SetActive(element, true);
|
||||
}
|
||||
return element;
|
||||
}
|
||||
|
||||
private static GameObject CreateSerieLabel(string name, Transform parent, SerieLabel label, Font font, Color color,
|
||||
float iconWidth, float iconHeight)
|
||||
{
|
||||
var element = ChartHelper.AddSerieLabel(name, parent, font,
|
||||
color, label.backgroundColor, label.fontSize, label.fontStyle, label.rotate,
|
||||
label.backgroundWidth, label.backgroundHeight, 1);
|
||||
ChartHelper.AddIcon("Icon", element.transform, iconWidth, iconHeight);
|
||||
return element;
|
||||
}
|
||||
|
||||
public static void Release(GameObject element)
|
||||
{
|
||||
if (element == null) return;
|
||||
ChartHelper.SetActive(element, false);
|
||||
if (!Application.isPlaying) return;
|
||||
if (!m_ReleaseDic.ContainsKey(element.GetInstanceID()))
|
||||
{
|
||||
m_Stack.Push(element);
|
||||
m_ReleaseDic.Add(element.GetInstanceID(), true);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
m_ReleaseDic.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Internal/Pools/SerieLabelPool.cs.meta
Normal file
11
Runtime/Internal/Pools/SerieLabelPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e960aeb14c09844e3bdcdc4138af0761
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user