x


Fast layer assignment

Hi, I need to add a gameobject and all its children to a layer via c# code. In editor i'm asked if i want to add the single object or the whole hierarchy. i wonder if there is an equally fast way to do this in a script without writing explicit code

more ▼

asked Sep 02 '10 at 01:55 PM

Patrik gravatar image

Patrik
59 13 13 20

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

To put a stop to your wondering, the short answer is that there is no implemented method to do this that I can find.

As == says, you will have to recurse through the hierarchy.

You could do something like:

//recursive calls
void MoveToLayer(Transform root, int layer) {
    root.gameObject.layer = layer;
    foreach(Transform child in root)
        MoveToLayer(child, layer);
}

or

//no recursive calls
using System.Collections;
void MoveToLayer(Transform root, int layer) {
    Stack<Transform> moveTargets = new Stack<Transform>();
    moveTargets.push(root);
    Transform currentTarget;
    while(moveTargets.Count != 0)
    {
        currentTarget = moveTargets.Pop();
        currentTarget.gameObject.layer = layer;
        foreach(Transform child in currentTarget)
            moveTargets.Push(child);
    }
}
more ▼

answered Sep 02 '10 at 07:21 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

(comments are locked)
10|3000 characters needed characters left

There is no reason you couldn't recursively move down the top-most object's transform hierarchy and set each gameobject's layer accordingly.

Add this script to your Editor folder:

using UnityEngine;
using UnityEditor;
using System.Collections;

public class LayerChanger : ScriptableWizard {

    public int layer;
    public GameObject gameObject;
    [MenuItem ("GameObject/Manage Layers Recursively")]

    static void CreateWizard () {
        ScriptableWizard.DisplayWizard("Manage Layers", typeof (LayerChanger),"Improv!");
    }

    void OnWizardCreate () {
        Pew(gameObject);
    }

    void Pew(GameObject g)
    {
        g.layer = layer;
        foreach(Transform t in g.transform)
        {
            t.gameObject.layer = layer;
            Pew(t.gameObject);
        }
    }
}

Cheers

==

more ▼

answered Sep 02 '10 at 02:16 PM

equalsequals gravatar image

equalsequals
4.5k 16 28 63

I know. I was asking if there is already a method to do this

Sep 02 '10 at 02:40 PM Patrik

Write the 20-odd lines of code needed to do it?

Sep 02 '10 at 07:17 PM equalsequals

It's even less. More like 5-13 lines.

Sep 02 '10 at 07:24 PM skovacs1

Feeling generous, wrote it for you.

Sep 02 '10 at 07:33 PM equalsequals

Did the question specify whether the code being asked about was for the editor or the run-time? I figured since the editor will do this for you as the question describes that they didn't really need an editor script. Thanks for covering the other interpretation - one of us has to have the correct answer.

Sep 02 '10 at 09:05 PM skovacs1
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x147
x106
x86

asked: Sep 02 '10 at 01:55 PM

Seen: 3776 times

Last Updated: Sep 02 '10 at 01:55 PM