Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Apply shader after editor compiled

Discussion in 'Scripting' started by lmaoroot2, Oct 19, 2021.

  1. lmaoroot2

    lmaoroot2

    Joined:
    May 6, 2021
    Posts:
    17
    Hello,

    I need to make a script that waits until EditorApplication.isCompiling and EditorApplication.isUpdating are both false, so everything is refreshed and ready to go, and then applys a shader. I BELIEVE what I am supposed to use is a coroutine with an IEnumerable, but I wouldnt know how to go about this. I'm fairly new to c# in the regard of coroutines. If someone can point me in the right direction, and/or solve my problem of applying a shader to a material RIGHT AFTER importing the shader, then that would be great. The problem is that since the shader is "new" to the project, it takes a while for unityeditor to realize theres a shader, so it fails to find the shader using Shader.Find and therefore doesnt do anything.
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Yeah, using a coroutine for that would probably work. What do you have so far?

    Here's some pseudocode of what I imagine it'd look like for starters.

    Code (CSharp):
    1. IEnumerator WaitForShader()
    2. {
    3.     while (EditorApplication.isCompiling || EditorApplication.isUpdating)
    4.         yield return new WaitForSeconds(1);
    5.    
    6.     // do your thing here
    7. }
     
  3. lmaoroot2

    lmaoroot2

    Joined:
    May 6, 2021
    Posts:
    17

    I don't really know where to go from there to be honest.
    This is basically what I have so far:
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. public class TestClass : MonoBehaviour
    6. {
    7. private static IEnumerator coroutine;
    8. [MenuItem("applyShader"), false, 1]
    9. private static void applyShader(){
    10.    Material temp = (Material)AssetDatabase.LoadAssetAtPath(someMaterial, typeof(Material));
    11.    coroutine = WaitForShader(temp);
    12.    StartCoroutine(coroutine);[/INDENT]
    13. }
    14. private static IEnumerator WaitForShader(Material temp)
    15.     {
    16.         while (EditorApplication.isCompiling || EditorApplication.isUpdating)
    17.             yield return new WaitForSeconds(1);
    18.         temp.shader = Shader.Find("test/newshader");
    19.     }[/INDENT]
    20. }
    Does this look okay to you? The problem I'm getting is that it's telling me that I cannot have a static IEnumerator or something. I'm not sure how else to accomplish it.
     
    Last edited: Oct 19, 2021
  4. lmaoroot2

    lmaoroot2

    Joined:
    May 6, 2021
    Posts:
    17
    Well, unfortunately this doesn't work, so if I can get some help, that'd be wonderful <3
     
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,996
  6. lmaoroot2

    lmaoroot2

    Joined:
    May 6, 2021
    Posts:
    17
    That's quite a bit old, yeah. However, this listens for the compiling and does something while it is compiling, but i need to apply a shader to a material after EditorApplication.isCompiling() and isUpdating() are both false... as well as I need to run this type of coroutine in a static method, so im not exactly sure how to go about this.
     
  7. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,920
    Well, coroutines need additional package in the editor.

    On the other hand you can try this callback:
    https://docs.unity3d.com/ScriptReference/Compilation.CompilationPipeline-compilationFinished.html