保存当前Scrollrect位置Unity3D

vsmadaxz  于 2022-12-13  发布在  其他
关注(0)|答案(2)|浏览(220)

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

vd8tlhqk

vd8tlhqk1#

可惜,你想做的东西没有现成的,你得自己做
首先使用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();
    }
}
xfb7svmp

xfb7svmp2#

您是否尝试过读/写normalizedPosition?
您基本上需要做两件事:您需要将此脚本附加到包含ScrollRect的GameObject,以便保持位置:

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); 
    }
}

在初始化ScrollRect并应用所需内容后,还需要应用normalizedPosition:

this.transform.Find("Scroll View").GetComponent<ScrollRect>().normalizedPosition = new Vector2(PlayerPrefs.GetFloat("scrollX"), 0F);

相关问题