Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to know if I have the LWRP / HDRP API at compile time?

Discussion in 'Graphics Experimental Previews' started by stephero, Aug 22, 2018.

  1. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    123
    Hi guys,

    I am looking for a way to detect that I have the LWRP or the HDRP API in my project. A simple preprocessor macro would be enough for me.

    Simple use-case: I want to write a function which change the background color of a camera, and I would like to be able to use this function in a project with or without the HDRP API.

    With the legacy RP the code is:
    camera.backgroundColor = Color.red;


    When using HDRP the code requires the usage of a new API located in the namepace UnityEngine.Experimental.Rendering.HDPipeline:
    var cameraData = camera.GetComponent<UnityEngine.Experimental.Rendering.HDPipeline.HDAdditionalCameraData>();
    cameraData.backgroundColorHDR = Color.red;


    But I can't obviously just run this code in a project on which the HDRP API doesn't exist, since the namespace UnityEngine.Experimental.Rendering.HDPipeline won't exist then.

    So I am looking for something like a magic macro that would allow me to write something like that:
    #ifdef UNITY_USE_HDRP
    var cameraData = camera.GetComponent<UnityEngine.Experimental.Rendering.HDPipeline.HDAdditionalCameraData>();
    cameraData.backgroundColorHDR = Color.red;
    #else
    camera.backgroundColor = Color.red;
    #endif


    Do you know how I could handle that?
    Thanks
     
    FlightOfOne, ModLunar and tetto_green like this.
  2. tetto_green

    tetto_green

    Joined:
    Dec 22, 2015
    Posts:
    35
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
  4. tetto_green

    tetto_green

    Joined:
    Dec 22, 2015
    Posts:
    35
  5. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    We really need some better way to do this... I want to be able to code for both pipelines but I'll just get errors if one render pipeline package or another is not installed (specially with the using statements). Having code that works for both would mean installing both packages even though you'll end up using only one for a particular project...
     
  6. Raul_T

    Raul_T

    Joined:
    Jan 10, 2015
    Posts:
    363
    We currently do this for our asset store products and it worked with no issues for our customers for the past 4-5 months
    Code (CSharp):
    1. using UnityEngine.Rendering;
    2.  
    3. namespace MadGoat.Core.Utils {
    4.     public static class RenderPipelineUtils {
    5.  
    6.         public enum PipelineType {
    7.             Unsupported,
    8.             BuiltInPipeline,
    9.             UniversalPipeline,
    10.             HDPipeline
    11.         }
    12.  
    13.         /// <summary>
    14.         /// Returns the type of renderpipeline that is currently running
    15.         /// </summary>
    16.         /// <returns></returns>
    17.         public static PipelineType DetectPipeline() {
    18. #if UNITY_2019_1_OR_NEWER
    19.             if (GraphicsSettings.renderPipelineAsset != null) {
    20.                 // SRP
    21.                 var srpType = GraphicsSettings.renderPipelineAsset.GetType().ToString();
    22.                 if (srpType.Contains("HDRenderPipelineAsset")) {
    23.                     return PipelineType.HDPipeline;
    24.                 }
    25.                 else if (srpType.Contains("UniversalRenderPipelineAsset") || srpType.Contains("LightweightRenderPipelineAsset")) {
    26.                     return PipelineType.UniversalPipeline;
    27.                 }
    28.                 else return PipelineType.Unsupported;
    29.             }
    30. #elif UNITY_2017_1_OR_NEWER
    31.             if (GraphicsSettings.renderPipelineAsset != null) {
    32.                 // SRP not supported before 2019
    33.                 return PipelineType.Unsupported;
    34.             }
    35. #endif
    36.             // no SRP
    37.             return PipelineType.BuiltInPipeline;
    38.         }
    39.     }
    40. }
    Then we use the result from this to add our own define symbols for compilation (i.e. SSAA_HDRP or SSAA_URP). For API on this you should look into
    Code (CSharp):
    1. PlayerSettings.GetScriptingDefineSymbolsForGroup(targetPlatform);
    2. PlayerSettings.SetScriptingDefineSymbolsForGroup(targetPlatform, defines);
    As a side note, our code only takes SRPs into account on Unity2019+ since we don't support SRPs pre 2019.

    P.S. A define symbol that would get added automatically with the SRP package similar to Post Process Stack v2 which always adds UNITY_POST_PROCESSING_STACK_V2 for example would be a good option, but I think it would cause issues when a project has a scriptable renderer installed but not used in the graphics settings, so in the end you still need to check which SRP is set in the graphics settings.
     
    Last edited: Mar 5, 2020
    FlightOfOne, ModLunar, sabint and 3 others like this.