2021-05-04
May the fourth be with you
So I exported a SGB project to Unity and needed to change all the shaders on the exported materials. This is a tedious task to do by hand especially when there are many materials --- but fortunately, Unity makes Editor Scripting easy and you can perform batch operations on assets easily.
I learned about the AssetDatabase and how you can use it to find and manipulate assets. I slapped together a scriptable-wizard that takes two shader values and runs through all materials in the project and swaps the first with the second. This lets me target shaders that I know can be replaced properly.
using UnityEngine;
using UnityEditor;
using System.Collections;
public class Wizard : ScriptableWizard
{
public Shader target;
public Shader replacement;
[MenuItem("Batch Ops/Wizard")]
public static void SetWizard()
{
ScriptableWizard.DisplayWizard("Some Batch Op",typeof(Wizard),"Swap Shaders");
}
void OnWizardCreate()
{
string[] guids = AssetDatabase.FindAssets("t:material");
foreach (string guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
Material material = (Material) AssetDatabase.LoadAssetAtPath(path, typeof(Material));
if (material.shader == target)
{
material.shader = replacement;
Debug.Log("Replaced shader in " + material.name);
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
The following wizard shows up when I access it from the top menu.
Fortunately, the script ran with no issues and I was able to avoid tedious shader replacement. I'm sure it's prone to errors but I'll deal with those as they happen.
Top comments (0)