using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
[InitializeOnLoad]
public class RenderingPipelineDefines
{
enum PipelineType
{
Unsupported,
BuiltInPipeline,
UniversalPipeline,
HDPipeline
}
static RenderingPipelineDefines()
{
UpdateDefines();
}
///
/// Update the unity pipeline defines for URP
///
static void UpdateDefines()
{
var pipeline = GetPipeline();
if (pipeline == PipelineType.UniversalPipeline)
{
AddDefine("ZED_URP");
}
else
{
RemoveDefine("ZED_URP");
}
if (pipeline == PipelineType.HDPipeline)
{
AddDefine("ZED_HDRP");
}
else
{
RemoveDefine("ZED_HDRP");
}
}
///
/// Returns the type of renderpipeline that is currently running
///
///
static PipelineType GetPipeline()
{
#if UNITY_2019_1_OR_NEWER
if (GraphicsSettings.renderPipelineAsset != null)
{
// SRP
var srpType = GraphicsSettings.renderPipelineAsset.GetType().ToString();
if (srpType.Contains("HDRenderPipelineAsset"))
{
return PipelineType.HDPipeline;
}
else if (srpType.Contains("UniversalRenderPipelineAsset"))
{
return PipelineType.UniversalPipeline;
}
else return PipelineType.Unsupported;
}
#elif UNITY_2017_1_OR_NEWER
if (GraphicsSettings.renderPipelineAsset != null) {
// SRP not supported before 2019
return PipelineType.Unsupported;
}
#endif
// no SRP
return PipelineType.BuiltInPipeline;
}
///
/// Add a custom define
///
///
///
public static void AddDefine(string define)
{
var definesList = GetDefines();
if (!definesList.Contains(define))
{
definesList.Add(define);
SetDefines(definesList);
}
}
///
/// Remove a custom define
///
///
///
public static void RemoveDefine(string define)
{
var definesList = GetDefines();
if (definesList.Contains(define))
{
definesList.Remove(define);
SetDefines(definesList);
}
}
public static List GetDefines()
{
var target = EditorUserBuildSettings.activeBuildTarget;
var buildTargetGroup = BuildPipeline.GetBuildTargetGroup(target);
var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(buildTargetGroup);
return defines.Split(';').ToList();
}
public static void SetDefines(List definesList)
{
var target = EditorUserBuildSettings.activeBuildTarget;
var buildTargetGroup = BuildPipeline.GetBuildTargetGroup(target);
var defines = string.Join(";", definesList.ToArray());
PlayerSettings.SetScriptingDefineSymbolsForGroup(buildTargetGroup, defines);
}
}