Last active
September 5, 2023 10:11
-
-
Save allfake/c7eef9efd4259ba3efa10fed025eda98 to your computer and use it in GitHub Desktop.
When use assetbundle in edtor -> materials become pink. This script help to reapply shader and particle. This helpful when develop. Do not use this on production.
This file contains hidden or 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; | |
public class ReApplyShaders : MonoBehaviour | |
{ | |
public Renderer[] renderers; | |
public Material[] materials; | |
public string[] shaders; | |
void Awake() | |
{ | |
renderers = GetComponentsInChildren<Renderer>(true); | |
} | |
void Start() | |
{ | |
foreach(var rend in renderers) | |
{ | |
materials = rend.sharedMaterials; | |
shaders = new string[materials.Length]; | |
if (rend.GetComponent<ParticleSystem>() != null) | |
{ | |
Material tempMaterial = rend.sharedMaterial; | |
Shader shader = Shader.Find(rend.sharedMaterial.shader.name); | |
if (shader) | |
{ | |
rend.sharedMaterial.shader = shader; | |
rend.sharedMaterial.renderQueue = tempMaterial.renderQueue; | |
} | |
continue; | |
} | |
for( int i = 0; i < materials.Length; i++) | |
{ | |
shaders[i] = materials[i].shader.name; | |
} | |
for( int i = 0; i < materials.Length; i++) | |
{ | |
Material tempMaterial = materials[i]; | |
materials[i].shader = Shader.Find(shaders[i]); | |
rend.sharedMaterial.renderQueue = tempMaterial.renderQueue; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
GameObject obj = Instantiate(somePrefab);
if (Application.isEditor)
{
obj.AddComponent<ReApplyShaders>();
}