using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(ScrollRect))]
public class ScrollNormalizedPositionInitializer : MonoBehaviour {

    [SerializeField]
    private bool initializeHorizontal = default;
    [SerializeField]
    private bool initializeVertical = default;
    
    [SerializeField]
    private int firstIndex = 1;

    void Start(){
        var scrollRect = GetComponent<ScrollRect>();
        int activeChildCount = 0;
        foreach(Transform c in scrollRect.content){
            if(c.gameObject.activeSelf) ++activeChildCount;
        }
        if(initializeHorizontal){
            scrollRect.horizontalNormalizedPosition = (float)(firstIndex + 1.0f) / (float)(activeChildCount - 1);
        }
        if(initializeVertical){
            scrollRect.verticalNormalizedPosition = 1.0f - (float)(firstIndex + 1.0f) / (float)(activeChildCount - 1);
        }
    }
}
