- Home /
Dynamically add layers
Is it possible to create layers dynamically in Unity, and is there a limit?
Basically, I'm using the DepthMask shader from the unity wiki, and to create multiple layers, am basically forced to create a camera and associated culling layer for each mask "layer".
I'm trying to come up with a generic solution to be able to add this to any project without disturbing existing layers, but am hitting a brick wall. Ideally I'd like to be able to see what user layers are in use, and dynamically create higher layers (at runtime, but if possible at design time, that's okay too.) and assign various objects to those layers.
Any hints at how this might be achieved would be very welcome!
Answer by Berenger · Feb 20, 2012 at 07:59 PM
I don't think you can do that by script. However, it's just a list of names, so you can do your own. The layer would then be 1 << (myLayers.IndexOf( "Coconut" ) + nbUnityLayers; You'll have to manually enter nbUnityLayers though. I think the only difference will be that unity won't be able to display the layer's name in the inspector.
Just thinking through the way layermasks work, and I guess it's obvious that because it's stored as an integer, the implication is that there are a maximum of 31 layers in Unity? Given that there are only 32-bit integers, e.g. 31 bits to play with?
isn't (2^31)-1 much larger than 31, actually equals 2147483647...
im sure 31 is 2^5-1. And only 5 bits (with one bit for sign).
32 layers, not 31. Each layer needs its own bit—this is a layermask, not an integer—so it's not possible to have more than 32, as long as you're using a 32-bit number.
Your answer
Follow this Question
Related Questions
Export objects to a .3DS file at runtime 1 Answer
Platforms lose their frictionless property after dynamic layer change 0 Answers
How to dynamically change the shape of a mesh during runtime? 1 Answer
Dynamic Layer Switching Performance 0 Answers
Dynamicaly Creating A Plane With A JPG Material ... 2 Answers