Home > Blockchain >  How to get all children of object recursive with level set to go through?
How to get all children of object recursive with level set to go through?

Time:05-17

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class GetChilds : MonoBehaviour
{
    public List<Transform> allChildren = new List<Transform>();

    // Start is called before the first frame update
    void Start()
    {
        GetChildren(10);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void GetChildren(int level)
    {
        int count = 0;
        foreach (Transform child in transform.GetComponentsInChildren<Transform>())
        {
            if(count == level)
            {
                break;
            }

            allChildren.Add(child);
            count  ;
        }
    }
}

The wayt it is now it's not really a level it's just getting then umber of childs i'm setting in the level.

Level i mean how deep to loop through to get the children. For example :

Parent
   Children2
      Children3
      Children4
      Children5
         Children6
            Children7
            Children8
   Children9

If for example i set the level to 1 it should get Children2 and Children9 if set it to level 2 then it should get children2 children9 and children3,4,5 if set to level 3 then get also children6 if setting to level 4 then get also children7 and 8

also it could be nice maybe somehow to loop through all children recursive and map all the level so i will know for example that there are 5 levels or 10 levels. but the main goal is to find the children depending on the level ho deep through to loop.

I tried this but it's still not working as i wanted :

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class GetChilds : MonoBehaviour
{
    public List<Transform> allChildren = new List<Transform>();

    private List<int> levels = new List<int>();

    // Start is called before the first frame update
    void Start()
    {
        GetChildRecursive(transform, 3);
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void GetChildRecursive(Transform obj, int level)
    {
        foreach (Transform child in obj)
        {
            if (null == child)
                continue;

            if (child.childCount > 0)
            {
                levels.Add(child.childCount);
            }

            if (levels.Count <= level)
            {
                allChildren.Add(child);
                GetChildRecursive(child, level);

                levels = new List<int>();
            }
        }
    }
}

What it does now :

If for example i have this structure :

Child1
  Child2
  Child3
    Child5
    Child6
  Child4

If i set the level to 1 get only Child1

If i set the level to 2 get Child1 , Child2 , Child3 , Child4

If i set the level to 3 then get them all in this case. The problem is now if i set it to level 3 it will dig inside Child3 when it's getting to Child3 and i want it to get only the top of each level Child1 Child2 Child3 Child4 and not to dig into the Child3 yet.

Always get the level before and the level next but don't get through inside if the next level some child have more children.

First get the children of a child in the end get inside more as more levels.

CodePudding user response:

I made a simple script for you.

Invoke IterateOverChild method with target transform, 0 as current level param and desired depth.

  public class TranformIterator : MonoBehaviour
    {
        void Start()
        {
            IterateOverChild(transform, 0, 2);   
        }
    
        private void IterateOverChild(Transform original, int currentLevel, int maxLevel)
        {
            if (currentLevel > maxLevel) return;
            for (var i = 0; i < original.childCount; i  )
            {
                Debug.Log($"{original.GetChild(i)}"); //Do with child what you need
                IterateOverChild(original.GetChild(i), currentLevel   1, maxLevel);
            }
        }
    }
  • Related