qiankanglai / LoopScrollRect

These scripts will make your UGUI ScrollRect reusing cells, to improve performance, loading time and draw calls.
http://qiankanglai.me/2015/08/15/LoopScrollRect/
MIT License
2.29k stars 476 forks source link

Help passing an inventory list to the Scrollrect #150

Closed Fishchip511 closed 1 year ago

Fishchip511 commented 1 year ago
I want to pass an inventory that contains scriptable objects of type SO_Item and display it on a scrollrect 
via the InitOnStartMulti? Is that how it works? My current issue is I am only able to get the inventory's count 
and not change the default CellPrefab into the contents of the inventory. 

For example: Inventory contains 3 SO_Item bow, sword, spear what happens is Cellprefab would 
only instantiate 3 default CellPrefabs. It doesn't change CellPrefab's display to bow, sword, spear. 

I will provide more simplified context via code below.

public class ScrollIndexCallback_Ingredient : ScrollIndexCallbackBase
    {
       [SerializeField] private TMP_Text m_CellText;
       [SerializeField] private TMP_Text m_ValueText;
       [SerializeField] private Image m_Image;
       // scriptable object item
       [SerializeField] private SO_Item m_Item; 

       // other code based from ScrollIndexCallbackCustom

       private void InitializeData(int idx, object content, string ClickUniqueID = "", object ClickObject = null)
        {
            if (m_CellText != null)
            {
                m_CellText.text = m_Item.DisplayName;
            }

            if(m_ValueText != null)
            {
                m_ValueText.text = m_Item.BaseDamage.ToString();
            }

            if (m_Image != null)
            {
                m_Image.sprite = m_Item.Icon;
            }
        }       
public class Inventory : MonoBehaviour
{
   [SerializeField] private List<SO_Item> _itemsList;
}

I am not sure where and how to pass the inventory list properly so that it gets read by scrollrect.
Thanks in advance!
qiankanglai commented 1 year ago

There is one demo called DemoScene_MultiCell, providing three types of cells.

There are only two key APIs that need attension:

I would suggest check GetObject first, to make sure the right SO_Item is provided for every index.

Fishchip511 commented 1 year ago

Sorry for long reply just came back from vacation. Thanks for the reply I managed to get it working by following what you said. I also made a customlooplistbank that was tailored to handle SO_Item. Then I made a ScrollIndexCallback_SO_Item which weapons, consumables etc will inherit from.

Thank you so much for the useful scrollrect tool!