Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
1
Question by hfiani · Aug 20, 2017 at 02:27 PM · sorting layers

Add SortingLayer by script

hello i need to create dynamically sorting layers by code (C#). then i can get its ID and give to a sprite.

i need to do this since i have no idea what is the exact number of the sprites, and each sprite HAS to have its own sorting layer

PS: not a normal Layer, but the Sorting Layer

i tried:

 SortingLayer sl = new SortingLayer ();
 sl.name = "something";
 sl.id = 12;

but obviously this does not work. any help?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by gnumaru · Jul 05, 2018 at 10:52 AM

@hfiani

Something like this should work (editor only)

 SerializedObject tagsAndLayersManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
 SerializedProperty sortingLayersProp = tagsAndLayersManager.FindProperty("m_SortingLayers");
 sortingLayersProp.InsertArrayElementAtIndex(sortingLayersProp.arraySize);
 var newlayer = sortingLayersProp.GetArrayElementAtIndex(sortingLayersProp.arraySize);
 newlayer.FindPropertyRelative("uniqueID").intValue = UnityEngine.Random.Range(int.MinValue, int.MaxValue);
 newlayer.FindPropertyRelative("name").stringValue = "my new layer";
 tagsAndLayersManager.ApplyModifiedProperties();

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 kala476 · May 06, 2019 at 03:02 PM 0
Share

I wrote code based on what you posted and it works perfectly! Thank you! However, I would like to save my changes (right now they are jumping back to what it was before after I exit play mode).

avatar image gregoryb kala476 · May 29, 2020 at 10:25 PM 0
Share

This method is meant only for use at editor time, editing projectsettings in play mode is not possible, and will always revert back once play mode is exited.

avatar image
0

Answer by hfiani · Aug 20, 2017 at 03:25 PM

no one knows the answer to this question? i searched a lot but found nothing

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 gregoryb · May 29, 2020 at 10:31 PM 0
Share

What you are trying to do is not possible exactly as described because sorting layers are part of projectsettings and not modifiable at runtime. What you are allowed to do however is change the sortingOrder dynamically which will do the same thing.
I assume you are re-sorting SpriteRenderer components at runtime to move objects visually in render order. That can be achieved easily like so: SpriteRenderer spriteRenderer = someObject.GetComponent(); spriteRenderer.sortingOrder = 10;

avatar image
0

Answer by Nadogeto · Jan 17 at 03:39 PM

https://stackoverflow.com/questions/49270205/how-to-add-sorting-layer-via-script/49270639#49270639?newreg=b5385c3543c84dc7be686c6cfc194564

here's an appropriate and functional answer, if anyone is still out there searching

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

71 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 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 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

Complicated Question about Arrays and Duplicate Sorting Orders! 1 Answer

Shader vs Sprites - Sorting layer (ZWrite Off not working) 1 Answer

object always appears in front of another 3 Answers

Rendering objects by their y position with a script 0 Answers

Split-Screen without Showing Other Player 1 Answer


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