From f7f7535a7c4cefd74077b2e536e026e8ea90290d Mon Sep 17 00:00:00 2001 From: "Simon (darkside) Jackson" Date: Fri, 23 Oct 2020 19:10:51 +0100 Subject: [PATCH] Initial check-in of a static library to collate shaders on first use. --- .../Effects/ShaderEffects/UIAdditiveEffect.cs | 2 +- .../Effects/ShaderEffects/UIImageCrop.cs | 2 +- .../ShaderEffects/UILinearDodgeEffect.cs | 2 +- .../Effects/ShaderEffects/UIMultiplyEffect.cs | 2 +- .../Effects/ShaderEffects/UIScreenEffect.cs | 2 +- .../ShaderEffects/UISoftAdditiveEffect.cs | 2 +- Runtime/Scripts/Effects/SoftMaskScript.cs | 4 +-- Runtime/Scripts/Effects/UIParticleSystem.cs | 2 +- Runtime/Scripts/Utilities/ShaderLibrary.cs | 29 +++++++++++++++++++ .../Scripts/Utilities/ShaderLibrary.cs.meta | 11 +++++++ 10 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 Runtime/Scripts/Utilities/ShaderLibrary.cs create mode 100644 Runtime/Scripts/Utilities/ShaderLibrary.cs.meta diff --git a/Runtime/Scripts/Effects/ShaderEffects/UIAdditiveEffect.cs b/Runtime/Scripts/Effects/ShaderEffects/UIAdditiveEffect.cs index 6b90e47..f8bf812 100644 --- a/Runtime/Scripts/Effects/ShaderEffects/UIAdditiveEffect.cs +++ b/Runtime/Scripts/Effects/ShaderEffects/UIAdditiveEffect.cs @@ -25,7 +25,7 @@ namespace UnityEngine.UI.Extensions if (mGraphic.material == null || mGraphic.material.name == "Default UI Material") { //Applying default material with UI Image Crop shader - mGraphic.material = new Material(Shader.Find("UI Extensions/UIAdditive")); + mGraphic.material = new Material(ShaderLibrary.GetShaderInstance("UI Extensions/UIAdditive")); } } else diff --git a/Runtime/Scripts/Effects/ShaderEffects/UIImageCrop.cs b/Runtime/Scripts/Effects/ShaderEffects/UIImageCrop.cs index 46c4065..aca20c8 100644 --- a/Runtime/Scripts/Effects/ShaderEffects/UIImageCrop.cs +++ b/Runtime/Scripts/Effects/ShaderEffects/UIImageCrop.cs @@ -32,7 +32,7 @@ namespace UnityEngine.UI.Extensions if (mGraphic.material == null || mGraphic.material.name == "Default UI Material") { //Applying default material with UI Image Crop shader - mGraphic.material = new Material(Shader.Find("UI Extensions/UI Image Crop")); + mGraphic.material = new Material(ShaderLibrary.GetShaderInstance("UI Extensions/UI Image Crop")); } mat = mGraphic.material; } diff --git a/Runtime/Scripts/Effects/ShaderEffects/UILinearDodgeEffect.cs b/Runtime/Scripts/Effects/ShaderEffects/UILinearDodgeEffect.cs index f09934d..4d883b4 100644 --- a/Runtime/Scripts/Effects/ShaderEffects/UILinearDodgeEffect.cs +++ b/Runtime/Scripts/Effects/ShaderEffects/UILinearDodgeEffect.cs @@ -25,7 +25,7 @@ namespace UnityEngine.UI.Extensions if (mGraphic.material == null || mGraphic.material.name == "Default UI Material") { //Applying default material with UI Image Crop shader - mGraphic.material = new Material(Shader.Find("UI Extensions/UILinearDodge")); + mGraphic.material = new Material(ShaderLibrary.GetShaderInstance("UI Extensions/UILinearDodge")); } } else diff --git a/Runtime/Scripts/Effects/ShaderEffects/UIMultiplyEffect.cs b/Runtime/Scripts/Effects/ShaderEffects/UIMultiplyEffect.cs index e3fa701..00206a2 100644 --- a/Runtime/Scripts/Effects/ShaderEffects/UIMultiplyEffect.cs +++ b/Runtime/Scripts/Effects/ShaderEffects/UIMultiplyEffect.cs @@ -25,7 +25,7 @@ namespace UnityEngine.UI.Extensions if (mGraphic.material == null || mGraphic.material.name == "Default UI Material") { //Applying default material with UI Image Crop shader - mGraphic.material = new Material(Shader.Find("UI Extensions/UIMultiply")); + mGraphic.material = new Material(ShaderLibrary.GetShaderInstance("UI Extensions/UIMultiply")); } } else diff --git a/Runtime/Scripts/Effects/ShaderEffects/UIScreenEffect.cs b/Runtime/Scripts/Effects/ShaderEffects/UIScreenEffect.cs index ba5e2bc..386a3fb 100644 --- a/Runtime/Scripts/Effects/ShaderEffects/UIScreenEffect.cs +++ b/Runtime/Scripts/Effects/ShaderEffects/UIScreenEffect.cs @@ -25,7 +25,7 @@ namespace UnityEngine.UI.Extensions if (mGraphic.material == null || mGraphic.material.name == "Default UI Material") { //Applying default material with UI Image Crop shader - mGraphic.material = new Material(Shader.Find("UI Extensions/UIScreen")); + mGraphic.material = new Material(ShaderLibrary.GetShaderInstance("UI Extensions/UIScreen")); } } else diff --git a/Runtime/Scripts/Effects/ShaderEffects/UISoftAdditiveEffect.cs b/Runtime/Scripts/Effects/ShaderEffects/UISoftAdditiveEffect.cs index 6d80b95..764d0b9 100644 --- a/Runtime/Scripts/Effects/ShaderEffects/UISoftAdditiveEffect.cs +++ b/Runtime/Scripts/Effects/ShaderEffects/UISoftAdditiveEffect.cs @@ -25,7 +25,7 @@ namespace UnityEngine.UI.Extensions if (mGraphic.material == null || mGraphic.material.name == "Default UI Material") { //Applying default material with UI Image Crop shader - mGraphic.material = new Material(Shader.Find("UI Extensions/UISoftAdditive")); + mGraphic.material = new Material(ShaderLibrary.GetShaderInstance("UI Extensions/UISoftAdditive")); } } else diff --git a/Runtime/Scripts/Effects/SoftMaskScript.cs b/Runtime/Scripts/Effects/SoftMaskScript.cs index 999c35c..b5edda9 100644 --- a/Runtime/Scripts/Effects/SoftMaskScript.cs +++ b/Runtime/Scripts/Effects/SoftMaskScript.cs @@ -48,7 +48,7 @@ namespace UnityEngine.UI.Extensions var text = GetComponent(); if (text != null) { - mat = new Material(Shader.Find("UI Extensions/SoftMaskShader")); + mat = new Material(ShaderLibrary.GetShaderInstance("UI Extensions/SoftMaskShader")); text.material = mat; cachedCanvas = text.canvas; cachedCanvasTransform = cachedCanvas.transform; @@ -64,7 +64,7 @@ namespace UnityEngine.UI.Extensions var graphic = GetComponent(); if (graphic != null) { - mat = new Material(Shader.Find("UI Extensions/SoftMaskShader")); + mat = new Material(ShaderLibrary.GetShaderInstance("UI Extensions/SoftMaskShader")); graphic.material = mat; cachedCanvas = graphic.canvas; cachedCanvasTransform = cachedCanvas.transform; diff --git a/Runtime/Scripts/Effects/UIParticleSystem.cs b/Runtime/Scripts/Effects/UIParticleSystem.cs index 6bd1897..3266a1d 100644 --- a/Runtime/Scripts/Effects/UIParticleSystem.cs +++ b/Runtime/Scripts/Effects/UIParticleSystem.cs @@ -76,7 +76,7 @@ namespace UnityEngine.UI.Extensions if (material == null) { - var foundShader = Shader.Find("UI Extensions/Particles/Additive"); + var foundShader = ShaderLibrary.GetShaderInstance("UI Extensions/Particles/Additive"); if (foundShader) { material = new Material(foundShader); diff --git a/Runtime/Scripts/Utilities/ShaderLibrary.cs b/Runtime/Scripts/Utilities/ShaderLibrary.cs new file mode 100644 index 0000000..292c179 --- /dev/null +++ b/Runtime/Scripts/Utilities/ShaderLibrary.cs @@ -0,0 +1,29 @@ +/// Credit SimonDarksideJ + +using System.Collections.Generic; +using UnityEditor; + +namespace UnityEngine.UI.Extensions +{ + [InitializeOnLoadAttribute] + public static class ShaderLibrary + { + public static Dictionary shaderInstances = new Dictionary(); + public static Shader[] preLoadedShaders; + + public static Shader GetShaderInstance(string shaderName) + { + if (shaderInstances.ContainsKey(shaderName)) + { + return shaderInstances[shaderName]; + } + + var newInstance = Shader.Find(shaderName); + if (newInstance != null) + { + shaderInstances.Add(shaderName, newInstance); + } + return newInstance; + } + } +} diff --git a/Runtime/Scripts/Utilities/ShaderLibrary.cs.meta b/Runtime/Scripts/Utilities/ShaderLibrary.cs.meta new file mode 100644 index 0000000..4a6e91f --- /dev/null +++ b/Runtime/Scripts/Utilities/ShaderLibrary.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3d4dc4c08600582419712a4f2370c81b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: