0
\$\begingroup\$

The title might not be very clear, so I'll give an example.

Let's say I have two types of buttons in my game's UI : buttons of the category A and buttons of the category B.

They are both very similar, and have the same "Normal Color", "Highlighted Color", etc. The only difference is that they have a different "Pressed Color". For example, A buttons are red when pressed and B buttons are blue.

Now I could create two different prefabs as models for A buttons and B buttons, and call it a day, but I might end up adding some more categories of buttons later (C, D, etc.). And each one has a different "Pressed Color". And if I decide to change the "Normal Color" for each button, I would have to change it in each prefab.

So is there a way to do some kind of... prefab inheritance I guess? For example, each category prefab would inherit from a more high level prefab that would define the "Normal Color", "Highlighted Color", etc.

If yes, how?

\$\endgroup\$
  • \$\begingroup\$ You may try creating ScriptableObjects(SO) that contain a color. Make SOs for Normal, Highlighted and many versions of Pressed. Then your prefabs use the color from those scriptable objects assets/instances. That way by changing the SO asset for Normal you will effectively change all the buttons that use Normal. Im on my phone now and can't give you details but you may assign SO values on Start/Awake, or use editor code, or if I remember correctly even inherit from the button component and change it to use SOs. \$\endgroup\$ – Nikaas Mar 19 '18 at 7:55
1
\$\begingroup\$

Not really, no.

The best solution I can think of is a script that will modify the values to the desired ones, and then that script can be subclassed to have different values.

This could probably be done as an editor script and update the prefabs, but I'm not sure.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.