Beispiel #1
0
 private static Texture2D GetAssetPreviewFromGUID(string guid)
 {
     return(AssetPreview.GetAssetPreviewFromGUID(guid));
 }
Beispiel #2
0
        public static void AddToUnityPackage(string pathToFileOrDirectory, string targetDir, ref Dictionary <string, string> guidToFile)
        {
            if (!File.Exists(pathToFileOrDirectory) && !Directory.Exists(pathToFileOrDirectory))
            {
                Debug.LogError("File " + pathToFileOrDirectory + " does not exist");
                return;
            }
            if (Directory.Exists(targetDir))
            {
                Directory.CreateDirectory(targetDir);
            }

            if (guidToFile.ContainsValue(pathToFileOrDirectory))
            {
                throw new ArgumentException($"Duplicate Path exported! {pathToFileOrDirectory}");
            }

            var isFile = File.Exists(pathToFileOrDirectory);

            if (pathToFileOrDirectory.EndsWith(".meta", StringComparison.Ordinal))
            {
                return;
            }
            var metaPath    = pathToFileOrDirectory + ".meta";
            var guid        = default(string);
            var hasMetaFile = File.Exists(metaPath);

            if (hasMetaFile)
            {
                using (var reader = new StreamReader(metaPath))
                {
                    var line = reader.ReadLine();
                    while (line != null)
                    {
                        if (line.StartsWith("guid:", StringComparison.Ordinal))
                        {
                            guid = line.Substring("guid:".Length).Trim();
                            if (guidToFile.ContainsKey(guid))
                            {
                                throw new ArgumentException($"Duplicate GUID in AssetDatabase ({guid})?! Existing: {guidToFile[guid]}, new: {pathToFileOrDirectory}");
                            }
                            guidToFile.Add(guid, pathToFileOrDirectory);
                            break;
                        }
                        line = reader.ReadLine();
                    }
                }
            }
            else
            {
                if (File.Exists(pathToFileOrDirectory))
                {
                    var bytes = File.ReadAllBytes(pathToFileOrDirectory);
                    using (var md5 = MD5.Create()) {
                        md5.TransformFinalBlock(bytes, 0, bytes.Length);
                        var hash = md5.Hash;
                        // guid = BitConverter.ToString(hash).Replace("-","");//(md5.Hash);

                        guid = new Guid(hash).ToString("N");
                        int maxTries = 200;
                        while (guidToFile.ContainsKey(guid))
                        {
                            Debug.LogWarning($"GUID collision ({guid}), Existing: {guidToFile[guid]}, new: {pathToFileOrDirectory}");
                            hash[0]++;
                            guid = new Guid(hash).ToString("N");
                            maxTries--;
                            if (maxTries < 0)
                            {
                                throw new ArgumentException($"GUID collision on file: {pathToFileOrDirectory} with GUID {guid} even after 200 attempts of finding a new one. Aborting.");
                            }
                        }
                        guidToFile.Add(guid, pathToFileOrDirectory);
                    }
                }
            }

            if (guid == null)
            {
                Debug.LogError("No guid for " + pathToFileOrDirectory);
            }
            pathToFileOrDirectory = pathToFileOrDirectory.Replace("\\", "/");
            var projectPath = Path.GetFullPath(Application.dataPath + "/../").Replace("\\", "/");
            var relPath     = pathToFileOrDirectory;

            if (relPath.StartsWith(projectPath, StringComparison.Ordinal))
            {
                relPath = relPath.Substring(projectPath.Length);
            }

            var outDir = targetDir + "/" + guid;

            if (!Directory.Exists(outDir))
            {
                Directory.CreateDirectory(outDir);
            }
            var pathNameFilePath = outDir + "/pathname";

            if (File.Exists(pathNameFilePath))
            {
                File.Delete(pathNameFilePath);
            }
            File.WriteAllText(pathNameFilePath, relPath);
            if (isFile)
            {
                File.Copy(pathToFileOrDirectory, outDir + "/asset", true);
            }
            if (hasMetaFile)
            {
                File.Copy(metaPath, outDir + "/asset.meta", true);
            }

            if (IncludePreviewImages && isFile && !extensionsWithoutThumbnails.Contains(Path.GetExtension(pathToFileOrDirectory).ToLowerInvariant()))
            {
#if UNITY_2020_1_OR_NEWER
                var preview = AssetPreview.GetAssetPreviewFromGUID(guid);
#else
                var assetPath = AssetDatabase.GUIDToAssetPath(guid);
                var asset     = string.IsNullOrEmpty(assetPath) ? null : AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(assetPath);
                var preview   = asset ? AssetPreview.GetAssetPreview(asset) : default;
#endif
                if (preview)
                {
                    var thumbnailWidth  = Mathf.Min(preview.width, 128);
                    var thumbnailHeight = Mathf.Min(preview.height, 128);
                    var rt = RenderTexture.GetTemporary(thumbnailWidth, thumbnailHeight, 0, RenderTextureFormat.Default, RenderTextureReadWrite.sRGB);

                    var copy = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false);//, preview.graphicsFormat, preview.mipmapCount, TextureCreationFlags.None);

                    RenderTexture.active = rt;
                    GL.Clear(true, true, new Color(0, 0, 0, 0));
                    Graphics.Blit(preview, rt);
                    copy.ReadPixels(new Rect(0, 0, copy.width, copy.height), 0, 0, false);
                    copy.Apply();
                    RenderTexture.active = null;

                    var bytes = copy.EncodeToPNG();
                    if (bytes != null && bytes.Length > 0)
                    {
                        File.WriteAllBytes(outDir + "/preview.png", bytes);
                    }

                    RenderTexture.ReleaseTemporary(rt);
                }
            }
        }