static Texture2D LoadTextureInternal(string imagePath, bool doMipMaps) { Texture2D resultTexture = null; IntPtr texture = Texture2D.blackTexture.GetNativeTexturePtr(); resultTexture = Texture2D.CreateExternalTexture(4, 4, TextureFormat.RGBA32, doMipMaps, false, texture); using (AssetStreaming streamTexture = new AssetStreaming(imagePath)) { WWW wwwTexture = streamTexture.GetWWW(); if (wwwTexture != null) { wwwTexture.LoadImageIntoTexture(resultTexture); } } return(resultTexture); }
static Texture2D LoadTextureInternalAsync(string imagePath, bool doMipMaps, int preloadWidth = 4, int preloadHeight = 4) { Texture2D resultTexture = null; IntPtr texture = UnityEngine.GameObject.Instantiate <Texture2D>(Texture2D.blackTexture).GetNativeTexturePtr(); if (texture != IntPtr.Zero) { lock (loadingTextures) { resultTexture = Texture2D.CreateExternalTexture(preloadWidth, preloadHeight, TextureFormat.RGBA32, doMipMaps, false, texture); loadingTextures.Add(texture, resultTexture); AssetStreaming streamTexture = new AssetStreaming(imagePath); streamTexture.GetWWWAsync((WWW wwwTexture) => { if (wwwTexture != null) { Texture2D result = null; if (loadingTextures.TryGetValue(texture, out result)) { loadingTextures.Remove(texture); if (result) { wwwTexture.LoadImageIntoTexture(result); } } wwwTexture.Dispose(); } else { loadingTextures.Remove(texture); } streamTexture.Unload(); }); } } return(resultTexture); }