- Home /
Manage the script execution order, how?
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.
Don't use Update on the children, then, and ins$$anonymous$$d implement your own manager on the parent that calculates priority and calls the necessary methods on the children?
Not exactly. I want to controll the execution sequence of the onGui function. I am writing a script to controll the gui depth, but I was just wondering if that could be somehow done by sorting the script component
I don't believe you can sort the components at runtime.
And control the scripts execution order on siblings? If so the depth method is the only option in my case.
An alternative to UnityGUI would be to use actual 3D objects (=regular scene objects) for your interface. With a billboard class, you can ensure any planes or Text$$anonymous$$eshes are aligned to the camera and are perceived as 2D elements, but any sorting and visibility/occlusion (especially with other scene objects) will be handled by the normal rendering process.
As long as you don't need specialized, complex GUI functions, this is usually the best approach.
Answer by cregox · Jun 11, 2013 at 05:45 PM
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");
}
}
}
Your answer
Follow this Question
Related Questions
How to check execution order from script? 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Destroyed object is still registering a trigger event 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer