diff options
Diffstat (limited to 'Assets/UI_Extension/Scripts')
42 files changed, 1304 insertions, 0 deletions
diff --git a/Assets/UI_Extension/Scripts/Animation.meta b/Assets/UI_Extension/Scripts/Animation.meta new file mode 100644 index 0000000..d108950 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Animation.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: f33d78ae280cb724f99154d977d556dc +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Animation/Tween.meta b/Assets/UI_Extension/Scripts/Animation/Tween.meta new file mode 100644 index 0000000..85f2fd1 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Animation/Tween.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6c9d4f729e1ee6344be85d61910dcd7e +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Animation/UIFreeFall.cs b/Assets/UI_Extension/Scripts/Animation/UIFreeFall.cs new file mode 100644 index 0000000..b55f0ce --- /dev/null +++ b/Assets/UI_Extension/Scripts/Animation/UIFreeFall.cs @@ -0,0 +1,16 @@ +using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class UIFreeFall : MonoBehaviour {
+
+ // Use this for initialization
+ void Start () {
+
+ }
+
+ // Update is called once per frame
+ void Update () {
+
+ }
+}
diff --git a/Assets/UI_Extension/Scripts/Animation/UIFreeFall.cs.meta b/Assets/UI_Extension/Scripts/Animation/UIFreeFall.cs.meta new file mode 100644 index 0000000..3ac37b2 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Animation/UIFreeFall.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 62bb377fe7cf46249b16cdd50dd8961a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Controls.meta b/Assets/UI_Extension/Scripts/Controls.meta new file mode 100644 index 0000000..d76cc1a --- /dev/null +++ b/Assets/UI_Extension/Scripts/Controls.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 194c26f2019ca7a458c7d29b48fa0990 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Controls/ButtonExt.cs b/Assets/UI_Extension/Scripts/Controls/ButtonExt.cs new file mode 100644 index 0000000..036a55f --- /dev/null +++ b/Assets/UI_Extension/Scripts/Controls/ButtonExt.cs @@ -0,0 +1,16 @@ +using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class ButtonExt : MonoBehaviour {
+
+ // Use this for initialization
+ void Start () {
+
+ }
+
+ // Update is called once per frame
+ void Update () {
+
+ }
+}
diff --git a/Assets/UI_Extension/Scripts/Controls/ButtonExt.cs.meta b/Assets/UI_Extension/Scripts/Controls/ButtonExt.cs.meta new file mode 100644 index 0000000..42b526b --- /dev/null +++ b/Assets/UI_Extension/Scripts/Controls/ButtonExt.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ba3eab0aeb6419d49a20e4f317f2d9db +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Controls/LoopTabBar.cs b/Assets/UI_Extension/Scripts/Controls/LoopTabBar.cs new file mode 100644 index 0000000..75b71f7 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Controls/LoopTabBar.cs @@ -0,0 +1,16 @@ +using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class LoopTabBar : MonoBehaviour {
+
+ // Use this for initialization
+ void Start () {
+
+ }
+
+ // Update is called once per frame
+ void Update () {
+
+ }
+}
diff --git a/Assets/UI_Extension/Scripts/Controls/LoopTabBar.cs.meta b/Assets/UI_Extension/Scripts/Controls/LoopTabBar.cs.meta new file mode 100644 index 0000000..fb2a347 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Controls/LoopTabBar.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0d43ae712f12d32409496dbd5a5758df +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Controls/ProgressBar.cs b/Assets/UI_Extension/Scripts/Controls/ProgressBar.cs new file mode 100644 index 0000000..b4a2b09 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Controls/ProgressBar.cs @@ -0,0 +1,16 @@ +using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class ProgressBar : MonoBehaviour {
+
+ // Use this for initialization
+ void Start () {
+
+ }
+
+ // Update is called once per frame
+ void Update () {
+
+ }
+}
diff --git a/Assets/UI_Extension/Scripts/Controls/ProgressBar.cs.meta b/Assets/UI_Extension/Scripts/Controls/ProgressBar.cs.meta new file mode 100644 index 0000000..d8527b1 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Controls/ProgressBar.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 71e0f9cfbb7afd4469a569d3101497cc +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Controls/RadialSlider.cs b/Assets/UI_Extension/Scripts/Controls/RadialSlider.cs new file mode 100644 index 0000000..0668737 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Controls/RadialSlider.cs @@ -0,0 +1,16 @@ +using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class RadialSlider : MonoBehaviour {
+
+ // Use this for initialization
+ void Start () {
+
+ }
+
+ // Update is called once per frame
+ void Update () {
+
+ }
+}
diff --git a/Assets/UI_Extension/Scripts/Controls/RadialSlider.cs.meta b/Assets/UI_Extension/Scripts/Controls/RadialSlider.cs.meta new file mode 100644 index 0000000..928c694 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Controls/RadialSlider.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f20b62258fabcc44e89e2da07b4f12c1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/DefaultControls.cs b/Assets/UI_Extension/Scripts/DefaultControls.cs new file mode 100644 index 0000000..adb63a9 --- /dev/null +++ b/Assets/UI_Extension/Scripts/DefaultControls.cs @@ -0,0 +1,11 @@ +using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+namespace UIExt
+{
+ public class DefaultControls
+ {
+
+ }
+}
diff --git a/Assets/UI_Extension/Scripts/DefaultControls.cs.meta b/Assets/UI_Extension/Scripts/DefaultControls.cs.meta new file mode 100644 index 0000000..b860bb9 --- /dev/null +++ b/Assets/UI_Extension/Scripts/DefaultControls.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b0b24d249a55b8f41b875096ffbd5318 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Effects.meta b/Assets/UI_Extension/Scripts/Effects.meta new file mode 100644 index 0000000..cb9ccb2 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Effects.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 4fdd7a495ccb6514e98215767e69a227 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Effects/BaseUIEffect.cs b/Assets/UI_Extension/Scripts/Effects/BaseUIEffect.cs new file mode 100644 index 0000000..3fefbd3 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Effects/BaseUIEffect.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; +using UnityEngine.EventSystems; +using UnityEditor; + +[ExecuteInEditMode] +[RequireComponent(typeof(Graphic))] +public abstract class BaseUIEffect + : UIBehaviour + , IMeshModifier + , IMaterialModifier +{
+ [NonSerialized]
+ private Graphic m_Graphic;
+
+ protected Graphic graphic
+ {
+ get
+ {
+ if (m_Graphic == null)
+ m_Graphic = GetComponent<Graphic>();
+
+ return m_Graphic;
+ }
+ }
+
+ protected override void OnEnable()
+ {
+ base.OnEnable();
+ if (graphic != null)
+ graphic.SetVerticesDirty();
+ }
+
+ protected override void OnDisable()
+ {
+ if (graphic != null)
+ graphic.SetVerticesDirty();
+ base.OnDisable();
+ }
+
+ protected override void OnDidApplyAnimationProperties()
+ {
+ if (graphic != null)
+ graphic.SetVerticesDirty();
+ base.OnDidApplyAnimationProperties();
+ }
+
+#if UNITY_EDITOR + protected override void OnValidate()
+ {
+ base.OnValidate();
+ if (graphic != null)
+ graphic.SetVerticesDirty();
+ }
+
+#endif +
+ public void ModifyMesh(Mesh mesh)
+ {
+ using (var vh = new VertexHelper(mesh))
+ {
+ ModifyMesh(vh);
+ vh.FillMesh(mesh);
+ }
+ }
+
+ public virtual void ModifyMesh(VertexHelper vh)
+ {
+ }
+
+ public virtual Material GetModifiedMaterial(Material baseMaterial)
+ {
+ return baseMaterial;
+ } + +} diff --git a/Assets/UI_Extension/Scripts/Effects/BaseUIEffect.cs.meta b/Assets/UI_Extension/Scripts/Effects/BaseUIEffect.cs.meta new file mode 100644 index 0000000..eb709fa --- /dev/null +++ b/Assets/UI_Extension/Scripts/Effects/BaseUIEffect.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 741aae2b712d7ce4e9c23c79391c28a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Effects/BlurEffect.cs b/Assets/UI_Extension/Scripts/Effects/BlurEffect.cs new file mode 100644 index 0000000..11c48eb --- /dev/null +++ b/Assets/UI_Extension/Scripts/Effects/BlurEffect.cs @@ -0,0 +1,16 @@ +using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class BlurEffect : MonoBehaviour {
+
+ // Use this for initialization
+ void Start () {
+
+ }
+
+ // Update is called once per frame
+ void Update () {
+
+ }
+}
diff --git a/Assets/UI_Extension/Scripts/Effects/BlurEffect.cs.meta b/Assets/UI_Extension/Scripts/Effects/BlurEffect.cs.meta new file mode 100644 index 0000000..58a879f --- /dev/null +++ b/Assets/UI_Extension/Scripts/Effects/BlurEffect.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 42214df8920bf054092e70ef6e58281d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Effects/ColorTintEffect.cs b/Assets/UI_Extension/Scripts/Effects/ColorTintEffect.cs new file mode 100644 index 0000000..e56ac2b --- /dev/null +++ b/Assets/UI_Extension/Scripts/Effects/ColorTintEffect.cs @@ -0,0 +1,16 @@ +using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class ColorTintEffect : MonoBehaviour {
+
+ // Use this for initialization
+ void Start () {
+
+ }
+
+ // Update is called once per frame
+ void Update () {
+
+ }
+}
diff --git a/Assets/UI_Extension/Scripts/Effects/ColorTintEffect.cs.meta b/Assets/UI_Extension/Scripts/Effects/ColorTintEffect.cs.meta new file mode 100644 index 0000000..4750b94 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Effects/ColorTintEffect.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9ddef7e92917bc44ca5dfb3a7a7fa026 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Effects/ExpandEffect.cs b/Assets/UI_Extension/Scripts/Effects/ExpandEffect.cs new file mode 100644 index 0000000..352645f --- /dev/null +++ b/Assets/UI_Extension/Scripts/Effects/ExpandEffect.cs @@ -0,0 +1,16 @@ +using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class ExpandEffect : MonoBehaviour {
+
+ // Use this for initialization
+ void Start () {
+
+ }
+
+ // Update is called once per frame
+ void Update () {
+
+ }
+}
diff --git a/Assets/UI_Extension/Scripts/Effects/ExpandEffect.cs.meta b/Assets/UI_Extension/Scripts/Effects/ExpandEffect.cs.meta new file mode 100644 index 0000000..c565207 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Effects/ExpandEffect.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 783b26d58e6c73d4397de9efb3a4774e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/LayoutGroup.meta b/Assets/UI_Extension/Scripts/LayoutGroup.meta new file mode 100644 index 0000000..fff0d42 --- /dev/null +++ b/Assets/UI_Extension/Scripts/LayoutGroup.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 25f2cafc34be4d9418a6a5923eb2395d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/LayoutGroup/GalleryLayoutGroup.cs b/Assets/UI_Extension/Scripts/LayoutGroup/GalleryLayoutGroup.cs new file mode 100644 index 0000000..1c2ad1b --- /dev/null +++ b/Assets/UI_Extension/Scripts/LayoutGroup/GalleryLayoutGroup.cs @@ -0,0 +1,16 @@ +using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class GalleryLayoutGroup : MonoBehaviour {
+
+ // Use this for initialization
+ void Start () {
+
+ }
+
+ // Update is called once per frame
+ void Update () {
+
+ }
+}
diff --git a/Assets/UI_Extension/Scripts/LayoutGroup/GalleryLayoutGroup.cs.meta b/Assets/UI_Extension/Scripts/LayoutGroup/GalleryLayoutGroup.cs.meta new file mode 100644 index 0000000..e18f836 --- /dev/null +++ b/Assets/UI_Extension/Scripts/LayoutGroup/GalleryLayoutGroup.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4691293df58d55b43bbe3bd962488c4d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/LayoutGroup/GridLayoutGroupExt.cs b/Assets/UI_Extension/Scripts/LayoutGroup/GridLayoutGroupExt.cs new file mode 100644 index 0000000..9d202b9 --- /dev/null +++ b/Assets/UI_Extension/Scripts/LayoutGroup/GridLayoutGroupExt.cs @@ -0,0 +1,153 @@ +using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.UI;
+using UnityEngine.EventSystems;
+using UnityEditor;
+
+namespace UIExt
+{
+ [AddComponentMenu("UI Extensions/LayoutGroup/GridLayoutGroupExt")]
+ [DisallowMultipleComponent]
+ [CanEditMultipleObjects]
+ public class GridLayoutGroupExt
+ : UIBehaviour
+ , ILayoutElement
+ , ILayoutGroup
+ {
+ public enum ScrollMode
+ {
+ Vertical = 1,
+ Horizontal = 2,
+ Auto = 3 // both horizontal and vertical
+ }
+
+ public enum MovementType + { + Unrestricted, // Unrestricted movement -- can scroll forever + Elastic, // Restricted but flexible -- can go past the edges, but springs back in place + Clamped, // Restricted movement where it's not possible to go past the edges + }
+
+ [System.NonSerialized] private RectTransform m_Rect; + protected RectTransform rectTransform + { + get + { + if (m_Rect == null) + m_Rect = GetComponent<RectTransform>(); + return m_Rect; + } + } + + [SerializeField] private ScrollRect m_Scrollrect;
+ public ScrollRect scrollRect { get { return m_Scrollrect; } set { m_Scrollrect = value; } }
+
+ [SerializeField] private RectTransform m_Content;
+ public RectTransform content { get { return m_Content; } set { m_Content = value; } }
+
+ [SerializeField] private LayoutItem m_ItemTemplate;
+ public LayoutItem itemTemplate { get { return m_ItemTemplate; } set { m_ItemTemplate = value; } }
+
+ [SerializeField] private int m_RowCount;
+ public int rowCount { get { return m_RowCount; } set { m_RowCount = value; } }
+ [SerializeField] private int m_ColumCount;
+ public int columCount { get { return m_ColumCount; } set { m_ColumCount = value; } }
+
+ [SerializeField] protected RectOffset m_Padding = new RectOffset(); + public RectOffset padding { get { return m_Padding; } set { SetProperty(ref m_Padding, value); } } +
+ [SerializeField] private Vector2Int m_CellSize;
+ public Vector2Int cellSize { get { return m_CellSize; } set { m_CellSize = value; } }
+
+ [SerializeField] private Vector2Int m_Spacing;
+ public Vector2Int spacing { get { return m_Spacing; } set { m_Spacing = value; } }
+
+ [SerializeField] private ScrollMode m_ScrollMode = ScrollMode.Vertical;
+
+ [SerializeField] private MovementType m_MovementType = MovementType.Elastic;
+
+ private List<object> m_DataList;
+ public List<object> dataList { get { return m_DataList; } }
+
+ public float minWidth
+ {
+ get
+ {
+ throw new System.NotImplementedException();
+ }
+ }
+
+ public float preferredWidth { get { return 0; } }
+
+ public float flexibleWidth { get { return 0; } }
+
+ public float minHeight { get { return 0; } }
+
+ public float preferredHeight { get { return 0; } }
+
+ public float flexibleHeight { get { return 0; } }
+
+ public int layoutPriority { get { return 0; } }
+
+ public void SetList(List<object> datalist)
+ {
+ m_DataList = datalist;
+ }
+
+ public void MoveTo(int indexOfData)
+ {
+
+ }
+
+ public void MoveTo(object data)
+ {
+
+ }
+
+ public void CalculateLayoutInputHorizontal()
+ {
+
+ }
+
+ public void CalculateLayoutInputVertical()
+ {
+
+ }
+
+ public void SetLayoutHorizontal()
+ {
+ }
+
+ public void SetLayoutVertical()
+ {
+ }
+
+ protected void SetProperty<T>(ref T currentValue, T newValue)
+ {
+ if ((currentValue == null && newValue == null) || (currentValue != null && currentValue.Equals(newValue)))
+ return;
+ currentValue = newValue;
+ SetDirty();
+ }
+
+ protected void SetDirty() + { + if (!IsActive()) + return; + + if (!CanvasUpdateRegistry.IsRebuildingLayout()) + LayoutRebuilder.MarkLayoutForRebuild(rectTransform); + else + StartCoroutine(DelayedSetDirty(rectTransform)); + }
+
+ IEnumerator DelayedSetDirty(RectTransform rectTransform) + { + yield return null; + LayoutRebuilder.MarkLayoutForRebuild(rectTransform); + } +
+ }
+
+}
diff --git a/Assets/UI_Extension/Scripts/LayoutGroup/GridLayoutGroupExt.cs.meta b/Assets/UI_Extension/Scripts/LayoutGroup/GridLayoutGroupExt.cs.meta new file mode 100644 index 0000000..14a6d7f --- /dev/null +++ b/Assets/UI_Extension/Scripts/LayoutGroup/GridLayoutGroupExt.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f8873f5959132ce46a5ee4027a88b867 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/LayoutGroup/HorizontalLayoutGroupExt.cs b/Assets/UI_Extension/Scripts/LayoutGroup/HorizontalLayoutGroupExt.cs new file mode 100644 index 0000000..2eb12d4 --- /dev/null +++ b/Assets/UI_Extension/Scripts/LayoutGroup/HorizontalLayoutGroupExt.cs @@ -0,0 +1,18 @@ +using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class HorizontalLayoutGroupExt
+ : MonoBehaviour
+{
+
+ // Use this for initialization
+ void Start () {
+
+ }
+
+ // Update is called once per frame
+ void Update () {
+
+ }
+}
diff --git a/Assets/UI_Extension/Scripts/LayoutGroup/HorizontalLayoutGroupExt.cs.meta b/Assets/UI_Extension/Scripts/LayoutGroup/HorizontalLayoutGroupExt.cs.meta new file mode 100644 index 0000000..d6872c4 --- /dev/null +++ b/Assets/UI_Extension/Scripts/LayoutGroup/HorizontalLayoutGroupExt.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 894dad6ac2c9eb3498d38fb489456748 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/LayoutGroup/LayoutItem.cs b/Assets/UI_Extension/Scripts/LayoutGroup/LayoutItem.cs new file mode 100644 index 0000000..da9cbf6 --- /dev/null +++ b/Assets/UI_Extension/Scripts/LayoutGroup/LayoutItem.cs @@ -0,0 +1,10 @@ +using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.EventSystems;
+using UnityEngine.UI;
+
+public class LayoutItem : UIBehaviour
+{
+
+}
diff --git a/Assets/UI_Extension/Scripts/LayoutGroup/LayoutItem.cs.meta b/Assets/UI_Extension/Scripts/LayoutGroup/LayoutItem.cs.meta new file mode 100644 index 0000000..c84bb8d --- /dev/null +++ b/Assets/UI_Extension/Scripts/LayoutGroup/LayoutItem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6d999d80aa990cf458ba4b8679fb12de +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/LayoutGroup/PagingView.cs b/Assets/UI_Extension/Scripts/LayoutGroup/PagingView.cs new file mode 100644 index 0000000..b8e9b98 --- /dev/null +++ b/Assets/UI_Extension/Scripts/LayoutGroup/PagingView.cs @@ -0,0 +1,554 @@ +//https://github.com/kiepng/Unity-PagingView
+using System;
+using UnityEngine.Events;
+using UnityEngine.EventSystems;
+
+namespace UnityEngine.UI.Extensions
+{
+ [SelectionBase]
+ [ExecuteInEditMode]
+ [DisallowMultipleComponent]
+ [RequireComponent(typeof(RectTransform))]
+ [AddComponentMenu("UI/Extensions/PagingView")]
+ public class PagingView
+ : UIBehaviour
+ , IInitializePotentialDragHandler
+ , IBeginDragHandler, IEndDragHandler, IDragHandler
+ , IScrollHandler
+ , ICanvasElement
+ , ILayoutElement
+ , ILayoutGroup
+ {
+ [Serializable] public class PagingEvent : UnityEvent<int> {}
+
+ [SerializeField] private RectTransform m_Content;
+ public RectTransform Content
+ {
+ get { return m_Content; }
+ set { m_Content = value; }
+ }
+
+ [SerializeField] private RectTransform[] m_Contents;
+ public RectTransform[] Contents
+ {
+ get { return m_Contents; }
+ set { m_Contents = value; }
+ }
+
+ [SerializeField] private bool m_Horizontal = true;
+ public bool Horizontal
+ {
+ get { return m_Horizontal; }
+ set { m_Horizontal = value; }
+ }
+
+ [SerializeField] private bool m_Vertical = true;
+ public bool Vertical
+ {
+ get { return m_Vertical; }
+ set { m_Vertical = value; }
+ }
+
+ [SerializeField] private float m_Elasticity = 0.1f;
+ public float Elasticity
+ {
+ get { return m_Elasticity; }
+ set { m_Elasticity = value; }
+ }
+
+ [SerializeField] private bool m_Inertia = true;
+ public bool Inertia
+ {
+ get { return m_Inertia; }
+ set { m_Inertia = value; }
+ }
+
+ [SerializeField] private float m_DecelerationRate = 0.135f;
+ public float DecelerationRate
+ {
+ get { return m_DecelerationRate; }
+ set { m_DecelerationRate = value; }
+ }
+
+ [SerializeField] private float m_ScrollSensitivity = 1.0f;
+ public float ScrollSensitivity
+ {
+ get { return m_ScrollSensitivity; }
+ set { m_ScrollSensitivity = value; }
+ }
+
+ [SerializeField] private RectTransform m_Viewport;
+ public RectTransform viewport
+ {
+ get {return m_Viewport; }
+ set { m_Viewport = value; SetDirtyCaching(); }
+ }
+
+ [SerializeField] private PagingEvent m_OnValueChanged = new PagingEvent();
+ public PagingEvent OnValueChanged
+ {
+ get { return m_OnValueChanged; }
+ set { m_OnValueChanged = value; }
+ }
+
+ private Vector2 m_PointerStartLocalCursor = Vector2.zero;
+ private Vector2 m_ContentStartPosition = Vector2.zero;
+
+ private RectTransform m_ViewRect;
+ public RectTransform ViewRect
+ {
+ get
+ {
+ if (m_ViewRect == null)
+ m_ViewRect = m_Viewport;
+ if (m_ViewRect == null)
+ m_ViewRect = (RectTransform) transform;
+ return m_ViewRect;
+ }
+ }
+
+ [NonSerialized] private RectTransform m_Rect;
+ private RectTransform RectTransform
+ {
+ get
+ {
+ if (m_Rect == null)
+ m_Rect = GetComponent<RectTransform>();
+ return m_Rect;
+ }
+ }
+
+ private Vector2 m_Velocity;
+ public Vector2 Velocity
+ {
+ get { return m_Velocity; }
+ set { m_Velocity = value; }
+ }
+
+ private bool m_Dragging;
+
+ private Bounds m_ViewBounds;
+ private Bounds m_ContentBounds;
+ private Bounds m_CurrentContentBounds;
+ private Bounds m_PrevViewBounds;
+ private Bounds m_PrevContentBounds;
+ private Bounds m_PrevCurrentContentBounds;
+ private Vector2 m_PrevPosition = Vector2.zero;
+
+ private DrivenRectTransformTracker m_Tracker;
+
+ [NonSerialized] private bool m_HasRebuiltLayout;
+
+ private const float EPSILON = float.Epsilon;
+
+ protected PagingView()
+ {
+ flexibleWidth = -1;
+ }
+
+ public void Rebuild(CanvasUpdate executing)
+ {
+ if (executing == CanvasUpdate.PostLayout)
+ {
+ UpdateBounds();
+ UpdatePrevData();
+
+ m_HasRebuiltLayout = true;
+ }
+ }
+
+ protected override void OnRectTransformDimensionsChange()
+ {
+ SetDirty();
+ }
+
+ public virtual void LayoutComplete() {}
+
+ public virtual void GraphicUpdateComplete() {}
+
+ public virtual void CalculateLayoutInputHorizontal() {}
+
+ public virtual void CalculateLayoutInputVertical() {}
+
+ public virtual float minWidth { get { return -1; } }
+
+ public virtual float preferredWidth { get { return -1; } }
+
+ public virtual float flexibleWidth { get; private set; }
+
+ public virtual float minHeight { get { return -1; } }
+
+ public virtual float preferredHeight { get { return -1; } }
+
+ public virtual float flexibleHeight { get { return -1; } }
+
+ public virtual int layoutPriority { get { return -1; } }
+
+ public virtual void SetLayoutHorizontal() { m_Tracker.Clear(); }
+
+ public virtual void SetLayoutVertical()
+ {
+ m_ViewBounds = new Bounds(ViewRect.rect.center, ViewRect.rect.size);
+ m_ContentBounds = GetBounds(m_Content);
+ m_CurrentContentBounds = GetBounds(m_Contents[m_ContentIndex]);
+ }
+
+ public virtual void OnScroll(PointerEventData eventData)
+ {
+ if (!IsActive())
+ return;
+
+ EnsureLayoutHasRebuilt();
+ UpdateBounds();
+
+ Vector2 delta = eventData.scrollDelta;
+ delta.y *= -1;
+ if (Vertical && !Horizontal)
+ {
+ if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
+ delta.y = delta.x;
+ delta.x = 0;
+ }
+
+ if (Horizontal && !Vertical)
+ {
+ if (Mathf.Abs(delta.y) > Mathf.Abs(delta.x))
+ delta.x = delta.y;
+ delta.y = 0;
+ }
+
+ Vector2 position = m_Content.anchoredPosition;
+ position += delta * m_ScrollSensitivity;
+
+ SetContentAnchoredPosition(position);
+ UpdateBounds();
+ }
+
+ public void OnInitializePotentialDrag(PointerEventData eventData)
+ {
+ if (eventData.button != PointerEventData.InputButton.Left)
+ return;
+
+ m_Velocity = Vector2.zero;
+ }
+
+ public void OnBeginDrag(PointerEventData eventData)
+ {
+ if (eventData.button != PointerEventData.InputButton.Left)
+ return;
+
+ if (!IsActive())
+ return;
+
+ UpdateBounds();
+
+ m_PointerStartLocalCursor = Vector2.zero;
+ RectTransformUtility.ScreenPointToLocalPointInRectangle(ViewRect, eventData.position,
+ eventData.pressEventCamera, out m_PointerStartLocalCursor);
+ m_ContentStartPosition = m_Content.anchoredPosition;
+ m_Dragging = true;
+ }
+
+ public void OnEndDrag(PointerEventData eventData)
+ {
+ if (eventData.button != PointerEventData.InputButton.Left)
+ return;
+
+ m_Dragging = false;
+
+ JudgementIndex();
+ }
+
+ public void OnDrag(PointerEventData eventData)
+ {
+ if (eventData.button != PointerEventData.InputButton.Left)
+ return;
+
+ if (!IsActive())
+ return;
+
+ Vector2 localCursor;
+ if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(ViewRect, eventData.position,
+ eventData.pressEventCamera, out localCursor))
+ return;
+
+ UpdateBounds();
+
+ var pointerDelta = localCursor - m_PointerStartLocalCursor;
+ Vector2 position = m_ContentStartPosition + pointerDelta;
+ Vector2 offset = CalculateOffset(m_ContentBounds, position - m_Content.anchoredPosition);
+ position += offset;
+ if (Math.Abs(offset.x) > EPSILON)
+ position.x = position.x - RubberDelta(offset.x, m_ViewBounds.size.x);
+ if (Math.Abs(offset.y) > EPSILON)
+ position.y = position.y - RubberDelta(offset.y, m_ViewBounds.size.y);
+
+ SetContentAnchoredPosition(position);
+ }
+
+ protected override void OnEnable()
+ {
+ base.OnEnable();
+
+ CanvasUpdateRegistry.RegisterCanvasElementForLayoutRebuild(this);
+ }
+
+ protected override void OnDisable()
+ {
+ CanvasUpdateRegistry.UnRegisterCanvasElementForRebuild(this);
+
+ m_Tracker.Clear();
+ m_Velocity = Vector2.zero;
+ LayoutRebuilder.MarkLayoutForRebuild(RectTransform);
+
+ base.OnDisable();
+ }
+
+ public override bool IsActive()
+ {
+ return base.IsActive() && m_Content != null;
+ }
+
+ private int m_PrevContentIndex;
+ private int m_ContentIndex;
+ private void LateUpdate()
+ {
+ if (!m_Content)
+ return;
+
+ EnsureLayoutHasRebuilt();
+ UpdateBounds();
+ float deltaTime = Time.unscaledDeltaTime;
+ Vector2 offset = CalculateOffset(m_CurrentContentBounds, Vector2.zero);
+ if (!m_Dragging && (offset != Vector2.zero || m_Velocity != Vector2.zero))
+ {
+ Vector2 position = m_Content.anchoredPosition;
+ for (int axis = 0; axis < 2; ++axis)
+ {
+ if (Math.Abs(offset[axis]) > EPSILON)
+ {
+ float speed = m_Velocity[axis];
+ position[axis] = Mathf.SmoothDamp(m_Content.anchoredPosition[axis], m_Content.anchoredPosition[axis] + offset[axis], ref speed, m_Elasticity, Mathf.Infinity, deltaTime);
+ m_Velocity[axis] = speed;
+ }
+ else if (m_Inertia)
+ {
+ m_Velocity[axis] *= Mathf.Pow(m_DecelerationRate, deltaTime);
+ if (Mathf.Abs(m_Velocity[axis]) < 1)
+ m_Velocity[axis] = 0;
+ position[axis] += m_Velocity[axis] * deltaTime;
+ }
+ else
+ {
+ m_Velocity[axis] = 0;
+ }
+ }
+
+ if (m_Velocity != Vector2.zero)
+ SetContentAnchoredPosition(position);
+ }
+
+ if (m_Dragging && m_Inertia)
+ {
+ Vector3 newVelocity = (m_Content.anchoredPosition - m_PrevPosition) / deltaTime;
+ m_Velocity = Vector3.Lerp(m_Velocity, newVelocity, deltaTime * 10);
+ }
+
+ if (m_Dragging && m_Velocity != Vector2.zero)
+ JudgementIndex(offset);
+
+ if (!m_Dragging && m_PrevContentIndex != m_ContentIndex)
+ {
+ m_OnValueChanged.Invoke(m_ContentIndex);
+ m_PrevContentIndex = m_ContentIndex;
+ }
+
+ if (m_ViewBounds != m_PrevViewBounds || m_ContentBounds != m_PrevContentBounds
+ || m_ContentBounds != m_PrevCurrentContentBounds || m_Content.anchoredPosition != m_PrevPosition)
+ UpdatePrevData();
+ }
+
+ public virtual void StopMovement()
+ {
+ m_Velocity = Vector2.zero;
+ }
+
+ protected virtual void SetContentAnchoredPosition(Vector2 position)
+ {
+ if (!m_Horizontal)
+ position.x = m_Content.anchoredPosition.x;
+ if (!m_Vertical)
+ position.y = m_Content.anchoredPosition.y;
+
+ if (position != m_Content.anchoredPosition)
+ {
+ m_Content.anchoredPosition = position;
+ UpdateBounds();
+ }
+ }
+
+ private void EnsureLayoutHasRebuilt()
+ {
+ if (m_HasRebuiltLayout && !CanvasUpdateRegistry.IsRebuildingLayout())
+ Canvas.ForceUpdateCanvases();
+ }
+
+ private void UpdatePrevData()
+ {
+ if (m_Content == null)
+ m_PrevPosition = Vector2.zero;
+ else
+ m_PrevPosition = m_Content.anchoredPosition;
+ m_PrevViewBounds = m_ViewBounds;
+ m_PrevContentBounds = m_ContentBounds;
+ m_PrevCurrentContentBounds = m_ContentBounds;
+ }
+
+ private static float RubberDelta(float overStretching, float viewSize)
+ {
+ return (1 - (1 / ((Mathf.Abs(overStretching) * 0.55f / viewSize) + 1)))
+ * viewSize * Mathf.Sign(overStretching);
+ }
+
+ private void UpdateBounds()
+ {
+ m_ViewBounds = new Bounds(ViewRect.rect.center, ViewRect.rect.size);
+ m_ContentBounds = GetBounds(m_Content);
+ m_CurrentContentBounds = GetBounds(m_Contents[m_ContentIndex]);
+
+ if (m_Content == null)
+ return;
+
+ Vector3 contentSize = m_ContentBounds.size;
+ Vector3 contentPos = m_ContentBounds.center;
+ Vector3 excess = m_ViewBounds.size - contentSize;
+ if (excess.x > 0)
+ {
+ contentPos.x -= excess.x * (m_Content.pivot.x - 0.5f);
+ contentSize.x = m_ViewBounds.size.x;
+ }
+ if (excess.y > 0)
+ {
+ contentPos.y -= excess.y * (m_Content.pivot.y - 0.5f);
+ contentSize.y = m_ViewBounds.size.y;
+ }
+
+ m_ContentBounds.size = contentSize;
+ m_ContentBounds.center = contentPos;
+ }
+
+ private readonly Vector3[] m_Corners = new Vector3[4];
+ private Bounds GetBounds(RectTransform content)
+ {
+ if (m_Content == null)
+ return new Bounds();
+
+ var vMin = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
+ var vMax = new Vector3(float.MinValue, float.MinValue, float.MinValue);
+
+ var toLocal = ViewRect.worldToLocalMatrix;
+ content.GetWorldCorners(m_Corners);
+ for (int j = 0; j < 4; j++)
+ {
+ Vector3 v = toLocal.MultiplyPoint3x4(m_Corners[j]);
+ vMin = Vector3.Min(v, vMin);
+ vMax = Vector3.Max(v, vMax);
+ }
+
+ var bounds = new Bounds(vMin, Vector3.zero);
+ bounds.Encapsulate(vMax);
+ return bounds;
+ }
+
+ private Vector2 CalculateOffset(Bounds bounds, Vector2 delta)
+ {
+ Vector2 offset = Vector2.zero;
+
+ Vector2 min = bounds.min;
+ Vector2 max = bounds.max;
+
+ if (m_Horizontal)
+ {
+ min.x += delta.x;
+ max.x += delta.x;
+ if (min.x > m_ViewBounds.min.x)
+ offset.x = m_ViewBounds.min.x - min.x;
+ else if (max.x < m_ViewBounds.max.x)
+ offset.x = m_ViewBounds.max.x - max.x;
+ }
+
+ if (m_Vertical)
+ {
+ min.y += delta.y;
+ max.y += delta.y;
+ if (max.y < m_ViewBounds.max.y)
+ offset.y = m_ViewBounds.max.y - max.y;
+ else if (min.y > m_ViewBounds.min.y)
+ offset.y = m_ViewBounds.min.y - min.y;
+ }
+
+ return offset;
+ }
+
+ private void JudgementIndex()
+ {
+ if (Horizontal)
+ {
+ if (-m_Velocity.x > m_ViewBounds.size.x)
+ m_ContentIndex = Mathf.Clamp(m_PrevContentIndex + 1, 0, m_Contents.Length - 1);
+ else if (m_Velocity.x > m_ViewBounds.size.x)
+ m_ContentIndex = Mathf.Clamp(m_PrevContentIndex - 1, 0, m_Contents.Length - 1);
+ }
+
+ if (Vertical)
+ {
+ if (m_Velocity.y > m_ViewBounds.size.y)
+ m_ContentIndex = Mathf.Clamp(m_PrevContentIndex + 1, 0, m_Contents.Length - 1);
+ else if (-m_Velocity.y > m_ViewBounds.size.y)
+ m_ContentIndex = Mathf.Clamp(m_PrevContentIndex - 1, 0, m_Contents.Length - 1);
+ }
+ }
+
+ private void JudgementIndex(Vector2 offset)
+ {
+ if (Horizontal)
+ {
+ if (offset.x > m_ViewBounds.extents.x)
+ m_ContentIndex = Mathf.Clamp(m_ContentIndex + 1, 0, m_Contents.Length - 1);
+ else if (-offset.x > m_ViewBounds.extents.x)
+ m_ContentIndex = Mathf.Clamp(m_ContentIndex - 1, 0, m_Contents.Length - 1);
+ }
+
+ if (Vertical)
+ {
+ if (-offset.y > m_ViewBounds.extents.y)
+ m_ContentIndex = Mathf.Clamp(m_ContentIndex + 1, 0, m_Contents.Length - 1);
+ else if (offset.y > m_ViewBounds.extents.y)
+ m_ContentIndex = Mathf.Clamp(m_ContentIndex - 1, 0, m_Contents.Length - 1);
+ }
+ }
+
+ protected void SetDirty()
+ {
+ if (!IsActive())
+ return;
+ LayoutRebuilder.MarkLayoutForRebuild(RectTransform);
+ }
+
+ protected void SetDirtyCaching()
+ {
+ if (!IsActive())
+ return;
+
+ CanvasUpdateRegistry.RegisterCanvasElementForLayoutRebuild(this);
+ LayoutRebuilder.MarkLayoutForRebuild(RectTransform);
+ }
+
+#if UNITY_EDITOR
+ protected override void OnValidate()
+ {
+ SetDirtyCaching();
+ }
+#endif
+ }
+}
diff --git a/Assets/UI_Extension/Scripts/LayoutGroup/PagingView.cs.meta b/Assets/UI_Extension/Scripts/LayoutGroup/PagingView.cs.meta new file mode 100644 index 0000000..3935af3 --- /dev/null +++ b/Assets/UI_Extension/Scripts/LayoutGroup/PagingView.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5d3fb2d406ecb624986076b829c961ad +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/LayoutGroup/RoundLayoutGroup.cs b/Assets/UI_Extension/Scripts/LayoutGroup/RoundLayoutGroup.cs new file mode 100644 index 0000000..1608f4a --- /dev/null +++ b/Assets/UI_Extension/Scripts/LayoutGroup/RoundLayoutGroup.cs @@ -0,0 +1,16 @@ +using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class RoundLayoutGroup : MonoBehaviour {
+
+ // Use this for initialization
+ void Start () {
+
+ }
+
+ // Update is called once per frame
+ void Update () {
+
+ }
+}
diff --git a/Assets/UI_Extension/Scripts/LayoutGroup/RoundLayoutGroup.cs.meta b/Assets/UI_Extension/Scripts/LayoutGroup/RoundLayoutGroup.cs.meta new file mode 100644 index 0000000..f3802f6 --- /dev/null +++ b/Assets/UI_Extension/Scripts/LayoutGroup/RoundLayoutGroup.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 541b1949d4980f749ae3d0ca8b26189a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Utilities.meta b/Assets/UI_Extension/Scripts/Utilities.meta new file mode 100644 index 0000000..2ca0069 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Utilities.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e9106d463951b4d4cac6baed153876eb +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Utilities/ListPool.cs b/Assets/UI_Extension/Scripts/Utilities/ListPool.cs new file mode 100644 index 0000000..774d55c --- /dev/null +++ b/Assets/UI_Extension/Scripts/Utilities/ListPool.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using UnityEngine; + +namespace UIExt +{ + + internal static class ListPool<T> + { + // Object pool to avoid allocations. + private static readonly ObjectPool<List<T>> s_ListPool = new ObjectPool<List<T>>(null, l => l.Clear()); + + public static List<T> Get() + { + return s_ListPool.Get(); + } + + public static void Release(List<T> toRelease) + { + s_ListPool.Release(toRelease); + } + } +} diff --git a/Assets/UI_Extension/Scripts/Utilities/ListPool.cs.meta b/Assets/UI_Extension/Scripts/Utilities/ListPool.cs.meta new file mode 100644 index 0000000..9f8bf39 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Utilities/ListPool.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c0ddf6384711d3e4489db520510ac43a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Utilities/ObjectPool.cs b/Assets/UI_Extension/Scripts/Utilities/ObjectPool.cs new file mode 100644 index 0000000..f1db3ec --- /dev/null +++ b/Assets/UI_Extension/Scripts/Utilities/ObjectPool.cs @@ -0,0 +1,50 @@ +using System.Collections.Generic; +using UnityEngine.Events;
+using UnityEngine; + +namespace UIExt +{ + + 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); + } + } +} diff --git a/Assets/UI_Extension/Scripts/Utilities/ObjectPool.cs.meta b/Assets/UI_Extension/Scripts/Utilities/ObjectPool.cs.meta new file mode 100644 index 0000000..5fe9f8d --- /dev/null +++ b/Assets/UI_Extension/Scripts/Utilities/ObjectPool.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 75cc36573df9c1a4fa34a095a99d7eeb +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |