JJBB/Assets/Editor/Scripts/ReplaceMobileShaders.cs

61 lines
2.4 KiB
C#
Raw Normal View History

2024-08-23 15:49:34 +08:00
using System;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class ReplaceMobileShaders
{
private static ShaderReplace[] _shaderReplace;
[MenuItem("ResourceTool/Replace Mobile Shaders")]
public static void ReplaceMobile()
{
if (_shaderReplace == null)
{
_shaderReplace = new []
{
new ShaderReplace("Mobile/Particles/Additive", "Particles/Additive", true),
new ShaderReplace("Mobile/Particles/Additive Culled", "Particles/Additive", false),
new ShaderReplace("Mobile/Particles/Additive Alpha", "Particles/Alpha Blended", false),
new ShaderReplace("Mobile/Particles/Alpha Blended", "Particles/Alpha Blended", true),
};
}
var allMaterials = from assetPath in AssetDatabase.GetAllAssetPaths()
let extension = Path.GetExtension(assetPath)
where !string.IsNullOrEmpty(extension) && extension.Equals(".mat", StringComparison.OrdinalIgnoreCase)
select assetPath;
foreach (var assetPath in allMaterials)
{
var material = AssetDatabase.LoadAssetAtPath<Material>(assetPath);
var shaderName = material.shader == null ? string.Empty : material.shader.name;
for(var i = 0; i < _shaderReplace.Length; i++)
if (shaderName.Equals(_shaderReplace[i].originName, StringComparison.OrdinalIgnoreCase))
{
const string tintColorName = "_TintColor";
var sourceColor = Color.white;
var replace = _shaderReplace[i];
if (replace.halfColor)
sourceColor = material.HasProperty(tintColorName) ? material.GetColor(tintColorName) : Color.white;
material.shader = replace.shader;
if (replace.halfColor)
material.SetColor(tintColorName, sourceColor * 0.5f);
EditorUtility.SetDirty(material);
}
}
}
private class ShaderReplace
{
public readonly string originName;
public readonly Shader shader;
public readonly bool halfColor;
public ShaderReplace(string origin, string target, bool useHalf)
{
originName = origin;
shader = Shader.Find(target);
halfColor = useHalf;
}
}
}