private static Texture2D GetTextureFromBMP(OP2BitmapFile bitmap) { int width = Mathf.Abs(bitmap.imageHeader.width); int height = Mathf.Abs(bitmap.imageHeader.height); // Get pixels from bitmap Color32[] imageData = new Color32[width * height]; for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { OP2UtilityDotNet.Bitmap.Color color = bitmap.GetEnginePixel(x, height - 1 - y); imageData[x + y * width] = new Color32(color.red, color.green, color.blue, color.alpha); } } // Convert to texture Texture2D texture = new Texture2D(width, height, TextureFormat.RGBA32, true); texture.SetPixels32(imageData); texture.wrapMode = TextureWrapMode.Clamp; //texture.filterMode = FilterMode.Point; texture.Apply(); return(texture); }
private static IEnumerator LoadAssetsRoutine(string[] modBundlePaths, OnInitializeCallback onInitCB) { // Load mod bundles foreach (string bundlePath in modBundlePaths) { AssetBundleCreateRequest bundleRequest = AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, bundlePath)); yield return(bundleRequest); if (bundleRequest.assetBundle != null) { _ModBundles.Add(bundleRequest.assetBundle); } } // Prepare legacy art loader try { _LegacyArt = new OP2BmpLoader("OP2_ART.BMP", _ArtFile); } catch (System.Exception ex) { Debug.LogWarning(ex); } yield return(null); float startTime = Time.realtimeSinceStartup; float curTime = startTime; // Load textures for (int i = 0; i < _ArtFile.imageMetas.Count; ++i) { Texture2D texture = GetAssetFromModBundle <Texture2D>("Bitmap" + i); if (texture != null) { _TextureLookup[i] = texture; _SpriteLookup[i] = Sprite.Create(texture, new UnityEngine.Rect(0, 0, texture.width, texture.height), Vector2.zero, 1); } else if (_LegacyArt != null) { // Load legacy texture try { OP2BitmapFile bitmap = _LegacyArt.GetImage(i); texture = GetTextureFromBMP(bitmap); _TextureLookup[i] = texture; _SpriteLookup[i] = Sprite.Create(texture, new UnityEngine.Rect(0, 0, texture.width, texture.height), Vector2.zero, 1); } catch (System.Exception ex) { Debug.LogException(ex); Debug.LogError("BmpIndex: " + i); } } else { // Could not find texture! Debug.LogError("Could not find texture for index: " + i); // Inform caller of failure onInitCB?.Invoke(false); yield break; } // Every X seconds, take a break to render the screen if (curTime + 0.25f < Time.realtimeSinceStartup) { yield return(null); curTime = Time.realtimeSinceStartup; } } Debug.Log("Texture Load Time: " + (Time.realtimeSinceStartup - startTime).ToString("N2") + " seconds"); yield return(LoadSoundsRoutine("sound.vol")); yield return(LoadSoundsRoutine("voices.vol")); Debug.Log("Total Load Time: " + (Time.realtimeSinceStartup - startTime).ToString("N2") + " seconds"); // Inform caller of success onInitCB?.Invoke(true); }