My fix ensures that Panel position Z is not taken into account when building widgets geometry.
What I do is:
1. Store Panel localPosition
2. Zero Panel localPosition.z
3. worldToLocal = cachedTransform.worldToLocalMatrix;
4. Restore Panel localPosition from 1.
This way I make sure that only Panel z position is taken into account when determining which panel should be drawn first as it is closer to the Camera and not widgets' geometry(that is currently based on Panel worldToLocal matrix hence also on Panel z position).
According [1] It is Panel's Z position that determines which Panel should be drawn first. But right now this is not always true as widgets's geometry is based on Panel Z position.
Example of the problem:
1. Panel1 has Z localPosition = -1
2. Panel2 has Z localPosition = 0
3. Panel1 widgets mesh is generated based on Panel1 worldToLocal matrix, therefor it is moved in z direction also by unknown value(I noticed that the value changes).
4. Panel2 widgets mesh in generated but not moved as Panel2 z is 0;
5. Set Panel1 Z localPosition = -0.5
6. Set Panel2 Z localPosition = -0.75
You would expect Panel2 to be drawn before Panel1(because it is close to camera), but this is not the case as Panel1 mesh is internally moved in z so Panel1 is before Panel2.
With the fix Panel2 will always be drawn before Panel1 as Panel's z position will not be taken into account when building widgets mesh.
[1]
http://www.tasharen.com/forum/index.php?topic=1858.0