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?