Last active
September 1, 2019 06:50
-
-
Save peeweek/be699789568a0198c19598d5ee7f9871 to your computer and use it in GitHub Desktop.
Unity Compile All Materials
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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
static class LoadAllMaterials | |
{ | |
static readonly string title = "Load All Materials"; | |
static GameObject obj; | |
static MeshRenderer sphere_renderer; | |
static Camera cam; | |
static IEnumerator coroutine; | |
[MenuItem("Edit/Render Pipeline/Load All Materials")] | |
static void LoadAll() | |
{ | |
obj = GameObject.CreatePrimitive(PrimitiveType.Sphere); | |
sphere_renderer = obj.GetComponent<MeshRenderer>(); | |
cam = SceneView.lastActiveSceneView.camera; | |
obj.transform.position = cam.transform.position + cam.transform.forward * 1.2f; | |
coroutine = LoadCoroutine(); | |
EditorApplication.update += Load; | |
} | |
static void Load() | |
{ | |
coroutine.MoveNext(); | |
} | |
static List<Material> cachedMaterials; | |
static IEnumerator LoadCoroutine() | |
{ | |
EditorUtility.DisplayProgressBar(title, "", 0.0f); | |
cachedMaterials = new List<Material>(); | |
var allRenderers = Object.FindObjectsOfType<Renderer>(); | |
int i = 0; | |
int count = allRenderers.Length - 1; | |
foreach (var renderer in allRenderers) | |
{ | |
int j = 0; | |
foreach (var material in renderer.sharedMaterials) | |
{ | |
if (material == null || cachedMaterials.Contains(material)) | |
continue; | |
float t = (float)i / count; | |
if(EditorUtility.DisplayCancelableProgressBar(title, renderer.gameObject.name + " : " + material.name + " (" + j+ "/" +renderer.sharedMaterials.Length+")", t)) | |
{ | |
EditorUtility.ClearProgressBar(); | |
Object.DestroyImmediate(obj); | |
EditorApplication.update -= Load; | |
yield break; | |
} | |
sphere_renderer.sharedMaterial = material; | |
cachedMaterials.Add(material); | |
j++; | |
yield return null; | |
} | |
i++; | |
} | |
EditorUtility.ClearProgressBar(); | |
Object.DestroyImmediate(obj); | |
EditorApplication.update -= Load; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment