Search Unity

Different materials per target platform

Discussion in 'Editor & General Support' started by kl3in3rhack3r, Sep 11, 2020.

  1. kl3in3rhack3r

    kl3in3rhack3r

    Joined:
    Nov 22, 2018
    Posts:
    15
    Hi,

    there is a way to provide different materials for the same model for each target platform?
    Background is: I want to use URP Lit Shader but on Android Phones it is too much so I want use only in this case a Simple Lit Shader. But I always use the same scene with the same prefabs / models inside.
    Additional it would be better if a material have default settings (shader selection + config) and you can here create an override for an other target platform (override selected shader + config). So all known and important things are saved on the same position.

    Thanks.
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You could use the platform pre-processor directives to determine the platform that the game is running on and assign a material to an object based on that.
    Example:
    Code (CSharp):
    1. public class PlatformMaterialAssigner : MonoBehaviour
    2. {
    3.    public Material materialForPC;
    4.    public Material materialForMobile;
    5.  
    6.    public Material GetMaterialForPlatform()
    7.    {
    8. #if UNITY_STANDALONE
    9.       return materialForPC;
    10. #elif UNITY_ANDROID || UNITY_IOS
    11.       return materialForMobile;
    12. #else
    13.       return null;
    14. #endif
    15.    }
    16. }
    See: https://docs.unity3d.com/Manual/PlatformDependentCompilation.html