Unity UGUI ScrollView 定位功能

using UnityEngine;
using UnityEngine.UI;

namespace Core.Misc.Utils
{
    public static class UGUIUtils
    {
        /// <summary>
        /// ScrollView定位。自动获取子物体来得到对应scrollRect.vertical(horizontal)NormalizedPosition
        /// 不支持vertical和horizontal同时勾选的情况
        /// </summary>
        /// <param name="inverse">从上到下,从右到左排列时勾选inverse</param>
        /// <param name="pixelOffset">像素偏移,向下、向右为正</param>
        /// <returns>0 ~ 1</returns>
        public static float GetScrollViewNormalizedPosition(ScrollRect scrollRect, int currentIndex, bool inverse = false, float pixelOffset = 0)
        {
            if (scrollRect.viewport == null || scrollRect.content == null)
            {
                DebugEO.LogError("ScrollView的viewport或content为空");
                return inverse ? 1 : 0;
            }

            var childTrans = scrollRect.content.GetChild(0)?.transform as RectTransform;
            if (childTrans == null)
            {
                DebugEO.LogError("ScrollView的Content下没有物体或者物体没有RectTransform");
                return inverse ? 1 : 0;
            }

            if (scrollRect.vertical && scrollRect.horizontal)
            {
                DebugEO.LogError("暂不支持vertical和horizontal同时勾选的情况");
                return inverse ? 1 : 0;
            }

            Rect viewportRect = scrollRect.viewport.rect;
            Rect contentRect  = scrollRect.content.rect;
            Rect childRect    = childTrans.rect;

            if (scrollRect.vertical)
            {
                var group = scrollRect.content.GetComponent<VerticalLayoutGroup>();
                if (group == null)
                {
                    DebugEO.LogError("获取VerticalLayoutGroup失败");
                    return inverse ? 1 : 0;
                }
                var diff = viewportRect.height - contentRect.height;
                var elementLength = childRect.height + group.spacing;
                if (inverse)
                    return Mathf.Clamp01((1 - currentIndex * elementLength - pixelOffset) / diff);
                else
                    return Mathf.Clamp01((currentIndex * elementLength - pixelOffset) / diff);
            }

            if (scrollRect.horizontal)
            {
                var group = scrollRect.content.GetComponent<HorizontalLayoutGroup>();
                if (group == null)
                {
                    DebugEO.LogError("获取HorizontalLayoutGroup失败");
                    return inverse ? 1 : 0;
                }
                var diff = viewportRect.width - contentRect.width;
                var elementLength = childRect.width + group.spacing;
                if (inverse)
                    return Mathf.Clamp01((1 - currentIndex * elementLength + pixelOffset) / diff);
                else
                    return Mathf.Clamp01((currentIndex * elementLength + pixelOffset) / diff);
            }
            
            return inverse ? 1 : 0;
        }
    }
}
上一篇:[Unity编辑器扩展基础总结] 第4章 脚本化对象 ScriptableObject


下一篇:k8s之list-watch机制、节点调度以及亲和性