public static void AddAsset(AssetStoreAsset searchResult, Texture2D placeholderPreviewImage) { if (placeholderPreviewImage != null) { searchResult.previewImage = ScaleImage(placeholderPreviewImage, 256, 256); } searchResult.previewInfo = null; searchResult.previewBundleRequest = null; // Dynamic previews is asset bundles to be displayed in // the inspector. Static previews are images. if (!string.IsNullOrEmpty(searchResult.dynamicPreviewURL) && searchResult.previewBundle == null) { // Debug.Log("dyn url " + searchResult.disposed.ToString() + " " + searchResult.dynamicPreviewURL); searchResult.disposed = false; // searchResult.previewBundle = AssetBundle.CreateFromFile("/users/jonasd/test.unity3d"); // searchResult.previewAsset = searchResult.previewBundle.mainAsset; // Request the asset bundle data from the url and register a callback AsyncHTTPClient client = new AsyncHTTPClient(searchResult.dynamicPreviewURL); client.doneCallback = delegate(IAsyncHTTPClient c) { if (!client.IsSuccess()) { System.Console.WriteLine("Error downloading dynamic preview: " + client.text); // Try the static preview instead searchResult.dynamicPreviewURL = null; DownloadStaticPreview(searchResult); return; } // We only suppport one asset so grab the first one AssetStoreAsset sel = GetFirstAsset(); // Make sure that the selection hasn't changed meanwhile if (searchResult.disposed || sel == null || searchResult.id != sel.id) { //Debug.Log("dyn disposed " + searchResult.disposed.ToString() + " " + (sel == null ? "null" : sel.id.ToString()) + " " + searchResult.id.ToString()); return; } // Go create the asset bundle in memory from the binary blob asynchronously try { AssetBundleCreateRequest cr = AssetBundle.LoadFromMemoryAsync(c.bytes); // Workaround: Don't subject the bundle to the usual compatibility checks. We want // to stay compatible with previews created in prior versions of Unity and with the // stuff we put into previews, we should generally be able to still load the content // in the editor. cr.DisableCompatibilityChecks(); searchResult.previewBundleRequest = cr; EditorApplication.CallbackFunction callback = null; // The callback will be called each tick and check if the asset bundle is ready double startTime = EditorApplication.timeSinceStartup; callback = () => { AssetStoreUtils.UpdatePreloading(); if (!cr.isDone) { double nowTime = EditorApplication.timeSinceStartup; if (nowTime - startTime > 10.0) { // Timeout. Stop polling EditorApplication.update -= callback; System.Console.WriteLine("Timed out fetch live preview bundle " + (searchResult.dynamicPreviewURL ?? "<n/a>")); // Debug.Log("Not done Timed out" + cr.progress.ToString() ); } else { // Debug.Log("Not done " + cr.progress.ToString() ); } return; } // Done cooking. Stop polling. EditorApplication.update -= callback; // Make sure that the selection hasn't changed meanwhile AssetStoreAsset sel2 = GetFirstAsset(); if (searchResult.disposed || sel2 == null || searchResult.id != sel2.id) { // No problem. Just ignore. // Debug.Log("dyn late disposed " + searchResult.disposed.ToString() + " " + (sel2 == null ? "null" : sel2.id.ToString()) + " " + searchResult.id.ToString()); } else { searchResult.previewBundle = cr.assetBundle; #pragma warning disable 618 if (cr.assetBundle == null || cr.assetBundle.mainAsset == null) { // Failed downloading live preview. Fallback to static searchResult.dynamicPreviewURL = null; DownloadStaticPreview(searchResult); } else { searchResult.previewAsset = searchResult.previewBundle.mainAsset; } #pragma warning restore 618 } }; EditorApplication.update += callback; } catch (System.Exception e) { System.Console.Write(e.Message); Debug.Log(e.Message); } }; client.Begin(); } else if (!string.IsNullOrEmpty(searchResult.staticPreviewURL)) { DownloadStaticPreview(searchResult); } // searchResult.previewBundle = null; AddAssetInternal(searchResult); RefreshFromServer(null); }
public static void AddAsset(AssetStoreAsset searchResult, Texture2D placeholderPreviewImage) { if (placeholderPreviewImage != null) { searchResult.previewImage = AssetStoreAssetSelection.ScaleImage(placeholderPreviewImage, 256, 256); } searchResult.previewInfo = null; searchResult.previewBundleRequest = null; if (!string.IsNullOrEmpty(searchResult.dynamicPreviewURL) && searchResult.previewBundle == null) { searchResult.disposed = false; AsyncHTTPClient client = new AsyncHTTPClient(searchResult.dynamicPreviewURL); client.doneCallback = delegate(AsyncHTTPClient c) { if (!client.IsSuccess()) { Console.WriteLine("Error downloading dynamic preview: " + client.text); searchResult.dynamicPreviewURL = null; AssetStoreAssetSelection.DownloadStaticPreview(searchResult); return; } AssetStoreAsset firstAsset = AssetStoreAssetSelection.GetFirstAsset(); if (searchResult.disposed || firstAsset == null || searchResult.id != firstAsset.id) { return; } try { AssetBundleCreateRequest cr = AssetBundle.LoadFromMemoryAsync(c.bytes); cr.DisableCompatibilityChecks(); searchResult.previewBundleRequest = cr; EditorApplication.CallbackFunction callback = null; double startTime = EditorApplication.timeSinceStartup; callback = delegate { AssetStoreUtils.UpdatePreloading(); if (!cr.isDone) { double timeSinceStartup = EditorApplication.timeSinceStartup; if (timeSinceStartup - startTime > 10.0) { EditorApplication.update = (EditorApplication.CallbackFunction)Delegate.Remove(EditorApplication.update, callback); Console.WriteLine("Timed out fetch live preview bundle " + (searchResult.dynamicPreviewURL ?? "<n/a>")); } return; } EditorApplication.update = (EditorApplication.CallbackFunction)Delegate.Remove(EditorApplication.update, callback); AssetStoreAsset firstAsset2 = AssetStoreAssetSelection.GetFirstAsset(); if (!searchResult.disposed && firstAsset2 != null && searchResult.id == firstAsset2.id) { searchResult.previewBundle = cr.assetBundle; if (cr.assetBundle == null || cr.assetBundle.mainAsset == null) { searchResult.dynamicPreviewURL = null; AssetStoreAssetSelection.DownloadStaticPreview(searchResult); } else { searchResult.previewAsset = searchResult.previewBundle.mainAsset; } } }; EditorApplication.update = (EditorApplication.CallbackFunction)Delegate.Combine(EditorApplication.update, callback); } catch (Exception ex) { Console.Write(ex.Message); Debug.Log(ex.Message); } }; client.Begin(); } else if (!string.IsNullOrEmpty(searchResult.staticPreviewURL)) { AssetStoreAssetSelection.DownloadStaticPreview(searchResult); } AssetStoreAssetSelection.AddAssetInternal(searchResult); AssetStoreAssetSelection.RefreshFromServer(null); }