Hello Guest

Author Topic: Unity 4.3 sorting layers  (Read 21207 times)

habitoti

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 82
    • View Profile
    • Rombos Homepage
Unity 4.3 sorting layers
« on: November 16, 2013, 07:51:08 am »
Hi,

one useful thing about the new sorting layers in 4.3 is the ability to lock them to prevent accidental selections and movements of e.g. background elements. However, Toolkit 2D seems not to consider that. If sprites are assigned to a locked layer, I can still select and move or size them. It would be great if the editor could consider the locked setting!

Another thing I noticed is that I can't assign a sprite to the default layer (when there are more than that one). Not sure whether this is a constraint already pf Unity or maybe just a bug in tk2d ;-)

Regards, habitoti
Checkout our homepage or visit the Rombos blog.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Unity 4.3 sorting layers
« Reply #1 on: November 16, 2013, 11:23:48 am »
How do you lock a layer?

I'm not sure if this is possible to implement, most of the new features in 4.3 are pretty much hardcoded. Eg. the sorting layer selection itself is simply hardcoded in the sprite editor code... I'll look into it after I figure out how you lock it !

habitoti

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 82
    • View Profile
    • Rombos Homepage
Re: Unity 4.3 sorting layers
« Reply #2 on: November 16, 2013, 09:23:32 pm »
You can lock a layer by clicking the lock symbol in the layer dropdown:



Unity sprites can't be selected any longer when assigned to such a locked layer. I would hope that there is some API to retrieve details about the sorting layers, at least whether it's locked or not. Then you could just ignore those sprites during selection whose renderers are assigned to that layer.

BTW: beyond the mentioned issue that I can't assign a tk2d-sprite to the "Default" layer (which would mean to set the renderers sortingLayerName property to null, but clicking on "Default" switches to the first entry only and does not stick to "Default"), it seems that the setting for the t2kdSprite layer assignment is not stable. Once I change the sorting layer setup (adding or deleting sorting layers), the layer assignment for the existing sprites change even if the initially set layer still exists.

Regards, habitoti
« Last Edit: November 16, 2013, 09:42:01 pm by habitoti »
Checkout our homepage or visit the Rombos blog.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Unity 4.3 sorting layers
« Reply #3 on: November 16, 2013, 09:43:28 pm »
I highly doubt there'll be an API for this, there isn't one to grab the sorting layer names either - we get that using reflection. Might be able to get what is required here... let me investigate.

wtg

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Unity 4.3 sorting layers
« Reply #4 on: December 01, 2013, 04:27:57 am »
I just discover this issue myself. I've been wanting Unity to implement this feature for quite some time and now that it's here, I'm kinda upset that I can't use it at work during production. Being able to lock objects from being selected is a handy tool that's speeds up production. Hopefully this will get added in the next update. The Unity Community will love you for it.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Unity 4.3 sorting layers
« Reply #5 on: December 01, 2013, 12:11:42 pm »
It is possible using reflection. I might be able to get it in the next update.

coshea

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: Unity 4.3 sorting layers
« Reply #6 on: December 17, 2013, 12:02:53 pm »
+1 the ability to honor the locked sprite layers in Unity 4.3 would be great

AbsurdInteractive

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Unity 4.3 sorting layers
« Reply #7 on: January 31, 2014, 11:36:43 pm »
BTW: beyond the mentioned issue that I can't assign a tk2d-sprite to the "Default" layer (which would mean to set the renderers sortingLayerName property to null, but clicking on "Default" switches to the first entry only and does not stick to "Default"), it seems that the setting for the t2kdSprite layer assignment is not stable. Once I change the sorting layer setup (adding or deleting sorting layers), the layer assignment for the existing sprites change even if the initially set layer still exists.

Was this issue fixed? This is causing an issue with our main character and other sprites, as we need the main character on the Default Sorting Layer?

AbsurdInteractive

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Unity 4.3 sorting layers
« Reply #8 on: February 01, 2014, 12:09:52 am »
It seems the issue is that in Unity, when SortingLayerName is set to Default, instead of the SortingLayerName string on the renderer being set to the direct string "Default", it is set to the string "", which must mean somewhere Unity is wiping the string when we do this. So I modified tk2dEditorUtility, which seems to work for now. Here's what I did.

Code: [Select]
public static string SortingLayerNamePopup( string label, string value ) {
string[] names = GetSortingLayerNames();

               //BEGIN MY MODIFIED CODE
               //If value is not in the array set it to Default
int hasValueInArray = Array.IndexOf(names, value);
if(hasValueInArray == -1)
{
value = "Default";
}
                //END MY MODIFIED CODE

if (names.Length == 0) {
return EditorGUILayout.TextField(label, value);
}
else {
int sel = 0;
for (int i = 0; i < names.Length; ++i) {
if (names[i] == value) {
sel = i;
break;
}
}
sel = EditorGUILayout.Popup(label, sel, names);
return names[sel];
}
}

It's a bit hacky, but it seems to do the trick until a better solution is made.
« Last Edit: February 01, 2014, 12:12:04 am by AbsurdInteractive »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Unity 4.3 sorting layers
« Reply #9 on: February 01, 2014, 01:59:36 pm »
This whole thing is quite nasty as it uses reflection to get into closed parts of Unity. This hack should work - I'll wait until the next unity release hopefully they'll expose it properly by then.

TekuStudios

  • 2D Toolkit
  • Full Member
  • *
  • Posts: 177
    • View Profile
    • Teku Studios
Re: Unity 4.3 sorting layers
« Reply #10 on: March 26, 2014, 11:44:33 am »
Did you finally get this working, unikron? The issue is still there in 2.4
We are an Indie videogame developer located in Teruel, Spain. A small 6-people team which is currently working on the upcoming 'Candle'.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Unity 4.3 sorting layers
« Reply #11 on: March 26, 2014, 12:33:23 pm »
You mean respecting the lock? That isn't possible yet, Unity don't expose the necessary bits.

TekuStudios

  • 2D Toolkit
  • Full Member
  • *
  • Posts: 177
    • View Profile
    • Teku Studios
Re: Unity 4.3 sorting layers
« Reply #12 on: March 26, 2014, 06:17:25 pm »
Ok, I misunderstood the previous pots. My fault.
We are an Indie videogame developer located in Teruel, Spain. A small 6-people team which is currently working on the upcoming 'Candle'.