playMaker

Author Topic: Playmaker Action to Scroll to a specific element in ScrollRect with Unity UI?  (Read 82 times)

mmzbr

  • Playmaker Newbie
  • *
  • Posts: 30
    • View Profile
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

mmzbr

  • Playmaker Newbie
  • *
  • Posts: 30
    • View Profile
I've tried to create this custom actions based on those scripts but I'm really bad at coding.  :-\