1

I am new with unity and C#, I have question about how I save current scrollrect position. Example : I am scrolling the view , and move to another scene and then back to previous scene but the scroll shows the previous position before I moved the scene, not resetting the scroll to default.

lLm003
  • 11
  • 2

2 Answers2

0

Unfortunately, what you want to make is not available ready-made, you have to make it yourself

first use Recyclable-Scroll-Rect

When scrolling to the bottom of the scroll, you have to save the id you sent to DemoCall via PlayerPrefs, then when you go to another scene and back again to the selected scene, call the scroll info from the point it left off, which is the id you saved

EDIT

After adding the Recyclable-Scroll-Rect, you can use this code

using System.Collections.Generic;
using UnityEngine;
using PolyAndCode.UI;
using System.Collections;

public struct ContactTsnif
{
    public string id;
}
public class Objx
{
    public string id;
}

public class RecyclTsnif : MonoBehaviour, IRecyclableScrollRectDataSource
{


    [SerializeField]
    RecyclableScrollRect _recycHat;
    
    public GameObject RecyScrHat;
    [SerializeField]
    public int _dataLenHat;
    public int beginning;
    private List<ContactTsnif> _contactList = new List<ContactTsnif>(); 
    
    
    public List<string> id = new List<string>();

    void Start()
    {
        beginning = PlayerPrefebs.GetInt("Start", 5)// start with 5
        GetHat();
    }
    
    public void GetHat()
    {
        _dataLenHat = 0;
        _recycHat.DataSource = this;
        InitDataHat();
        RecyScrHat.GetComponent<RecyclableScrollRect>().Initialize();
    }
    public void InitDataHat()
    {
        if (_contactList != null) _contactList.Clear();

        for (int i = beginning; i < _dataLenHat;)
        {
            ContactTsnif obj = new ContactTsnif();  
            obj.id = id[i];
            i++;
            _contactList.Add(obj);
        }
    }
    #region DATA-SOURCE


    public int GetItemCount()
    {
        return _contactList.Count;
    }


    public void SetCell(ICell cell, int index)
    {
        var item1 = cell as DemoTsnif;
        item1.ConfigureCellSor(_contactList[index], index);
    }

    #endregion
}

Demo

using UnityEngine;
using System;
using System.Collections;

public class DemoTsnif : MonoBehaviour, ICell
{

    private ContactTsnif _ContactInfo;
    private int _cellIndex;
    public int id;

    public void GetData()
    {
        
    }
    
    public void ConfigureCellSor(ContactTsnif contactInfo,int cellIndex)
    {
            _cellIndex = cellIndex;
            _ContactInfo = contactInfo;
            id = contactInfo.id;

            GetData();
    }
}
Lelouch kun
  • 449
  • 1
  • 13
  • I understand with sent id to DemoCall with PlayerPrefs but I don't understand about "call the scroll info from the point it left off, which is the id you saved" can you explain how do that ? maybe with example script. – lLm003 Feb 19 '22 at 07:43
  • For the sake of information: you probably don't want to be touching PlayerPrefs, as on Windows, [it saves into the registry](https://docs.unity3d.com/ScriptReference/PlayerPrefs.html), which is pretty terrible design that should absolutely be avoided – dimitar.bogdanov Feb 19 '22 at 18:25
  • You are not really saving the information. What you memorize is where the information begins beginning = PlayerPrefebs.GetInt("Start", 5) – Lelouch kun Feb 19 '22 at 18:47
0

Do you tried read / write normalizedPosition?

You basically need to do two things: You need to attach this script to the GameObject which contains the ScrollRect in order to persist the position:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems; // Required when using event data
using UnityEngine.UI;

public class DragNotify : MonoBehaviour, IEndDragHandler // required interface when using the OnEndDrag method.
{
    //Do this when the user stops dragging this UI Element.
    public void OnEndDrag(PointerEventData data)
    {
        PlayerPrefs.SetFloat("scrollX", this.GetComponent<ScrollRect>().normalizedPosition.x); 
    }
}

You also need to apply the normalizedPosition once you initialized the ScrollRect and after you applied the desired content:

this.transform.Find("Scroll View").GetComponent<ScrollRect>().normalizedPosition = new Vector2(PlayerPrefs.GetFloat("scrollX"), 0F);
Marvin Krüger
  • 127
  • 1
  • 1
  • 11