GithubHelp home page GithubHelp logo

mukarillo / unitydynamicscrollrect Goto Github PK

View Code? Open in Web Editor NEW
468.0 14.0 45.0 10.57 MB

An optimized approach to lists with dozens of elements and a Pooling system

License: MIT License

C# 100.00%
unity ui ui-components scrollview scroll unity3d-plugin unity2d

unitydynamicscrollrect's Introduction

UnityDynamicScrollRect

An optimized approach to lists with dozens of elements.

Example

How to use

you can find a pratical example inside this repository in DynamicScrollScene scene

1 - Create a class to store all the information that each element of the list will need.

public class ExampleData
{
      public int postId;
      public int id;
      public string name;
      public string email;
      public string body;
}

2 - Create a class that extends DynamicScrollObject<ExampleData> and implement its abstract members (make sure to call base.updateScrollObject(item, index);) and set the object width and height in currentWidth and currentHeight.

public class ExampleDynamicObject : DynamicScrollObject<ExampleData>
{
  public override float currentHeight { get; set; }
  public override float currentWidth { get; set; }

  private Text idText;
  private Text nameEmailText;
  private Text bodyText;

  public void Awake()
  {
    currentHeight = GetComponent<RectTransform>().rect.height;
    currentWidth = GetComponent<RectTransform>().rect.width;

    idText = transform.Find("PostId").GetComponent<Text>();
    nameEmailText = transform.Find("NameEmail").GetComponent<Text>();
    bodyText = transform.Find("Body").GetComponent<Text>();         
  }

  public override void updateScrollObject(ExampleData item, int index)
  {
    base.updateScrollObject(item, index);

    idText.text = item.id.ToString();
    nameEmailText.text = string.Format("{0} ({1})", item.name, item.email);
    bodyText.text = item.body;
  }
}

3 - Create a class to initiate the DynamicList (use DynamicScrollRect instead of ScrollRect)

public class ExampleScroll : MonoBehaviour
{
  public DynamicScrollRect verticalScroll;
  public GameObject referenceObject;

  private DynamicScroll<ExampleData, ExampleDynamicObject> mVerticalDynamicScroll = new DynamicScroll<ExampleData, ExampleDynamicObject>();

  public IEnumerator Start()
  {
    WWW www = new WWW(@"https://jsonplaceholder.typicode.com/comments");
    yield return www;
    var data = JsonHelper.getJsonArray<ExampleData>(www.text);

    mVerticalDynamicScroll.spacing = 5f;
    mVerticalDynamicScroll.Initiate(verticalScroll, data, 0, referenceObject);
  }      
}

DynamicScroll<T, T1> public overview

Properties

name type description
spacing float Value that represent the spacing between elements of the list
centralizeOnStop bool If the list should centralize the closest element to the center of the viewport after stop moving
objectPool readonly Pooling < T1 > The elements of the list
OnDragEvent Action < Vector2 > Event that triggers whenever the user scrolls the list, the parameter represent the velocity of the drag
OnBeginDragEvent UnityEvent < PointerEventData > Event that triggers in the first frame of dragging
OnEndDragEvent UnityEvent < PointerEventData > Event that triggers in the last frame of dragging

Methods

dynamicScroll.Initiate

  • Description: Initiate the scroll rect with objReference objects applying infoList data.

  • Parameters:

name type description
scrollRect ScrollRect a reference to the scroll rect
infoList T[] the list with the data information
startIndex int the item of index startindex will be the first element of the list
objReference GameObject a reference of the object that will be inside the list
createMoreIfNeeded bool if the list needs more itens, it will create more if createMoreIfNeeded == true
forceAmount int? if setted, it will force forceAmount objects to be created at start

dynamicScroll.ChangeList

  • Description: Change the current list of the scroll rect.

  • Parameters :

name type description
infoList T[] the list with the data information
startIndex int the item of index startindex will be the first element of the list. If -1, the current index will be setted.
resetContentPosition bool reset list position

dynamicScroll.RefreshPosition

  • Description: Repaint the whole scroll rect. This is useful if any item inside the scroll rect changes the size (currentWidth and currentHeight).

dynamicScroll.ToggleScroll

  • Description: Enable or Disable the ability to scroll the list.

  • Parameters :

name type description
active bool enable or Disable the ability to scroll the list

dynamicScroll.CanMove

  • Description: Returns true if all directions send thro parameter are available.

  • Parameters :

name type description
directions ScrollDirection Enum flag with all the directions you want to know if are available

dynamicScroll.MoveToIndex

  • Description: Tweens the content to centralize the object of index specified in the parameters.

  • Parameters :

name type description
i int Index of the element to be centralized
totalTime float? Total time to the animation happen (if you choose to input this value, the next one will be ignored)
timePerElement float? This value will be multiplied by the difference between the current centralized element and the target element to get the totalTime

dynamicScroll.GetCentralizedObject

  • Description: Returns the closest element to the center of the viewport.

dynamicScroll.GetLowest

  • Description: Returns the most left (if horizontal scroll) or most bottom (if vertical scroll) T1 object.

dynamicScroll.GetHighest

  • Description: Returns the most right (if horizontal scroll) or most upper (if vertical scroll) T1 object.

unitydynamicscrollrect's People

Contributors

breckoec avatar igorhosantos avatar lenweseregon avatar mukarillo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

unitydynamicscrollrect's Issues

Scroll to index?

Hello, i think you might want to add some feature, that will be great: scroll to index
it basically do is that scrolls the scrollview to make the element visible, on center of the scrollRect (if possible), you'll need to pass an index argument to the method and it should animate or jump directly to the scroll position (by an optional bool/speed parameter?)

i have seen that feature in similar assets and i think it will be a great addiction if you can for sure :)

Use IList instead of an array to make solution more flexible

If you make a little change and use IList interface instead of info array list, your solution will be prepared for use in new scenarios.

For example, I can't load all information and store it in an array, because it is too big. But if I can use an IList, I can implement a way to load only information which dynamic scroll needs.

Items are not removed when data list gets shorter?

Hi,

I am trying to integrate this into my project. My items are addable and deletable. When I add or delete them, I do this:

public void UpdateList(ItemData[] listData) //updated list of items, could be more, could be less than before
        {
            if(!dynamicScroll.Initialized) //I added this bool in the DynamicScroll to know if DynamicScroll.Initiate(...) has been called already
            {
                dynamicScroll.spacing = scrollSpacing;
                dynamicScroll.centralizeOnStop = scrollCentralizeOnStop;
                dynamicScroll.Initiate(dynamicScrollRect, listData, 0, sceneObjectTemplate);
            }
            else
            {
                dynamicScroll.ChangeList(listData);
            }
        }

It works for adding new items. But when I remove items, the UI elements are not removed (or hidden).

Am I using this right? How do I do this properly?

Start on the last element of the collection.

Hi, first of all, thank you to sharing this great asset.

The thing is that i need to change the start point of the scroll, by this I mean to start at the end of the list, on the last object, therefore on the end of the scrollrect so you can only start scrolling up.

I saw that on the methods Initialize and ChangeList, there is a startIndex that can be set, but if for example i use the mData.Length - 1 instead of 0, it only shows the last object. Looking why this is happening I found that in the CreateList method is no support for using the the max size of a collection. So I proceeded to modify this and add the functionality to iterate from the last object to the first one but it still not working as I want.

Can you guide me what I need to change/modify on the pooling or the refreshing of objects to achieve this ?

Thanks.

[Feature Request] Elastic Movement

Asset works great from the first look. However I'm missing elastic movement when dragging beyond the end or beginning. Is there a plan to include it?

Compatible with Grid Layout?

Hello,

First, thank you to sharing with us this great project, this is very useful!

Now I would like to apply the same kind of process but for elements inside of a grid layout that is inside a VerticalScrollRect.

Basically, it the almost same thing instead of I have more than one element by row, and it doesn't seem to work well from now.

I will try to update the code to make this possible, but if you have any advice of how you would do it, it will be great! ๐Ÿ‘

Can't add through Package Manager in Unity 2021.1.19f1

I know there hasn't been much work done to this in years, but I still figured I'd throw the question your way.
Any chance we can have this updated so that one can add the package through the Package Managers "Add package from git URL"?

Scollbar support?

Hi, thanks for this great component.

I want to show a scrollbar on my UI, which can interact with the ScrollRect.
I set a vertical scrollbar to Dynicamic ScrollRect, but the scrollbar did not work properly.
How can I config the scrollbar?

ScrollObjct is not necessary to derive from MonoBehaviour

I think it's not necessary to add a mono script on each item in scroll list. When driving from monoBehaviour, unity have to execute code in Awake , which is performance-cost.
As a replacement, I prefer to add a virtual methd in PoolingObject like:

public GameObject gameObject { get; set; }

public virtual void AttachView(GameObject go)
{
    gameObject = go;
}

And call it in Pooling.CreateObject:

var go = GameObject.Instantiate(referenceOjbct);
var obj = new T();
obj.AttachView(go);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.