Skip to content

Instantly share code, notes, and snippets.

@adamski11
Last active November 21, 2019 16:23
Show Gist options
  • Save adamski11/bd61693a1f90130c536d080496012ed9 to your computer and use it in GitHub Desktop.
Save adamski11/bd61693a1f90130c536d080496012ed9 to your computer and use it in GitHub Desktop.
Use this in Unity to quickly give the children of a transform a number. You can use a prefix to replace their existing name, a suffix to add information after the number, and choose to start from zero.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode()]
public class NumberiseChildrenUtility : MonoBehaviour
{
//Use https://gist.github.com/adamski11/bd61693a1f90130c536d080496012ed9 to use a button in the inspector
//[TestButton("Numberise Children", "NumberiseChildren")]
[Header("Click to Run")]
public bool shouldRun = false;
[Header("Values")]
public string prefix = "";
public string suffix = "";
public int countBackIndex = 0;
public bool startAtZero = false;
public bool includeSpaceBeforeNumber = true;
void Update()
{
if(shouldRun)
{
NumberiseChildren();
shouldRun = false;
}
}
void NumberiseChildren()
{
for (int i = 0; i < transform.childCount; i++)
{
string newName = transform.GetChild(i).name;
if (prefix != "")
newName = prefix;
newName = newName.Substring(0, newName.Length - countBackIndex);
newName = newName + (includeSpaceBeforeNumber ? " " : "") + (startAtZero ? i : i+1) + suffix;
transform.GetChild(i).name = newName;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment