Hi
Building a vehicle configurator and need to find a way to easily change colors without rebuilding or reorganizing the existing model.
So what I need to do is
On changing colors
-recursively search all children of a game object, searching for any game objects with “color1”
-swap these materials with another material, say “color2”
How would I do this in code, and is there a limit to this technique?
ie if too many nested children will there be a noticeable delay before ALL children change color?
Rather than recursively search, attach a MonoBehaviour to each object whose color you’d like to change. The script itself can just use GetComponent().material to modify its material - or if you want to change all materials within its children in the hierarchy, GetComponentsInChildren. (Make sure you cache the result of your GetComponent(sInChildren) call, as that will likely be the slowest part of this.)
If these objects are scattered through the scene, your next challenge is to get all of them to change the color on demand. I see 3 ways to do this:
- FindObjectsOfType(), then loop through the resulting array, calling its ChangeColorFunction;
- Maintain a static array in your MaterialChangingScript code, and loop through that. Just like #1, but faster and cleaner, but slightly harder to implement.
- Use delegates and events to send out a wide-ranging call, and the components simply answer this call by registering themselves with the event. Probably the best solution.
public class ColorChangingScript : MonoBehaviour {
public delegate void ChangeMaterialDelegate(Material old, Material newMat);
public static event ChangeMaterialDelegate OnChangeMaterial;
void OnEnable() {
OnChangeMaterial += ChangeMaterial;
}
void OnDisable() {
OnChangeMaterial -= ChangeMaterial;
}
public void ChangeMaterial(Material old, Material newMat) {
//do stuff
}
}
// anywhere else
ColorChangingScript.OnChangeMaterial(someMaterial, someOtherMaterial);
When you call that last line, ALL your ColorChangingScripts will execute that function, which can check for matching materials and change them if so. If you wanted to optimize this further, you could even make a Dictionary<Material, ChangeMaterialDelegate> so that each type of material has its own event! I don’t think this is gonna be a major performance hog, though, so I wouldn’t worry about that.
Thanks, appreciated!!
So multiple MonoBehaviors is considered better than a single MonoBehavior with a recursive search? Just wonder if there is a tipping point when you might have a hundred or more MonoBehaviors all listening to the same event, or if that is strictly a memory issue.
Already am using an Event manager class so will go with 3rd option.
MonoBehaviours are pretty lightweight overall. (Just delete any empty Start() and Update() function, as those cause inefficiencies and overhead) That said, if all the materials you’re looking to modify are under the same root object, there’s no need to have multiple MonoBehaviours either.
Thanks but one point of confusion. If not multiple Monobehaviors doesn’t that mean some type of recursive search!
GetComponentsInChildren is a recursive search.