Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
2
Question by andre_mariano · Sep 01, 2015 at 04:28 AM · c#layerseditorwindowlayermask

Is it possible to access Layer visibility and lock status via script?

I'm making a level editor and when an option is on in my EditorWindow script, I can select multiple gameobjects and add them to a collection.

I want to be able to select just the gameobjects in a particular layer, so when this option is on the other layers should be locked, but I'm unable to find any way to access layer properties from code.

Is it possible to do change layer's lock and visibility from my script? Is there some clever way to do that?

Thanks!

Comment
Add comment · Show 2
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 Mikilo · Sep 01, 2015 at 07:09 AM 0
Share

What is a layer lock?

avatar image andre_mariano · Sep 01, 2015 at 01:59 PM 1
Share

@mikilo the layer property to lock it and prevent the objects in it from being selected.

alt text

unity-layer-props.png (6.7 kB)

1 Reply

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

Answer by Mikilo · Sep 01, 2015 at 02:13 PM

Hahahaha!

I had an issue where I was unable to select objects in scene... You just gave me the explanation... XD

Anyway, you can toggle layer's lock state by calling :

 InternalEditorUtility.SetSortingLayerLocked(index, state);

Since it is hidden, you need to call it through reflection. Will you need help?

Edit: With some further research, you might change it through Tools.visibleLayers and Tools.lockedLayers.

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 andre_mariano · Sep 02, 2015 at 04:18 AM 0
Share

Hey @$$anonymous$$ikilo, thanks for your help! I got this to work, but I'm not sure it's the best way yet. I was able to call the SetSortingLayerLocked() function through reflection (and btw I had never used reflecion before, but encountered no difficulties in the process) but I supose this function is intended to work just with sorting layers and not with layer masks, which is my case. I ended up using the Tools.lockedLayers even though it has an odd behavior. It gets and sets an int number based on 2 to the power of the layer's index on the layers array. So the code turned out quite simple:

 Tools.lockedLayers = (int)$$anonymous$$athf.Pow(2, Layer$$anonymous$$ask.NameToLayer("LayerName"));

I have no idea why that work this way, do you have any clue? And thanks again for your help!

P.S. Glad to know that I accidentally gave you an explanation about that issue. Haha!

avatar image Mikilo · Sep 02, 2015 at 04:39 AM 5
Share

Hi!

Oh no! Tools.lockedLayers is a mask (32 bits in 4 bytes in an integer).

Why is that? Because layers in Unity are used as a mask.

Also, about your code. Since you dont really know mask, I give you a tip about how to play with them.

Tools.lockedLayers = 1 << Layer$$anonymous$$ask.NameToLayer("LayerName"); // Replace the whole value of lockedLayers. Tools.lockedLayers |= 1 << Layer$$anonymous$$ask.NameToLayer("LayerName"); // Add a value to lockedLayers. Tools.lockedLayers &= ~(1 << Layer$$anonymous$$ask.NameToLayer("LayerName")); // Remove a value from lockedLayers. Tools.lockedLayers ^= 1 << Layer$$anonymous$$ask.NameToLayer("LayerName")); // Toggle a value in lockedLayers.

End of the story.

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

29 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't change an object Layers at runtime via c# code (Help Please) 0 Answers

Raycast hitting layer explicitly told to ignore (and doesn't even have collider!) 1 Answer

help to fix bug with using GUIUtility.RotateAroundPivot + GUI.DrawTexture 1 Answer

Assets/MFPS/Scripts/Network/bl_chatRoom.cs(78,14): error CS0019: Operator `!=' cannot be applied to operands of type `PhotonMessageInfo' and `null' 1 Answer

Raycasting keeps getting blocked by objects on a layer it should be ignoring,Raycast blocked by layer it shouldnt be blocked by 0 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