- Home /
How to create a custom Renderer component?
I have created a custom component MyRenderer
, that behaves just like a Renderer
, but Is derived from MonoBehaviour
.
I have another component that has a member with a Renderer
reference and may change certain properties of the renderer, at runtime. I would like for this member to be able to accept MyRenderer
components, as well as standard unity Renderers
(both programmatically, and using the standard ObjectField
function in the editor).
Unfortunately, if I derive MyRenderer
from the Renderer
class, rather than MonoBehaviour
, Unity prevents me from adding it as a component to any game objects. Instead, I get a popup telling me the object must be derived from Monobehavior.
Q: How can I create a custom class, that I can both add to GameObjects as a component, AND reference as a Renderer (AND have the ObjectField controls show them when the specified ObjectField type is Renderer
)?
Other thoughts: I’ve looked up the decompiled code for MeshRenderer, a valid component, and it appears to simply be derived from Renderer. Why can this class, which is derived from Renderer, be added as a component, but not my custom class, also derived from Renderer? I've managed to get kinda of working by deriving MyRender from MonoBehavior and using interfaces, and adapter patterns; but it's ugly, less-efficient, and still doesn't work with ObjectFields.
Answer by steven3Dim · Nov 30, 2017 at 08:18 AM
I also tried to derive a class from Renderer, but I cannot add it as a component. This is probably closed code from unity and they have hard-coded it so that Renderer can be a component. See also this thread: https://forum.unity.com/threads/custom-renderer-how-to-do-that.145807/
Your answer
Follow this Question
Related Questions
Changing two different objects renderer colour 1 Answer
Skills system - class design 0 Answers
Manually add vertext to TrailRenderer? 1 Answer
Camera can see an Object 1 Answer
Problem with open world games 3 Answers