Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by youngapprentice · Nov 16, 2012 at 12:03 AM · cameraculling

Edit Camera Culling Mask

Hi, all! Is it possible to change my camera's culling mask via code? How so?

(I'd like to change it from rendering just one layer, to rendering everything, then back to just the player).

Thanks! -YA

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
19
Best Answer

Answer by Paulius-Liekis · Nov 16, 2012 at 10:56 AM

http://docs.unity3d.com/Documentation/ScriptReference/Camera-cullingMask.html

 int oldMask = camera.cullingMask;
 
 // change mask
 camera.cullingMask = (1 << LayerMask.NameToLayer("TransparentFX")) | (1 << LayerMask.NameToLayer("OtherLayer"));
 // do something
 // ...
 
 // restore mask
 camera.cullingMask = oldMask;
Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image youngapprentice · Nov 16, 2012 at 12:05 PM 0
Share

Oh yes I understand the syntax for it, I just need someone to explain it more in depth for me. Like, why is there a pipe character and what is its functionality, why do you have two arguments, and which one does what?

avatar image Paulius-Liekis · Nov 16, 2012 at 12:24 PM 1
Share

culling$$anonymous$$ask is stored as bit mask. First bit (i.e. (1 << 0) or simply 1) represenst first layer. Sencond bit (i.e. (1 << 1) or simply 2) represent second layer. Third bit (i.e. (1 << 2) or simply 4) represent third layer. And so on. When you want to have two layers active you combine their appropriate bits with | operator. For example I need fist and fourth layer, so my mask would be ((1 << 0) | (1 << 3)) = (1 | 8) = 9

avatar image Paulius-Liekis · Nov 17, 2012 at 10:50 AM 1
Share

0xffffffff - which is 32 bits set all set to 1.

avatar image Paulius-Liekis · Nov 17, 2012 at 04:44 PM 2
Share

No. Just: Camera.culling$$anonymous$$ask = 0xffffffff; $$anonymous$$an you really need to lear bitwise logic and understand how binary math. Understand numbers with base 16 is useful too ;)

avatar image PixelFireXY · Nov 14, 2016 at 04:17 PM 1
Share

If someone don't know how to convert uint to int (Camera.culling$$anonymous$$ask = 0xffffffff;) just use unchecked keyword: http://stackoverflow.com/questions/1131843/how-do-i-convert-uint-to-int-in-c

Show more comments
avatar image
40

Answer by roberto_sc · Nov 23, 2012 at 05:50 PM

This way you don't need backup values of old masks:

 // Turn on the bit using an OR operation:
 private void Show() {
     camera.cullingMask |= 1 << LayerMask.NameToLayer("SomeLayer");
 }
 
 // Turn off the bit using an AND operation with the complement of the shifted int:
 private void Hide() {
     camera.cullingMask &=  ~(1 << LayerMask.NameToLayer("SomeLayer"));
 }
 
 // Toggle the bit using a XOR operation:
 private void Toggle() {
     camera.cullingMask ^= 1 << LayerMask.NameToLayer("SomeLayer");
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mcroswell · Nov 24, 2012 at 09:27 PM 1
Share

Roberto: Thanks for showing some "real" CS here! Good technique! -m

Listed for others convenience: ~ BIT-NOT (010111 becomes 101000) & BIT-AND (011 & 001 becomes 001) | BIT-OR (011 | 001 becomes 011) ^ EXCLUSIVE-OR (011 ^ 001 becomes 010)

avatar image joaoVictorCardoso · Dec 20, 2017 at 07:16 PM 0
Share

Thanks dude! very thanks for encapsulating this code it's very good, God bless you!

avatar image
16

Answer by Eric5h5 · Nov 24, 2012 at 09:33 PM

Simple visual way:

 var playerMask : LayerMask;    // select desired layer in inspector
 var everythingMask : LayerMask; // select "Everything" in inspector
 ..
 camera.cullingMask = playerMask;
 ..
 camera.cullingMask = everythingMask;
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image laurachen · Aug 29, 2016 at 04:54 PM 0
Share

This works great, thanks for sharing!

avatar image
6

Answer by asterix14 · Dec 22, 2015 at 05:23 PM

An easy way to set the culling mask to "Everything" is:

camera.cullingMask = -1; // -1 means "Everything"
-1 is converted to a bit mask having all bits set to 1 (which means all layers).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by GLeBaTi · Oct 01, 2021 at 12:23 PM

 Camera.main.cullingMask = LayerMask.GetMask("UI", "Other");
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Attaching an RTS scrolling script to a camera (Noob Question) 3 Answers

camera.layerCullDistances not working on gameobject with combinechildren 1 Answer

Shadows from culled objects 0 Answers

How to make camera position relative to a specific target. 1 Answer

Camera is culling terrain detail objects regardless of settings 6 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges