N
N
nikita_fridman21082020-01-30 06:31:54
PHP
nikita_fridman2108, 2020-01-30 06:31:54

How to switch to a given block in SnapScrolling?

I found a lesson on creating a SnapScrollingMenu, but there was such a problem:

every time when the game starts, it shows the first one as the selected block, but if I want, for example, to show the 100th element of the array immediately upon loading? How to be here? Code from the lesson on Youtube, commented by the author). I don't know much about C#, so any detailed answers would be greatly appreciated.

//This script from tutorial by AndroidHelper.
//Link to the channel: https://www.youtube.com/c/huaweisonichelpAHRU
 
using UnityEngine;
using UnityEngine.UI;
 
public class SnapScrolling : MonoBehaviour
{
    [Range(1, 50)]
    [Header("Controllers")]
    public int panCount;
    [Range(0, 500)]
    public int panOffset;
    [Range(0f, 20f)]
    public float snapSpeed;
    [Range(0f, 10f)]
    public float scaleOffset;
    [Range(1f, 20f)]
    public float scaleSpeed;
    [Header("Other Objects")]
    public GameObject panPrefab;
    public ScrollRect scrollRect;
 
    private GameObject[] instPans;
    private Vector2[] pansPos;
    private Vector2[] pansScale;
 
    private RectTransform contentRect;
    private Vector2 contentVector;
    
    [SerializeField]
    private int selectedPanID;
    private bool isScrolling;
 
    private void Start ()
    {
        contentRect = GetComponent<RectTransform>();
        instPans = new GameObject[panCount];
        pansPos = new Vector2[panCount];
        pansScale = new Vector2[panCount];
        
        for (int i = 0; i < panCount; i++)
        {
            instPans[i] = Instantiate(panPrefab, transform, false);
            instPans[i].GetComponent<LoadLevelHandler>().level = i + 1;
            if (i == 0)
            {
                instPans[i].transform.localPosition = new Vector2(0, 0);
                continue;
            }
            instPans[i].transform.localPosition = new Vector2(instPans[i-1].transform.localPosition.x + panPrefab.GetComponent<RectTransform>().sizeDelta.x + panOffset,
                0);
            pansPos[i] = -instPans[i].transform.localPosition;
        }
    }
 
    private void FixedUpdate()
    {
        if (contentRect.anchoredPosition.x >= pansPos[0].x && !isScrolling || contentRect.anchoredPosition.x <= pansPos[pansPos.Length - 1].x && !isScrolling)
            scrollRect.inertia = false;
        float nearestPos = float.MaxValue;
        for (int i = 0; i < panCount; i++)
        {
            float distance = Mathf.Abs(contentRect.anchoredPosition.x - pansPos[i].x);
            if (distance < nearestPos)
            {
                nearestPos = distance;
                selectedPanID = i;
            }
            float scale = Mathf.Clamp(1 / (distance / panOffset) * scaleOffset, 0.5f, 1f);
            pansScale[i].x = Mathf.SmoothStep(instPans[i].transform.localScale.x, scale + 0.3f, scaleSpeed * Time.fixedDeltaTime);
            pansScale[i].y = Mathf.SmoothStep(instPans[i].transform.localScale.y, scale + 0.3f, scaleSpeed * Time.fixedDeltaTime);
            instPans[i].transform.localScale = pansScale[i];
        }
        float scrollVelocity = Mathf.Abs(scrollRect.velocity.x);
        if (scrollVelocity < 400 && !isScrolling) scrollRect.inertia = false;
        if (isScrolling || scrollVelocity > 400) return;
        contentVector.x = Mathf.SmoothStep(contentRect.anchoredPosition.x, pansPos[selectedPanID].x, snapSpeed * Time.fixedDeltaTime);
        contentRect.anchoredPosition = contentVector;
    }
 
    public void Scrolling(bool scroll)
    {
        isScrolling = scroll;
        if (scroll) scrollRect.inertia = true;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maksim Fedorov, 2019-05-06
@wolverine777

Do not work with global variables, work with the Request object, it already has all the values: both url parameters and query parameters

public function supPage(Request $request){
      $siteId = $request->query->get('siteId');
      // ...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question