Skip to content

Loading Custom Sprites and Custom Audio

Alex Neargarder edited this page Jun 17, 2024 · 5 revisions

Loading Custom Sprites and Custom Audio

If you are looking to replace the sprites for your bro's Main Sprite, Gun Sprite, Special Icon Sprite, Avatar Sprite, or Cutscene Sprite, this can all be done with a JSON file, and will be a lot easier than doing it via programming. However if you need to load additional sprites, for custom projectiles or if you need to perhaps replace your main sprite with a different one when you activate an ability or something, similar to what Brominator does for his special, then it is also possible to load new sprites via programming.

In order to do this, I would recommend you use BroMakerLib's ResourcesController.cs

ResourcesController.cs

This class which is builtin to BroMakerLib has several useful methods for loading sprites and AudioClips.


The main methods I would recommend that you use for loading custom sprites are

public static Material GetMaterial(string filePath);
public static Material GetMaterial(string path, string fileName);

These methods will use the default shader that most Broforce objects use, and they will also store the material in a cache so that they doesn't have to reload it from the file every time your bro is respawned. So if you use these methods, you don't have to worry about adding in your own caching system, you can just call them and have the results automatically cached.


The main methods I would recommend that you use for loading AudioClips are

public static AudioClip GetAudioClip(string filePath);
public static AudioClip GetAudioClip(string path, string fileName);

These methods also cache the results similar to the previous ones. The only caveat to watch out for is that if you expect that these AudioClips may be played several times simultaneously, for example if you have multiple people all using the same custom bro, then if the same AudioClip is played more than once, it will glitch out. If this is something you expect might happen then you should use the

public static AudioClip CreateAudioClip(string filePath);
public static AudioClip CreateAudioClip(string path, string fileName);

methods instead, which will avoid using the cache and create unique AudioClips each time, which will avoid that issue.


If you need to create a sprite which has transparent pixels, you should make sure to change the layer of the GameObject to which your sprite is attached to a layer that supports transparency. I've found that layers 5, 19, and 28 work. Layers 5 and 28 make the object appear in front of certain objects like grass and the railings on bridges, which objects normally don't appear in front of. Layer 19 makes the object appear behind these objects, so it's probably the most useful.


Changing Sprites of Objects

In order to change the sprite of a Broforce Object you'll generally need to change the material of the MeshRenderer. This should look something like this:

base.GetComponent<Renderer>().material = nameOfYourCustomMaterialVariable;

I would definitely avoid changing the sharedMaterial variable, because this can often affect other unrelated objects.