Skip to content

Instantly share code, notes, and snippets.

@No3371
Created January 22, 2022 10:17
Show Gist options
  • Save No3371/a96fb5f1ea36e7a87f684f519df924e5 to your computer and use it in GitHub Desktop.
Save No3371/a96fb5f1ea36e7a87f684f519df924e5 to your computer and use it in GitHub Desktop.
Reorder GameObjects to enable batching for UGUI.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIReorderer : MonoBehaviour
{
[Tooltip("The gameobjects get reordered accoring to this. Disable this cause no side effect.")]
public GameObject[] order;
[Tooltip("These are always lower then order[].")]
public GameObject[] baseInsert;
public bool destroyOnReordered;
void Start ()
{
for (var i = 0; i < order.Length; i++)
{
if (order[i] == null) continue;
order[i].transform.SetParent(this.transform, true);
order[i].transform.SetAsLastSibling();
}
for (var i = 0; i < baseInsert.Length; i++)
{
if (baseInsert[i] == null) continue;
baseInsert[i].transform.SetParent(this.transform, true);
baseInsert[i].transform.SetAsFirstSibling();
}
if (Application.isPlaying && destroyOnReordered) Destroy(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment