Skip to content

Instantly share code, notes, and snippets.

@stopiccot
Created April 19, 2020 18:47
Show Gist options
  • Save stopiccot/575593b8c2e5ec6c7c58c683ed4647cc to your computer and use it in GitHub Desktop.
Save stopiccot/575593b8c2e5ec6c7c58c683ed4647cc to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Swapper : MonoBehaviour
{
public void Swap()
{
var rt = GetComponent<RectTransform>();
var m = rt.anchorMin;
m.x = 1 - m.x;
rt.anchorMin = m;
var n = rt.anchorMax;
n.x = 1 - n.x;
rt.anchorMax = n;
var p = rt.anchoredPosition;
p.x = -p.x;
rt.anchoredPosition = p;
var k = rt.pivot;
k.x = 1 - k.x;
rt.pivot = k;
var lg = GetComponent<HorizontalLayoutGroup>();
if (lg != null)
{
Transform[] tt = new Transform[transform.childCount];
for (int i = 0; i < transform.childCount; i++)
{
tt[i] = transform.GetChild(i);
}
for (int i = 0; i < transform.childCount; i++)
{
tt[i].SetSiblingIndex(transform.childCount - 1 - i);
}
}
}
[ContextMenu("Swap All")]
public void SwapAll()
{
foreach (var o in FindObjectsOfType<Swapper>())
{
o.Swap();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment