3
\$\begingroup\$

Most of my assets use the Unity standard shader. It looks and runs fine on Desktop, but on mobile the performance isn't good.

I assume the solution is to switch to one of the mobile friendly vertex-lit shaders for mobile. But how can I switch shaders depending on platform? Is there a way that Unity can do this for you, or do I need to manually change them before building? Also, would I be able to just specify the shader, or would I need to create all new materials for all my assets?

I really have no idea how to approach this, and I'd appreciate it if I can be pointed in the right direction.

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

It may lead to longer load times, but you could switch shaders in Awake based on the platform:

// Your Assets folder should contain a folder called Resources,
// containing a shader called mobileShader
Shader mobileShader = Resources.Load("mobileShader") as Shader;

// Called when a GameObject with this script is loaded
void Awake() 
{
    // Check if we are on mobile
    if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
    {
        // Change just the shader
        GetComponent<Renderer>().material.shader = mobileShader;
    }
}
\$\endgroup\$
2
\$\begingroup\$

You can also use shader compile directives in custom shaders to skip over expensive sections, resulting in a simpler look, which only triggers when on a mobile device.

https://docs.unity3d.com/Manual/SL-MultipleProgramVariants.html

I've got a shader I wrote which uses that feature that wraps around a visual effect, the effect can then be disabled in the in-game options menu (and being purely visual, although cool, the game play isn't changed).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .