0

我是 Unity 和 C# 的新手,我对如何保存当前的 scrollrect 位置有疑问。示例:我正在滚动视图,然后移动到另一个场景,然后返回上一个场景,但滚动显示了我移动场景之前的上一个位置,而不是将滚动重置为默认值。

4

1 回答 1

0

不幸的是,你想做的不是现成的,你必须自己做

首先使用Recyclable-Scroll-Rect

当滚动到滚动底部时,您必须保存您通过 PlayerPrefs 发送给 DemoCall 的 id,然后当您转到另一个场景并再次返回所选场景时,从它离开的点调用滚动信息,即你保存的id

编辑

添加Recyclable-Scroll-Rect后,您可以使用此代码

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
}

演示

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();
    }
}
于 2022-02-18T23:24:18.810 回答