LoopScrollRect icon indicating copy to clipboard operation
LoopScrollRect copied to clipboard

Horizontal scroll rect all item call ReturnObject

Open KimYeonmu opened this issue 3 years ago • 6 comments

https://user-images.githubusercontent.com/38003448/175812359-f30717dc-d124-4530-b6e6-d0f04e673258.mov

Hi I Used loop horizontal scroll rect Switching from Game View to Scene View causes problems.

KimYeonmu avatar Jun 26 '22 11:06 KimYeonmu

// Init
m_LoopCropsScrollRect.prefabSource = this;
m_LoopCropsScrollRect.dataSource = this;
m_LoopCropsScrollRect.totalCount = m_ProductDataList.Count;
m_LoopCropsScrollRect.RefreshCells();
m_LoopCropsScrollRect.RefillCells();`
// Get Object
public GameObject GetObject(int index)
{
        if (m_Pool.Count == 0)
        {
                var element = Instantiate(m_CropsElementPrefab);
                element.transform.SetParent(m_LoopCropsScrollRect.content, false);
                return element.gameObject;
        }

        Transform candidate = m_Pool.Pop();
        candidate.SetParent(m_LoopCropsScrollRect.content, false);
        candidate.gameObject.SetActive(true);
        return candidate.gameObject;
}
// Return Object
public void ReturnObject(Transform trans)
{
        trans.gameObject.SetActive(false);
        trans.SetParent(m_LoopCropsScrollRect.transform, false);

        m_Pool.Push(trans);
        Debug.Log("return");
}

KimYeonmu avatar Jun 26 '22 11:06 KimYeonmu

Hi,

Sorry for late reply. I tried to reproduce the problem within demo but failed. image

Would you mind providing LoopScroll's RectTransform information in SceneView? From the video, it seems all elements in LoopScroll are returned to pool, which means there is no space needed to fill. Also does switching back to GameView and drag helps?

qiankanglai avatar Jul 02 '22 08:07 qiankanglai

Hi image

Problems also occur when switching to game view.

Does it happen if the layout element component is not added?

KimYeonmu avatar Jul 04 '22 04:07 KimYeonmu

Hi everyone! I have the same problem, but not only with switching. It also occurs on start sometimes. P.s. I don't have Layout Element component on button prefab because it should change its size depending on text inside.

Bdiebeak avatar Jul 07 '22 14:07 Bdiebeak

(Again apologize for only have time to reply in weekend)

From the screenshot, it looks right with the width/right. At the same time I can reproduce the problem with LayoutElement disabled. LoopScroll is relying on LayoutUtility.GetPreferredHeight to get the element's size to fill. https://github.com/qiankanglai/LoopScrollRect/blob/c6da00ac2d378d0d8142c75abe9ef24d713a54ff/Runtime/LoopVerticalScrollRect.cs#L20

If LayoutElement is not added, you may have to implement ILayoutElement manually to feed the element's size info.

qiankanglai avatar Jul 10 '22 06:07 qiankanglai

P.s. I don't have Layout Element component on button prefab because it should change its size depending on text inside.

I just wrote a small example for customizing size per element.

public class ScrollIndexCallback1 : MonoBehaviour, ILayoutElement
{
    public Image image;
    public Text text;
    private int index = 0;

    public float minWidth { get { return -1; } }

    public float preferredWidth { get { return 150; } }

    public float flexibleWidth { get { return -1; } }

    public float minHeight { get { return -1; } }

    public float preferredHeight { get { return (index % 2 == 0) ? 50 : 80; } }

    public float flexibleHeight { get { return -1; } }

    public int layoutPriority { get { return 0; } }
    
    public void CalculateLayoutInputHorizontal() { }

    public void CalculateLayoutInputVertical() { }

    void ScrollCellIndex (int idx) 
    {
        index = idx;
        string name = "Cell " + idx.ToString ();
        if (text != null) 
        {
                text.text = name;
        }
        if (image != null)
        {
            image.color = Rainbow(idx / 50.0f);
        }
        gameObject.name = name;
   }
}

1657434844161

qiankanglai avatar Jul 10 '22 06:07 qiankanglai