Hola buenos dias...
Mi duda era la siguiente, ¿Como podría insertar los hijos de un objeto en un array de GameObject[]??
Por poneros un ejemplo:
Lo que quiero es que se me muestren los hijos Art 7, Art 6 ...etc en el Array Artículos...
y luego también quería preguntaros si hubiera una forma de ordenarlos por orden alfabético...
Gracias de antemano chicos...
Edito :
He encontrado GetComponentsInChildren.<Transform>();
Pero recoge en el array todos los objetos, hasta los hijos de los hijos, y yo lo que busco es que solo aparezcan los primeros hijos...
Puedes recorrer los hijos usando un bucle. Primero inicializas el array con transform.ChildCount, luego haces un for añadiendo cada child al array con GetChild, puedes referenciar el Transform o la clase que prefieras.
Te dejo la documentación donde aparecen ejemplos:
https://docs.unity3d.com/ScriptReference/Transform.html (https://docs.unity3d.com/ScriptReference/Transform.html)
Para coger los GameObject emparentados utiliza el método GetChild junto con childCount:
using UnityEngine;
using System.Collections;
public class cogerNombres : MonoBehaviour {
public string[] nombres;
// Use this for initialization
void Start () {
this.nombres = new string[this.transform.childCount];
for (int i = 0; i < this.transform.childCount; i++) this.nombres = this.transform.GetChild(i).name;
}
}
También puedes utilizar un foreach:
using UnityEngine;
using System.Collections;
public class cogerNombres : MonoBehaviour {
// Use this for initialization
void Start () {
foreach(Transform child in this.transform)
{
Debug.Log(child.name);
}
}
}
Para ordenar los nombres utiliza la clásica rutina de ordenación que emplea dos bucles desde el principio o desde el final según quieras el orden acendente/descendente:
using UnityEngine;
using System.Collections;
public class ordenarNombres : MonoBehaviour {
// Use this for initialization
void Start () {
this.ordenar();
}
//De menor a mayor. Ascendente: a, b, c, d, e, f, ...
void ordenar()
{
for(int w = 0;w < this.transform.childCount; w++)
for(int i = w + 1; i < this.transform.childCount; i++)
{
if (string.Compare(this.transform.GetChild(i).name , this.transform.GetChild(w).name, true) < 0)
{
this.transform.GetChild(i).SetSiblingIndex(w);
}
}
}
}