- Home /
override the draw function
Hello,
I'm trying to override the function that unity calls for drawing GameObject's to the screen.
I want this so I can change values in the shader just before the object is drawn.
Right now I have this:
void Update () {
renderer.sharedMaterial.SetVector("center", new Vector4(transform.position.x, transform.position.y, 0, 0));
}
But since Update() is called for all GameObject's before any are drawn, only the last updated GameObject has the correct value for "center"
Any help would be appreciated,
Answer by robertbu · Mar 09, 2014 at 06:34 PM
To get things to draw correctly, use 'material' rather than the 'sharedMaterial'.
renderer.material.SetVector("center", new Vector4(transform.position.x, transform.position.y, 0, 0));
I assume you are using 'sharedMaterial' to avoid drawcalls. If so, even if you could someone figure out how to do what you ask, the results would be the same. That is, you will need to change the value before each object is drawn, so the objects could no longer be batched together and sent to the GPU (and I'm unaware of any way to make your approach work in Unity).
Thanks I just started with Unity and I understand the "renderer.material" / "renderer.shared$$anonymous$$aterial" features better now. :)
Your answer

Follow this Question
Related Questions
Cost of loading manually modified instances at runtime, on mobiles? 1 Answer
Why is the System.Drawing namespace not supported in Unity 3.0 Beta? 1 Answer
What is the best way to create a polygon shape in Unity? 3 Answers
Don't delete discrete lines in Vectrosity 2 Answers
White outline around tree leaves after upgrade to Unity 5 1 Answer