public virtual int ZOrderedObjectSorter(ZOrderedObject o1, ZOrderedObject o2) { int output = 0; if (o1.layerIndex > o2.layerIndex || o1.index > o2.index) { output = 1; } else if (o2.layerIndex > o1.layerIndex || o2.index > o1.index) { output = -1; } return(output); }
public virtual void OnValidate() { ZOrderedObject zOrderedObject; Renderer renderer; foreach (GameObject obj in addToZOrderedObjects) { renderer = obj.GetComponentInChildren <Renderer>(); zOrderedObject = new ZOrderedObject(); zOrderedObject.objectName = obj.name; zOrderedObject.order = renderer.sortingOrder; zOrderedObject.layerName = renderer.sortingLayerName; zOrderedObject.layerIndex = renderer.sortingLayerID; zOrderedObject.renderer = renderer; zOrderedObject.index = int.MaxValue; renderers.Add(renderer); renderers.Sort((a, b) => RendererSorter(a, b)); zOrderedObjects.Insert(renderers.IndexOf(renderer), zOrderedObject); } addToZOrderedObjects = new GameObject[0]; int previousLayerIndex = int.MinValue; int previousOrder = MIN_Z_ORDER; for (int i = 0; i < zOrderedObjects.Count; i++) { zOrderedObject = zOrderedObjects[i]; if (zOrderedObject.layerIndex > previousLayerIndex) { previousLayerIndex = zOrderedObject.layerIndex; previousOrder = MIN_Z_ORDER; } else { previousOrder++; } zOrderedObject.index = i; zOrderedObject.order = previousOrder; zOrderedObjects[i] = zOrderedObject; } zOrderedObjects.Sort((a, b) => ZOrderedObjectSorter(a, b)); }