简体   繁体   English

保存当前 Scrollrect position Unity3D

[英]Save current Scrollrect position Unity3D

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.我是 unity 和 C# 的新手,我对如何保存当前 scrollrect position 有疑问。示例:我正在滚动视图,然后移动到另一个场景,然后返回到上一个场景,但在我移动场景之前滚动显示之前的 position ,而不是将滚动重置为默认值。

Unfortunately, what you want to make is not available ready-made, you have to make it yourself可惜你想做的没有现成的,得自己做

first use Recyclable-Scroll-Rect首先使用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当滚动到滚动底部时,你必须保存你通过 PlayerPrefs 发送给 DemoCall 的 id,然后当你 go 到另一个场景并再次回到所选场景时,从它停止的地方调用滚动信息,这是你保存的id

EDIT编辑

After adding the Recyclable-Scroll-Rect , you can use this code添加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
}

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

Do you tried read / write normalizedPosition?你试过读/写 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:您基本上需要做两件事:您需要将此脚本附加到包含 ScrollRect 的 GameObject 以保留 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:您还需要在初始化 ScrollRect 并应用所需内容后应用 normalizedPosition:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM