From d2ab30e7a69bfe7efda63ae75812207377917bd3 Mon Sep 17 00:00:00 2001
From: miepzerino <o.skotnik@gmail.com>
Date: Sun, 30 Mar 2025 18:50:27 +0000
Subject: [PATCH] Merge branch 'Flexalon-UI-Layouts' into develop

---
 Assets/Flexalon/Samples/Runtime/FlexalonColorGradient.cs |   95 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 95 insertions(+), 0 deletions(-)

diff --git a/Assets/Flexalon/Samples/Runtime/FlexalonColorGradient.cs b/Assets/Flexalon/Samples/Runtime/FlexalonColorGradient.cs
new file mode 100644
index 0000000..3befce9
--- /dev/null
+++ b/Assets/Flexalon/Samples/Runtime/FlexalonColorGradient.cs
@@ -0,0 +1,95 @@
+using UnityEngine;
+
+namespace Flexalon.Samples
+{
+    // Changes the material or text color of each child to create a gradient.
+    [ExecuteAlways, AddComponentMenu("Flexalon Samples/Flexalon Color Gradient")]
+    public class FlexalonColorGradient : MonoBehaviour
+    {
+        // First color of the gradient.
+        [SerializeField]
+        private Color _color1;
+        public Color Color1
+        {
+            get => _color1;
+            set
+            {
+                _color1 = value;
+                UpdateColors(_node);
+            }
+        }
+
+        // Last color of the gradient.
+        [SerializeField]
+        private Color _color2;
+        public Color Color2
+        {
+            get => _color2;
+            set
+            {
+                _color2 = value;
+                UpdateColors(_node);
+            }
+        }
+
+        // Should update colors when layout changes?
+        [SerializeField]
+        private bool _runOnLayoutChange;
+        public bool RunOnLayoutChange
+        {
+            get => _runOnLayoutChange;
+            set
+            {
+                _runOnLayoutChange = value;
+                UpdateRunOnLayoutChange();
+            }
+        }
+
+        private FlexalonNode _node;
+
+        void OnEnable()
+        {
+            _node = Flexalon.GetOrCreateNode(gameObject);
+            UpdateRunOnLayoutChange();
+            UpdateColors(_node);
+        }
+
+        void UpdateRunOnLayoutChange()
+        {
+            _node.ResultChanged -= UpdateColors;
+            if (_runOnLayoutChange)
+            {
+                _node.ResultChanged += UpdateColors;
+            }
+        }
+
+        void OnDisable()
+        {
+            _node.ResultChanged -= UpdateColors;
+        }
+
+        private void UpdateColors(FlexalonNode node)
+        {
+            foreach (Transform child in transform)
+            {
+                var color = Color.Lerp(_color1, _color2, (float)(child.GetSiblingIndex()) / transform.childCount);
+#if UNITY_TMPRO
+                if (child.TryGetComponent<TMPro.TMP_Text>(out var text))
+                {
+                    text.color = color;
+                } else
+#endif
+#if UNITY_UI
+                if (child.TryGetComponent<UnityEngine.UI.Graphic>(out var graphic))
+                {
+                    graphic.color = color;
+                } else
+#endif
+                if (child.TryGetComponent<FlexalonDynamicMaterial>(out var tdm))
+                {
+                    tdm.SetColor(color);
+                }
+            }
+        }
+    }
+}
\ No newline at end of file

--
Gitblit v1.9.3