public static void HandlePostInstall()
        {
            // Remove from the Editor update loop; we only need this to run once.
            EditorApplication.update -= HandlePostInstall;

            // Check if user just installed or upgraded Poly Toolkit.
            string basePath        = PtUtils.GetPtBaseLocalPath();
            string upgradeFilePath = PtUtils.ToAbsolutePath(basePath + "/upgrade.dat");
            string currentVersion  = "";
            bool   isUpgrade       = false;

            try {
                currentVersion = File.ReadAllText(upgradeFilePath).Trim();
            } catch (Exception) {}

            if (currentVersion == PtSettings.Version.ToString())
            {
                return;
            }
            isUpgrade = !string.IsNullOrEmpty(currentVersion);
            // Show the welcome window.
            WelcomeWindow.ShowWelcomeWindow();
            AssetBrowserWindow.BrowsePolyAssets();
            File.WriteAllText(upgradeFilePath, PtSettings.Version.ToString());

            // In the future, if we need to do any post-upgrade maintenance, we can add it here.
            PtAnalytics.SendEvent(isUpgrade ? PtAnalytics.Action.INSTALL_UPGRADE
      : PtAnalytics.Action.INSTALL_NEW_2, PtSettings.Version.ToString());
        }
        /// <summary>
        /// Scans the project for PtAsset assets marked as third-party and generates an attributions
        /// file in the user's Resources directory containing a list of those resources, the names
        /// of the authors and links to the original creations.
        /// </summary>
        public static void Generate(bool showUi)
        {
            string fileFullPath = Path.Combine(PtUtils.ToAbsolutePath(PtSettings.Instance.resourcesPath),
                                               AttributionGeneration.ATTRIB_FILE_NAME);

            string[] assetGuids = AssetDatabase.FindAssets(PtAsset.FilterString);
            // List of assets that are licensed under Creative Commons (require attribution).
            List <PtAsset> ccByAssets = new List <PtAsset>();

            foreach (string assetGuid in assetGuids)
            {
                string  localPath = AssetDatabase.GUIDToAssetPath(assetGuid);
                PtAsset ptAsset   = AssetDatabase.LoadAssetAtPath <PtAsset>(localPath);
                if (ptAsset != null && ptAsset.license == PolyAssetLicense.CREATIVE_COMMONS_BY)
                {
                    ccByAssets.Add(ptAsset);
                }
            }

            if (ccByAssets.Count == 0)
            {
                // No need for an attribution file.
                if (File.Exists(fileFullPath))
                {
                    File.Delete(fileFullPath);
                }
                if (showUi)
                {
                    EditorUtility.DisplayDialog("No Assets Require Attribution",
                                                "No Poly assets were found in the project that require attribution. " +
                                                "No attribution file was generated.", "OK");
                }
                return;
            }

            Directory.CreateDirectory(Path.GetDirectoryName(fileFullPath));
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(AttributionGeneration.FILE_HEADER);
            ccByAssets.Sort((PtAsset a, PtAsset b) => { return(a.title.CompareTo(b.title)); });
            foreach (PtAsset ptAsset in ccByAssets)
            {
                sb.AppendLine();
                sb.Append(AttributionGeneration.GenerateAttributionString(ptAsset.title, ptAsset.author,
                                                                          ptAsset.url, AttributionGeneration.CC_BY_LICENSE)).AppendLine();
            }
            File.WriteAllText(fileFullPath, sb.ToString());

            if (showUi)
            {
                EditorUtility.DisplayDialog("File Generated",
                                            "Attributions file generated at:\n" + fileFullPath + ".\n\nYou can load this file at runtime " +
                                            "and display it with Resources.Load().", "OK");
            }
        }
Exemplo n.º 3
0
        private bool PrepareDownload(PolyAsset asset, out string baseName, out string downloadLocalPath)
        {
            assetsBeingDownloaded.Remove(asset);
            PtDebug.LogFormat("ABM: Preparing to download {0}", asset);

            // basePath is something like Assets/Poly/Sources.
            string baseLocalPath = PtUtils.NormalizeLocalPath(PtSettings.Instance.assetSourcesPath);

            if (!baseLocalPath.StartsWith("Assets/"))
            {
                Debug.LogErrorFormat("Invalid asset sources folder {0}. Must be under Assets folder.");
                baseName = downloadLocalPath = null;
                return(false);
            }

            // basePathAbs is something like C:\Users\foo\bar\MyUnityProject\Assets\Poly\Sources
            string baseFullPath = PtUtils.ToAbsolutePath(baseLocalPath);

            if (!Directory.Exists(baseFullPath))
            {
                Directory.CreateDirectory(baseFullPath);
            }

            baseName = PtUtils.GetPtAssetBaseName(asset);
            PtDebug.LogFormat("Import name: {0}", baseName);

            // downloadLocalPath is something like Assets/Poly/Sources/assetTitle_assetId
            downloadLocalPath = baseLocalPath + "/" + baseName;
            string downloadFullPath = PtUtils.ToAbsolutePath(downloadLocalPath);

            if (Directory.Exists(downloadFullPath))
            {
                if (PtSettings.Instance.warnOnSourceOverwrite &&
                    !EditorUtility.DisplayDialog("Warning: Overwriting asset source folder",
                                                 string.Format("The asset source folder '{0}' will be deleted and created again. " +
                                                               "This should be safe *unless* you have manually made changes to its contents, " +
                                                               "in which case you will lose those changes.\n\n" +
                                                               "(You can silence this warning in Poly Toolkit settings)",
                                                               asset.displayName, downloadLocalPath), "OK", "Cancel"))
                {
                    return(false);
                }
                Directory.Delete(downloadFullPath, /* recursive */ true);
            }

            // Create the download folder.
            // Something like C:\Users\foo\bar\MyUnityProject\Assets\Poly\Sources\assetTitle_assetId
            Directory.CreateDirectory(downloadFullPath);
            return(true);
        }
Exemplo n.º 4
0
        static WarnAboutUsingPolyInternal()
        {
            string basePath = PtUtils.GetPtBaseLocalPath();

            List <string> offendingFiles = new List <string>();

            foreach (string asset in AssetDatabase.GetAllAssetPaths())
            {
                // Only check .cs files.
                if (!asset.EndsWith(".cs"))
                {
                    continue;
                }
                // Don't check Poly Toolkit script files.
                if (asset.StartsWith(basePath + "/"))
                {
                    continue;
                }
                // If we got here, this is a user-defined script file. Let's check that it's not using PolyToolkitInternal.
                // Note that the asset database cannot always be trusted; sometimes the assets do not exist.
                string contents;
                try { contents = File.ReadAllText(asset); }
                catch (FileNotFoundException) { continue; }

                // If the user has silenced the warning by adding the special marker, skip this file.
                if (contents.Contains("NO_POLY_TOOLKIT_INTERNAL_CHECK"))
                {
                    continue;
                }

                // This is a pretty naive check but I can't think of any legitimate reason someone would have the string
                // "PolyToolkitInternal" or "PolyToolkitEditor" in their script file. But if they do, they can add
                // NO_POLY_TOOLKIT_INTERNAL_CHECK to the file to silence the warning.
                if (contents.Contains("PolyToolkitInternal") || contents.Contains("PolyToolkitEditor"))
                {
                    offendingFiles.Add(asset);
                }
            }
            if (offendingFiles.Count > 0)
            {
                string errorMessage = ERROR_MESSAGE_HEADER + string.Join("\n", offendingFiles.ToArray());
                Debug.LogError(errorMessage);
                EditorUtility.DisplayDialog(ERROR_MESSAGE_TITLE, errorMessage, "OK");
            }
        }
Exemplo n.º 5
0
        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);
                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");
                return;
            }

            string baseName, downloadLocalPath;

            if (!PrepareDownload(asset, out baseName, out downloadLocalPath))
            {
                return;
            }

            string absPath = PtUtils.ToAbsolutePath(downloadLocalPath);

            string extension = isGltf2 ? ".gltf2" : ".gltf";
            string fileName  = baseName + extension;

            // We have to place an import request so that PolyImporter does the right thing when it sees the new file.
            PolyImporter.AddImportRequest(new PolyImporter.ImportRequest(
                                              downloadLocalPath + "/" + fileName, ptAssetLocalPath, options, asset));

            // Now unpackage it. GltfProcessor will pick it up automatically.
            UnpackPackageToFolder(isGltf2 ? asset.GetFormatIfExists(PolyFormatType.GLTF_2) :
                                  asset.GetFormatIfExists(PolyFormatType.GLTF), absPath, fileName);

            PtDebug.LogFormat("ABM: Successfully downloaded {0} to {1}", asset, absPath);
            AssetDatabase.Refresh();
            if (null != refreshCallback)
            {
                refreshCallback();
            }
        }
 private void Initialize()
 {
     welcomeTex = PtUtils.LoadTexture2DFromRelativePath(WELCOME_TEX);
     position   = new Rect(position.xMin, position.yMin, DEFAULT_WIDTH, DEFAULT_HEIGHT);
 }
Exemplo n.º 7
0
        /// <summary>
        /// Executes the given import request, producing a PtAsset and a prefab.
        /// </summary>
        /// <param name="request">The request to perform.</param>
        private static void ExecuteImportRequest(ImportRequest request)
        {
            PtDebug.LogFormat("Executing import request: {0}", request);

            string gltfFullPath   = PtUtils.ToAbsolutePath(request.gltfLocalPath);
            string assetLocalPath = request.ptAssetLocalPath;
            string assetFullPath  = PtUtils.ToAbsolutePath(assetLocalPath);

            PtAsset assetToReplace = AssetDatabase.LoadAssetAtPath <PtAsset>(assetLocalPath);

            GameObject prefabToReplace = null;

            if (assetToReplace != null)
            {
                if (assetToReplace.assetPrefab == null)
                {
                    Debug.LogErrorFormat("Couldn't find prefab for asset {0}.", assetToReplace);
                    PtAnalytics.SendEvent(PtAnalytics.Action.IMPORT_FAILED, "Prefab not found");
                    return;
                }
                prefabToReplace = assetToReplace.assetPrefab;
            }

            // Determine if file is glTF2 or glTF1.
            bool isGltf2 = Path.GetExtension(request.gltfLocalPath) == ".gltf2";

            // First, import the GLTF and build a GameObject from it.
            EditorUtility.DisplayProgressBar(PROGRESS_BAR_TITLE, PROGRESS_BAR_TEXT, 0.5f);
            // Use a SanitizedPath stream loader because any format file we have downloaded and saved to disk we
            // have replaced the original relative path string with the MD5 string hash. This custom stream loader
            // will always convert uris passed to it to this hash value, and read them from there.
            IUriLoader binLoader = new HashedPathBufferedStreamLoader(Path.GetDirectoryName(gltfFullPath));

            ImportGltf.GltfImportResult result = null;
            using (TextReader reader = new StreamReader(gltfFullPath)) {
                result = ImportGltf.Import(isGltf2 ? GltfSchemaVersion.GLTF2 : GltfSchemaVersion.GLTF1,
                                           reader, binLoader, request.options.baseOptions);
            }
            EditorUtility.ClearProgressBar();
            string baseName = PtUtils.GetPtAssetBaseName(request.polyAsset);

            result.root.name = baseName;

            // Create the asset (delete it first if it exists).
            if (File.Exists(assetFullPath))
            {
                AssetDatabase.DeleteAsset(assetLocalPath);

                // If we are replacing an existing asset, we should rename the replacement to the new name,
                // since the name reflects the identity of the asset. So if the user is importing the asset
                // dog_a381b3g to replace what was previously cat_v81938.asset, the replacement file should
                // be named dog_a381b3g.asset, not cat_v81938.asset.
                assetLocalPath = PtUtils.GetDefaultPtAssetPath(request.polyAsset);
                assetFullPath  = PtUtils.ToAbsolutePath(assetLocalPath);
            }
            Directory.CreateDirectory(Path.GetDirectoryName(assetFullPath));

            // Create the new PtAsset and fill it in.
            AssetDatabase.CreateAsset(ScriptableObject.CreateInstance <PtAsset>(), assetLocalPath);
            PtAsset newAsset = AssetDatabase.LoadAssetAtPath <PtAsset>(assetLocalPath);

            newAsset.name    = baseName;
            newAsset.title   = request.polyAsset.displayName ?? "";
            newAsset.author  = request.polyAsset.authorName ?? "";
            newAsset.license = request.polyAsset.license;
            newAsset.url     = request.polyAsset.Url;

            // Ensure the imported object has a PtAssetObject component which references the PtAsset.
            result.root.AddComponent <PtAssetObject>().asset = newAsset;

            // Add all the meshes to the PtAsset.
            SaveMeshes(result.meshes, newAsset);

            // If the asset has materials, save those to the PtAsset.
            if (result.materials != null)
            {
                SaveMaterials(result.materials, newAsset);
            }

            // If the asset has textures, save those to the PtAsset.
            if (result.textures != null)
            {
                SaveTextures(result.textures, newAsset);
            }

            // Reimport is required to ensure custom asset displays correctly.
            AssetDatabase.ImportAsset(assetLocalPath);

            GameObject newPrefab;

            if (prefabToReplace)
            {
                // Replace the existing prefab with our new object, without breaking prefab connections.
                newPrefab = PrefabUtility.ReplacePrefab(result.root, prefabToReplace, ReplacePrefabOptions.ReplaceNameBased);
                AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(newPrefab), baseName);
            }
            else
            {
                // Create a new prefab.
                // Prefab path is the same as the asset path but with the extension changed to '.prefab'.
                string prefabLocalPath = Regex.Replace(assetLocalPath, "\\.asset$", ".prefab");
                if (!prefabLocalPath.EndsWith(".prefab"))
                {
                    Debug.LogErrorFormat("Error: failed to compute prefab path for {0}", assetLocalPath);
                    PtAnalytics.SendEvent(PtAnalytics.Action.IMPORT_FAILED, "Prefab path error");
                    return;
                }
                newPrefab = PrefabUtility.CreatePrefab(prefabLocalPath, result.root);
            }

            // Now ensure the asset points to the prefab.
            newAsset.assetPrefab = newPrefab;
            if (newAsset.assetPrefab == null)
            {
                Debug.LogErrorFormat("Could not get asset prefab reference for asset {0}", newAsset);
                PtAnalytics.SendEvent(PtAnalytics.Action.IMPORT_FAILED, "Prefab ref error");
            }

            GameObject.DestroyImmediate(result.root);

            AssetDatabase.Refresh();

            if (request.options.alsoInstantiate)
            {
                PrefabUtility.InstantiatePrefab(newPrefab);
            }

            PtDebug.LogFormat("GLTF import complete: {0}", request);

            PtAnalytics.SendEvent(PtAnalytics.Action.IMPORT_SUCCESSFUL, isGltf2 ? "GLTF2" : "GLTF1");

            // 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);

            // Select the prefab in the editor so the user knows where it is.
            AssetDatabase.Refresh();
            Selection.activeObject = newPrefab;
            EditorGUIUtility.PingObject(newPrefab);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Loads a texture file from the Poly Toolkit installation folder given a relative path
        /// from the installation folder to the texture.
        /// </summary>
        /// <param name="relativePath">The relative path were the texture is located. For example,
        /// this could be Editor/Textures/PolyToolkitTitle.png.</param>
        /// <returns>The texture.</returns>
        public static Texture2D LoadTexture2DFromRelativePath(string relativePath)
        {
            string basePath = PtUtils.GetPtBaseLocalPath();

            return(AssetDatabase.LoadAssetAtPath <Texture2D>(Path.Combine(basePath, relativePath)));
        }