Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: mmzbr on May 19, 2020, 05:49:48 PM

Title: Playmaker Action to Scroll to a specific element in ScrollRect with Unity UI?
Post by: mmzbr on May 19, 2020, 05:49:48 PM
I need to auto scroll to a specific element in ScrollRect content (like the player icon on the region map) when I open my Map.

Can someone help me create a Playmaker Action based on this script?

Code: [Select]
protected ScrollRect scrollRect;
protected RectTransform contentPanel;

public void SnapTo(RectTransform target)
    {
        Canvas.ForceUpdateCanvases();

        contentPanel.anchoredPosition =
            (Vector2)scrollRect.transform.InverseTransformPoint(contentPanel.position)
            - (Vector2)scrollRect.transform.InverseTransformPoint(target.position);
    }

Or

Code: [Select]
using UnityEngine;
using UnityEngine.UI;

namespace BlinkTalk
{
    public static class ScrollRectExtensions
    {
        public static Vector2 GetSnapToPositionToBringChildIntoView(this ScrollRect instance, RectTransform child)
        {
            Canvas.ForceUpdateCanvases();
            Vector2 viewportLocalPosition = instance.viewport.localPosition;
            Vector2 childLocalPosition   = child.localPosition;
            Vector2 result = new Vector2(
                0 - (viewportLocalPosition.x + childLocalPosition.x),
                0 - (viewportLocalPosition.y + childLocalPosition.y)
            );
            return result;
        }
    }
}

Code: [Select]
private void Update()
    {
        MyScrollRect.content.localPosition = MyScrollRect.GetSnapToPositionToBringChildIntoView(someChild);
    }

Reference: https://stackoverflow.com/questions/30766020/how-to-scroll-to-a-specific-element-in-scrollrect-with-unity-ui
Title: Re: Playmaker Action to Scroll to a specific element in ScrollRect with Unity UI?
Post by: mmzbr on May 23, 2020, 05:30:11 PM
I've tried to create this custom actions based on those scripts but I'm really bad at coding.  :-\