Automatically ship shaders on build

This commit is contained in:
Nahuel Rocchetti 2024-06-21 11:04:56 -03:00
parent 5cfa4de5c6
commit 4c4a365faf
2 changed files with 50 additions and 0 deletions

View file

@ -0,0 +1,39 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEditor.Rendering;
using UnityEngine;
using UnityEngine.Rendering;
public class BuildPreProcessor : IPreprocessBuildWithReport
{
public int callbackOrder => 0;
public void OnPreprocessBuild(BuildReport report)
{
var allshaders = Directory.GetFiles("Assets/Shaders", "*.shader", SearchOption.AllDirectories);
var graphicsSettings = AssetDatabase.LoadAssetAtPath<GraphicsSettings>("ProjectSettings/GraphicsSettings.asset");
var serializedObject = new SerializedObject(graphicsSettings);
var alwaysIncludedShadersProp = serializedObject.FindProperty("m_AlwaysIncludedShaders");
foreach(var shaderPath in allshaders)
{
var shader = AssetDatabase.LoadAssetAtPath<Shader>(shaderPath);
if (shader == null) continue;
for(var i=0;i<alwaysIncludedShadersProp.arraySize;i++)
{
var elem = alwaysIncludedShadersProp.GetArrayElementAtIndex(i);
if (elem.objectReferenceValue == shader)
continue;
}
alwaysIncludedShadersProp.InsertArrayElementAtIndex(0);
var arrayElem = alwaysIncludedShadersProp.GetArrayElementAtIndex(0);
arrayElem.objectReferenceValue = shader;
}
serializedObject.ApplyModifiedProperties();
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f4caf04b0595c5c4f8e91b22f501b249
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: