Optimal sorting of transforms in editor hierarchy

Example situation: I have 5 UI images in a canvas. Each UI image is paired with and displayed overhead of a different game object. I want closer game objects to have their image rendered on top (i.e., indexed last in the editor hierarchy).

Current solution: I have a script attached to the parent of the editor hierarchy that maintains a List<> of all of its children. On update(), it performs System.Collections.Generic.Sort() with a custom comparator to sort the list with farthest objects first. It then loops through the list and does SetSiblingIndex(i) for each element.

My current solution works, but I’m concerned with the overhead of calling a sort function every frame. Since I do have to (unavoidably) calculate the camera distance to each game object every frame, I’d like to somehow use that value directly in the editor. E.g., if I was using Canvas’ instead of Images, I could just set Canvas.sortingOrder equal to the distance and let Unity take care of the rest. Is there an alternative solution that would behave similar to this?

I prefer to use alpha sorting.

Find it much easier to find what im looking for. If I want something to be at the top, call it _example

Is there a way to enable alpha sorting for just one canvas in particular? I googled around and it doesn’t seem this script will achieve that.

Not that im aware of, but why does it matter?

In the final game, there isnt a hierarchy.

tbh, seems like a pointless goal to me.

Well I’ve just grown accustomed to the manual hierarchy sorting in laying out my projects. Plus it does matter when determining the sort order of other canvases.

I’m not overly concerned though. I’m content with my manual sorting implementation but would love to hear if anyone has any other better ideas. I guess Unity is performing its own sort function behind the scenes anyway (in the case using sorting priority integers).