Created
March 17, 2017 09:52
-
-
Save dimmduh/5641c66df766f8bddd02d92f2c56a2a4 to your computer and use it in GitHub Desktop.
automatic replace shader in unity from pc to mobile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEditor; | |
public class ShaderReplacer : MonoBehaviour { | |
private static readonly Dictionary<string, Shader> replaceTo = new Dictionary<string, Shader>() | |
{ | |
{"Legacy Shaders/Diffuse", Shader.Find("Mobile/Diffuse") }, | |
{"Legacy Shaders/Diffuse Detail", Shader.Find("Mobile/Diffuse") }, | |
{"Particles/Additive", Shader.Find("Mobile/Particles/Additive") }, | |
{"Particles/Multiply", Shader.Find("Mobile/Particles/Multiply") }, | |
{"Particles/Alpha Blended", Shader.Find("Mobile/Particles/Alpha Blended") }, | |
{"Legacy Shaders/Bumped Specular", Shader.Find("Mobile/Bumped Diffuse") }, | |
{"Legacy Shaders/Transparent/Bumped Specular", Shader.Find("Mobile/Bumped Diffuse") }, | |
}; | |
[MenuItem("IOGames/Replace Shader To Mobile")] | |
public static void ReplaceShader2Mobile() | |
{ | |
var materials = GetAllMaterials(); | |
var notReplace = GetNotReplaceShaderNames(); | |
foreach (var material in materials) | |
{ | |
var shaderName = material.Value.shader.name; | |
Shader shaderReplaceTo = null; | |
//со стандартным разберемся вручную | |
if (shaderName.Equals("Standard") || shaderName.Equals("Standard (Specular setup)")) | |
{ | |
if (material.Value.GetTexture("_BumpMap") == null || material.Value.GetTexture("_MainTex") == null) | |
{ | |
shaderReplaceTo = Shader.Find("Mobile/Diffuse"); | |
} | |
else | |
{ | |
shaderReplaceTo = Shader.Find("Mobile/Bumped Diffuse"); | |
} | |
} | |
if (replaceTo.ContainsKey(shaderName)) | |
{ | |
shaderReplaceTo = replaceTo[shaderName]; | |
} | |
if (shaderReplaceTo != null) | |
{ | |
Undo.RecordObject(material.Value, "Replace shader"); | |
material.Value.shader = shaderReplaceTo; | |
} | |
else | |
{ | |
if (!notReplace.Contains(shaderName)) | |
Debug.Log("Can not replace: " + shaderName); | |
} | |
} | |
} | |
//из тех, на что надо заменить получаем список, того, какие шейдеры не заменять | |
private static List<string> GetNotReplaceShaderNames() | |
{ | |
return replaceTo.Select(shader => shader.Value.name).ToList(); | |
} | |
//получаем все материалы на сцене | |
private static IDictionary<int, Material> GetAllMaterials() | |
{ | |
var _matList = new Dictionary<int, Material>(); | |
var renderers = Resources.FindObjectsOfTypeAll<Renderer>(); | |
foreach (var renderer in renderers) | |
{ | |
foreach (var sharedMaterial in renderer.sharedMaterials) | |
{ | |
if (sharedMaterial == null) | |
continue; | |
var matId = sharedMaterial.GetInstanceID(); | |
if (!_matList.ContainsKey(matId)) | |
_matList.Add(matId, sharedMaterial); | |
} | |
} | |
Debug.Log(_matList.Count); | |
return _matList; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment