I have a parent object contain several child objects with the componet scripts attached.
Those component scripts execute in the child objects scene sequence of order.
I need to sort (z-sort) that order at runtime basing on the current child distances from the camera.
There are at least 2 questions here. About the implicit GUI z-order one, I think it is mostly already answered on the comments: just have a OnGUI
manager which will call your own DrawGUI
on each component.
Now, for executing child objects in script based on distance from camera, you could do something like this (unverified code):
using System.Collections.Generic;
class whatever : MonoBehaviour {
public Transform parentOfCameraChildrenObjects;
private List cameraChildrenObjects = new List();
void Start ()
{
int i = -1;
Vector3 cameraPos = Camera.main.transform.position;
cameraChildrenObjects.AddRange(
parentOfCameraChildrenObjects.GetComponentsInChildren()
);
cameraChildrenObjects.Sort( (x,y) =>
Vector3.Distance(x.transform.position, cameraPos) >
Vector3.Distance(y.transform.position, cameraPos)
);
}
void Update ()
{
foreach (Transform child in cameraChildrenObjects)
{
child.SendMessage("MyMethod", "my string as parameter");
// or
child.GetComponent().MyMethod("my string as parameter");
}
}
}