private void Execute() { List <DepthSortedObject> depthSortedObjects = new List <DepthSortedObject>(); foreach (DepthSorted ds in GameObject.FindObjectsOfType <DepthSorted>()) { depthSortedObjects.Add(new DepthSortedObject(ds, ds.transform)); } float zDepth = 0f; depthSortedObjects.Sort(); for (int i = 0; i < depthSortedObjects.Count; i++) { DepthSortedObject dso = depthSortedObjects[i]; dso.Transform.position = new Vector3(dso.Transform.position.x, dso.Transform.position.y, zDepth); zDepth += zDepthIncrement; zDepth = this.SortChildren(dso.Transform, zDepth); } }
// greater-than signifies that this object exists behind the other public int CompareTo(object obj) { if (obj == null) { return(1); } DepthSortedObject other = obj as DepthSortedObject; if (other != null) { if ((int)(this.DepthSorted.SortingLayer) > (int)(other.DepthSorted.SortingLayer)) { return(1); } else if ((int)(this.DepthSorted.SortingLayer) < (int)(other.DepthSorted.SortingLayer)) { return(-1); } else { if (this.Transform.position.y > other.Transform.position.y) { return(1); } else if (this.Transform.position.y == other.Transform.position.y) { return(0); } else { return(-1); } } } else { throw new ArgumentException("Object is not a DepthSortedObject"); } }