Ejemplo n.º 1
0
 void bundleLoaded(KSPAssets.Loaders.AssetLoader.Loader loader)
 {
     for (int i = 0; i < loader.definitions.Length; ++i)
     {
         UnityEngine.Object obj = loader.objects[i];
         if (obj == null)
         {
             continue;
         }
         Shader s = obj as Shader;
         Debug.Log(s);
         if (s != null)
         {
             wingShader = s;
             return;
         }
     }
 }
        private void ShadersLoaded(KSPAssets.Loaders.AssetLoader.Loader loader) // thanks moarDV - https://github.com/Mihara/RasterPropMonitor/blob/5c9fa8b259dd391892fe121724519413ccbb6b59/RasterPropMonitor/Core/UtilityFunctions.cs
        {
            log.debug(string.Format("ShadersLoaded"));
            string aShaderName = string.Empty;

            for (int i = 0; i < loader.objects.Length; ++i)
            {
                UnityEngine.Object o = loader.objects[i];
                if (o != null && o is Shader)
                {
                    // We'll remember the name of whichever shader we were
                    // able to load.
                    aShaderName = o.name;
                    break;
                }
            }

            if (string.IsNullOrEmpty(aShaderName))
            {
                log.warning(string.Format("Unable to find a named shader \"{0}\".", aShaderName));
                return;
            }

            var loadedBundles = KSPAssets.Loaders.AssetLoader.LoadedBundles;

            if (loadedBundles == null)
            {
                log.warning(string.Format("Unable to find any loaded bundles in AssetLoader."));
                return;
            }

            // Iterate over all loadedBundles.  Experimentally, my bundle was
            // the only one in the array, but I expect that to change as other
            // mods use asset bundles (maybe none of the mods I have load this
            // early).
            for (int i = 0; i < loadedBundles.Count; ++i)
            {
                Shader[] shaders        = null;
                Font[]   fonts          = null;
                bool     theRightBundle = false;

                try
                {
                    // Try to get a list of all the shaders in the bundle.
                    shaders = loadedBundles[i].LoadAllAssets <Shader>();
                    if (shaders != null)
                    {
                        // Look through all the shaders to see if our named
                        // shader is one of them.  If so, we assume this is
                        // the bundle we want.
                        for (int shaderIdx = 0; shaderIdx < shaders.Length; ++shaderIdx)
                        {
                            if (shaders[shaderIdx].name == aShaderName)
                            {
                                theRightBundle = true;
                                break;
                            }
                        }
                    }
                    fonts = loadedBundles[i].LoadAllAssets <Font>();
                }
                catch { }

                if (theRightBundle)
                {
                    // If we found our bundle, set up our parsedShaders
                    // dictionary and bail - our mission is complete.
                    for (int j = 0; j < shaders.Length; ++j)
                    {
                        if (!shaders[j].isSupported)
                        {
                            log.debug(string.Format("Shader {0} - unsupported in this configuration", shaders[j].name));
                        }
                        loadedShaders[shaders[j].name] = shaders[j];
                    }
                    for (int j = 0; j < fonts.Length; ++j)
                    {
                        log.debug(string.Format("Adding KSP-Bundle-included font {0} / {1}", fonts[j].name, fonts[j].fontSize));
                        loadedFonts[fonts[j].name] = fonts[j];
                    }
                    return;
                }
            }

            log.error(string.Format("Failed to load shaders  - how did this callback execute?"));
        }