Skip to content

Commit

Permalink
Fixes and refactor based on current API implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
andybak committed Nov 13, 2024
1 parent 1beafc7 commit 4effae1
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 67 deletions.
10 changes: 5 additions & 5 deletions Assets/TestScenes/PolyToolkitCmd/PolyToolkitCmd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ private void CmdShow(string[] args) {
for (int i = 0; i < currentResults.Count; i++) {
PolyAsset asset = currentResults[i];
PrintLn("[{0}]: {1}\n ID: {2}\n Author: {3}\n Formats: {4}\n",
i, asset.displayName, asset.name, asset.authorName,
i, asset.displayName, asset.assetId, asset.authorName,
FormatListToString(asset.formats));
}
}
Expand Down Expand Up @@ -576,10 +576,10 @@ private void CmdImp(string[] args) {
PrintLn("Importing asset... May take a while. Please wait!");
PolyApi.Import(assetToImport, options, (PolyAsset asset, PolyStatusOr<PolyImportResult> result) => {
if (!result.Ok) {
PrintLn("ERROR: failed to import {0}: {1}", asset.name, result.Status);
PrintLn("ERROR: failed to import {0}: {1}", asset.assetId, result.Status);
return;
}
PrintLn("Successfully imported asset '{0}' ({1})", asset.name, asset.displayName);
PrintLn("Successfully imported asset '{0}' ({1})", asset.assetId, asset.displayName);

if (currentAsset != null) {
Destroy(currentAsset);
Expand Down Expand Up @@ -611,13 +611,13 @@ private void CmdThumb(string[] args) {
PrintLn("Fetching thumbnail... Please wait.");
PolyApi.FetchThumbnail(assetToUse, (PolyAsset asset, PolyStatus status) => {
if (status.ok) {
PrintLn("Successfully fetched thumbnail for asset '{0}'", asset.name);
PrintLn("Successfully fetched thumbnail for asset '{0}'", asset.assetId);
imageDisplay.sprite = Sprite.Create(asset.thumbnailTexture,
new Rect(0, 0, asset.thumbnailTexture.width, asset.thumbnailTexture.height),
Vector2.zero);
imageDisplay.gameObject.SetActive(true);
} else {
PrintLn("*** Error loading thumbnail for asset '{0}': {1}", asset.name, status);
PrintLn("*** Error loading thumbnail for asset '{0}': {1}", asset.assetId, status);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ private void OnRequestForSpecificAssetResult(PolyStatusOr<PolyAsset> result) {
PtDebug.Log("ABM: get asset request received result.");
assetResult = result.Value;

if (!thumbnailCache.TryGet(assetResult.name, out assetResult.thumbnailTexture)) {
if (!thumbnailCache.TryGet(assetResult.assetId, out assetResult.thumbnailTexture)) {
FetchThumbnail(assetResult);
}
} else {
Expand All @@ -402,7 +402,7 @@ private void FinishFetchingThumbnails(PolyStatusOr<PolyListAssetsResult> result)
if (result.Ok) {
List<PolyAsset> assetsMissingThumbnails = new List<PolyAsset>();
foreach (PolyAsset asset in listAssetsResult.Value.assets) {
if (!thumbnailCache.TryGet(asset.name, out asset.thumbnailTexture)) {
if (!thumbnailCache.TryGet(asset.assetId, out asset.thumbnailTexture)) {
assetsMissingThumbnails.Add(asset);
}
}
Expand All @@ -423,7 +423,7 @@ private void FetchThumbnail(PolyAsset asset) {
/// </summary>
private void OnThumbnailFetched(PolyAsset asset, PolyStatus status) {
if (status.ok) {
thumbnailCache.Put(asset.name, asset.thumbnailTexture);
thumbnailCache.Put(asset.assetId, asset.thumbnailTexture);
// Preserve the texture so it survives round-trips to play mode and back.
asset.thumbnailTexture.hideFlags = HideFlags.HideAndDontSave;
}
Expand All @@ -439,7 +439,7 @@ private HashSet<string> GetAssetsInUse() {
HashSet<string> assetsInUse = new HashSet<string>();
if (listAssetsResult == null || !listAssetsResult.Ok) return assetsInUse;
foreach (var asset in listAssetsResult.Value.assets) {
assetsInUse.Add(asset.name);
assetsInUse.Add(asset.assetId);
}
return assetsInUse;
}
Expand All @@ -453,7 +453,7 @@ private HashSet<string> GetAssetsInUse() {
/// <param name="options">Import options.</param>
public void StartDownloadAndImport(PolyAsset asset, string ptAssetLocalPath, EditTimeImportOptions options) {
if (!assetsBeingDownloaded.Add(asset)) return;
PtDebug.LogFormat("ABM: starting to fetch asset {0} ({1}) -> {2}", asset.name, asset.displayName,
PtDebug.LogFormat("ABM: starting to fetch asset {0} ({1}) -> {2}", asset.assetId, asset.displayName,
ptAssetLocalPath);

// Prefer glTF1 to glTF2.
Expand Down Expand Up @@ -498,7 +498,7 @@ public void ClearCaches() {
private void OnFetchFinished(PolyStatus status, PolyAsset asset, bool isGltf2,
string ptAssetLocalPath, EditTimeImportOptions options) {
if (!status.ok) {
Debug.LogErrorFormat("Error fetching asset {0} ({1}): {2}", asset.name, asset.displayName, status);
Debug.LogErrorFormat("Error fetching asset {0} ({1}): {2}", asset.assetId, asset.displayName, status);
EditorUtility.DisplayDialog("Download Error",
string.Format("*** Error downloading asset '{0}'. Try again later.", asset.displayName), "OK");
PtAnalytics.SendEvent(PtAnalytics.Action.IMPORT_FAILED, "Asset fetch failed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class AssetBrowserWindow : EditorWindow {
/// <summary>
/// URL of the user's profile page.
/// </summary>
private const string USER_PROFILE_URL = "https://poly.google.com/user";
private const string USER_PROFILE_URL = "https://icosa.gallery/user";

/// <summary>
/// Width and height of each asset thumbnail image in the grid.
Expand Down Expand Up @@ -106,7 +106,7 @@ public class AssetBrowserWindow : EditorWindow {
/// Texture to use for the title bar.
/// </summary>
private const string TITLE_TEX = "Editor/Textures/PolyToolkitTitle.png";

/// <summary>
/// Texture to use for the back button (back arrow) if the skin is Unity pro.
/// </summary>
Expand All @@ -116,12 +116,12 @@ public class AssetBrowserWindow : EditorWindow {
/// Texture to use for the back button (back arrow) if the skin is Unity personal.
/// </summary>
private const string BACK_ARROW_DARK_TEX = "Editor/Textures/BackArrowDark.png";

/// <summary>
/// Texture to use for the back button bar background if the skin is Unity pro.
/// </summary>
private const string DARK_GREY_TEX = "Editor/Textures/DarkGrey.png";

/// <summary>
/// Texture to use for the back button bar background if the skin is Unity personal.
/// </summary>
Expand Down Expand Up @@ -202,12 +202,12 @@ private enum UiMode {
/// The texture to use in place of a thumbnail when loading the thumbnail.
/// </summary>
private Texture2D loadingTex = null;

/// <summary>
/// The texture to use for the back button (back arrow).
/// </summary>
private Texture2D backArrowTex = null;

/// <summary>
/// Texture used for back button bar background.
/// </summary>
Expand Down Expand Up @@ -297,7 +297,7 @@ public static void BrowsePolyAssets2() {
/// </summary>
/// <param name="assetId">The ID of the asset that was just imported.</param>
public void HandleAssetImported(string assetId) {
if (selectedAsset != null && assetId == selectedAsset.name) {
if (selectedAsset != null && assetId == selectedAsset.assetId) {
justImported = true;
}
}
Expand Down Expand Up @@ -417,8 +417,8 @@ private bool DrawHeader(out int topMargin) {
/// </summary>
private void DrawTitleBar(bool withSignInUi) {
GUI.DrawTexture(new Rect(0, 0, position.width, TITLE_BAR_HEIGHT), Texture2D.whiteTexture);
GUIStyle titleStyle = new GUIStyle (GUI.skin.label);

GUIStyle titleStyle = new GUIStyle (GUI.skin.label);
titleStyle.margin = new RectOffset(TITLE_IMAGE_PADDING, TITLE_IMAGE_PADDING, TITLE_IMAGE_PADDING,
TITLE_IMAGE_PADDING);
if (GUILayout.Button(titleTex, titleStyle,
Expand Down Expand Up @@ -505,7 +505,7 @@ private void DrawBrowseUi() {
menu.ShowAsContext();
}
guiHelper.EndHorizontal();

// Draw the "Asset type" toggles.
bool showAssetTypeFilter = (CATEGORIES[selectedCategory].key != KEY_YOUR_LIKES);

Expand Down Expand Up @@ -578,7 +578,7 @@ private void DrawSearchUi() {
if (searchClicked && searchTerms.Trim().Length > 0) {
// Note: for privacy reasons we don't log the search terms, just the fact that a search was made.
PtAnalytics.SendEvent(PtAnalytics.Action.BROWSE_SEARCHED);

string assetId;
if (SearchTermIsAssetPage(searchTerms, out assetId)) {
manager.StartRequestForSpecificAsset(assetId);
Expand Down Expand Up @@ -621,7 +621,7 @@ private void DrawResultsGrid() {
}

if (manager.CurrentResult == null) {
return;
return;
}

if (manager.CurrentResult != null && !manager.CurrentResult.Status.ok) {
Expand All @@ -641,7 +641,7 @@ private void DrawResultsGrid() {

return;
}

if (manager.CurrentResult.Value.assets == null ||
manager.CurrentResult.Value.assets.Count == 0) {
GUILayout.Space(30);
Expand Down Expand Up @@ -724,7 +724,7 @@ private void DrawResultsGrid() {

guiHelper.EndHorizontal();
GUILayout.Space(10);

bool loadMoreClicked = false;
if (manager.resultHasMorePages) {
// If the current response has at least another page of results left, show the load more button.
Expand All @@ -738,7 +738,7 @@ private void DrawResultsGrid() {

guiHelper.EndVertical();
guiHelper.EndScrollView();

if (loadMoreClicked) {
manager.GetNextPageRequest();
return;
Expand Down Expand Up @@ -859,7 +859,7 @@ private void StartRequest() {
}
} else if (mode == UiMode.SEARCH) {
PolyListAssetsRequest request = new PolyListAssetsRequest();
request.keywords = searchTerms;
request.keywords = searchTerms;
manager.StartRequest(request);
} else {
throw new System.Exception("Unexpected UI mode for StartQuery: " + mode);
Expand Down Expand Up @@ -915,14 +915,14 @@ private void DrawDetailsUi() {

detailsScrollPos = guiHelper.BeginScrollView(detailsScrollPos);
const float width = 150;

guiHelper.BeginHorizontal();
GUILayout.Label(selectedAsset.displayName, detailsTitleStyle);
guiHelper.EndHorizontal();

guiHelper.BeginHorizontal();
GUILayout.Label(selectedAsset.authorName, EditorStyles.wordWrappedLabel);

GUILayout.FlexibleSpace();
if (GUILayout.Button("View on Web", GUILayout.MaxWidth(100))) {
PtAnalytics.SendEvent(PtAnalytics.Action.BROWSE_VIEW_ON_WEB);
Expand Down Expand Up @@ -970,7 +970,7 @@ private void DrawDetailsUi() {
guiHelper.EndScrollView();
return;
}

GUILayout.Space(5);
GUILayout.Label("Import Options", EditorStyles.boldLabel);
GUILayout.Space(5);
Expand Down Expand Up @@ -1000,7 +1000,7 @@ private void DrawDetailsUi() {
PtSettings.Instance.defaultImportOptions.alsoInstantiate = importOptions.alsoInstantiate;
}
SendImportOptionMutationAnalytics(oldOptions, importOptions);

GUILayout.Space(10);
GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));
GUILayout.Space(10);
Expand All @@ -1013,7 +1013,7 @@ private void DrawDetailsUi() {
GUILayout.FlexibleSpace();
guiHelper.EndHorizontal();
GUILayout.Space(5);

if (!File.Exists(PtUtils.ToAbsolutePath(ptAssetLocalPath))) {
GUILayout.Label(PolyInternalUtils.ATTRIBUTION_NOTICE, EditorStyles.wordWrappedLabel);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private static void ExecuteImportRequest(ImportRequest request) {
// If this is a third-party asset, we need to update the attributions file.
AttributionFileGenerator.Generate(/* showUi */ false);

EditorWindow.GetWindow<AssetBrowserWindow>().HandleAssetImported(request.polyAsset.name);
EditorWindow.GetWindow<AssetBrowserWindow>().HandleAssetImported(request.polyAsset.assetId);

// Select the prefab in the editor so the user knows where it is.
AssetDatabase.Refresh();
Expand Down
2 changes: 1 addition & 1 deletion Packages/icosa-api-client-unity/Editor/PtUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static string GetPtAssetBaseName(PolyAsset asset) {
return string.Format("{0}_{1}_{2}",
SanitizeToUseAsFileName(asset.displayName),
SanitizeToUseAsFileName(asset.authorName),
SanitizeToUseAsFileName(asset.name).Replace("assets_", ""));
SanitizeToUseAsFileName(asset.assetId).Replace("assets_", ""));
}

public static string GetPtBaseLocalPath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void ImportAsync(PolyAsset asset, PolyFormat format, PolyImportOptions op

private static void BackgroundThreadProc(object userData) {
ImportOperation operation = (ImportOperation)userData;
try {
try {
using (TextReader reader = new StreamReader(new MemoryStream(operation.format.root.contents), Encoding.UTF8)) {
operation.importState = ImportGltf.BeginImport(
operation.format.formatType == PolyFormatType.GLTF ? GltfSchemaVersion.GLTF1 : GltfSchemaVersion.GLTF2,
Expand Down Expand Up @@ -105,7 +105,7 @@ public void Update() {
if (finishedOperations.Count == 0) return;
operation = finishedOperations.Dequeue();
}

if (!operation.status.ok) {
// Import failed.
operation.callback(operation.status, root: null, meshCreator: null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class AttributionGeneration {
/// File header.
/// </summary>
public static readonly string FILE_HEADER =
"This project uses the following items from Poly (http://poly.google.com):";
"This project uses the following items from Icosa Gallery (http://icosa.gallery):";
/// <summary>
/// Information on the Creative Commons license for the attribution file.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ public static GltfImportResult Import(

private static GltfRootBase DeserializeGltfRoot(GltfSchemaVersion gltfVersion, JsonTextReader reader) {
switch (gltfVersion) {
case GltfSchemaVersion.GLTF1: {
var gltf1Root = kSerializer.Deserialize<Gltf1Root>(reader);
case GltfSchemaVersion.GLTF1:
{
Gltf1Root gltf1Root = kSerializer.Deserialize<Gltf1Root>(reader);
if (gltf1Root == null || gltf1Root.nodes == null) {
throw new Exception("Failed to parse GLTF1. File is empty or in the wrong format.");
}
Expand Down Expand Up @@ -786,12 +787,12 @@ static List<MeshPrecursor> CreateMeshPrecursorsFromPrimitive(ImportState state,
ChangeBasisAndApplyScale(data, Semantic.UnitlessVector, state.scaleFactor);
mesh.normals = (Vector3[]) data;
break;
case "COLOR":
case "COLOR":
case "COLOR_0": {
Color[] colors = data as Color[];
if (colors == null) {
Debug.LogWarningFormat(
"Unsupported: color buffer of type {0}",
"Unsupported: color buffer of type {0}",
data == null ? "null" : data.GetType().ToString());
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public void Import(PolyAsset asset, PolyImportOptions options, PolyApi.ImportCal
PolyFormat gltfFormat = asset.GetFormatIfExists(PolyFormatType.GLTF);
PolyFormat gltf2Format = asset.GetFormatIfExists(PolyFormatType.GLTF_2);

if (gltf2Format != null && gltfFormat == null) {
if (gltf2Format != null) {
FetchAndImportFormat(asset, gltf2Format, options, callback);
} else if (gltfFormat != null) {
FetchAndImportFormat(asset, gltfFormat, options, callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void Fetch() {
long cacheAgeMaxMillis = asset.IsMutable ? 0 : CACHE_MAX_AGE_MILLIS;
PolyMainInternal.Instance.webRequestManager.EnqueueRequest(MakeRequest, ProcessResponse, cacheAgeMaxMillis);
}

private UnityWebRequest MakeRequest() {
string url = asset.thumbnail.url;
// If an image size hint was provided, forward it to the server if the server supports it.
Expand All @@ -83,7 +83,7 @@ private void ProcessResponse(PolyStatus status, int responseCode, byte[] data) {
asset.thumbnailTexture.LoadImage(data);
} else {
Debug.LogWarningFormat("Failed to fetch thumbnail for asset {0} ({1}): {2}",
asset.name, asset.displayName, status);
asset.assetId, asset.displayName, status);
}
if (callback != null) {
callback(asset, status);
Expand Down
Loading

0 comments on commit 4effae1

Please sign in to comment.