Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Unity not picking up my external shader changes

Discussion in 'Editor & General Support' started by ScottieBizzle, Jul 10, 2015.

  1. ScottieBizzle

    ScottieBizzle

    Joined:
    Nov 9, 2013
    Posts:
    17
    Is there any way to get unity to pick up my external shader changes without having to click on unity every time?

    I'm developing a node-based shader authoring tool which works along side Unity but the second unity is not the active window, it goes into some idle state and doesn't pick up any external file changes. I'm able to find the Unity editor HWND using EnumWindowsProc in my C++ application and call SwitchToThisWindow(..) to activate Unity and then sleep for 100 ms then call it again to switch back to my application but it only works randomly. And when it does pick up my changes, updates the shader settings in the inspector but sometimes doesn't render.

    Is there a special windows message or custom message I can send to the Unity window or app to prompt it to check for external changes? I tried sending a bunch of WM messages which also don't seem to do much.

    Thanks!
     
  2. jpease

    jpease

    Joined:
    Apr 18, 2013
    Posts:
    11
    You could put an editor script like this in your project:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [InitializeOnLoad]
    5. class BackgroundRefresh : AssetPostprocessor
    6. {
    7.    static BackgroundRefresh() {
    8.      EditorApplication.update += Update;
    9.    }
    10.  
    11.    const float refreshInterval = 0.5f; // in seconds
    12.    static double nextRefreshTime;
    13.  
    14.    static void Update() {
    15.      double time = EditorApplication.timeSinceStartup;
    16.      if(time > nextRefreshTime) {
    17.        AssetDatabase.Refresh();
    18.        nextRefreshTime = time + refreshInterval;
    19.      }
    20.    }
    21.  
    22.    static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) {
    23.      UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
    24.    }
    25. }
    If you want to make it more strict about when and what it refreshes (i.e. so it only imports shaders and only when your tool changes them), you could write (in your tool) to a specific file that you check in Update, or use some other way of passing a message to this script code, and call ImportAsset on the shader asset instead of Refresh.
     
  3. ScottieBizzle

    ScottieBizzle

    Joined:
    Nov 9, 2013
    Posts:
    17
    You are the man! Works great :)

    Great idea about prompting unity to only pick up my changes, will give it a shot.
     
unityunity