Beispiel #1
0
            private static void ExportWebP(string path, string name, Texture2D texture, int quality, ICollection <FileInfo> output)
            {
                var hasAlpha      = ReallyHasAlpha(texture);
                var outputTexture = CopyTexture(texture, hasAlpha ? TextureFormat.RGBA32 : TextureFormat.RGB24);

                var tempPngInputPath = Path.Combine(Application.temporaryCachePath, "webp-input.png");

                File.WriteAllBytes(tempPngInputPath, outputTexture.EncodeToPNG());

                quality = Math.Max(0, Math.Min(100, quality));

                var dstFile = new FileInfo(Path.Combine(path, name + ".webp"))
                              .FullName;

                UTinyBuildUtilities.RunInShell(
                    $"cwebp -quiet -q {quality} \"{tempPngInputPath}\" -o \"{dstFile}\"",
                    new ShellProcessArgs()
                {
                    ExtraPaths = ImageUtilsPath().AsEnumerable()
                });

                File.Delete(tempPngInputPath);

                output.Add(new FileInfo(dstFile));
            }
        private static void InstallRuntime(bool force, bool silent)
        {
            try
            {
                var installLocation = new DirectoryInfo("UTiny");
                var versionFile     = new FileInfo(Path.Combine(installLocation.FullName, "lastUpdate.txt"));
                var sourcePackage   = new FileInfo(UTinyConstants.PackagePath + "tiny-runtime-dist.zip");
                var shouldUpdate    = sourcePackage.Exists && (!versionFile.Exists || versionFile.LastWriteTimeUtc < sourcePackage.LastWriteTimeUtc);

                if (!force && !shouldUpdate)
                {
                    if (!silent)
                    {
                        Debug.Log("Tiny: Runtime is already up to date");
                    }
                    return;
                }

                if (!sourcePackage.Exists)
                {
                    if (!silent)
                    {
                        Debug.LogError($"Tiny: could not find {sourcePackage.FullName}");
                    }
                    return;
                }

                if (installLocation.Exists)
                {
                    EditorUtility.DisplayProgressBar($"{UTinyConstants.ApplicationName} Runtime", "Removing old runtime...", 0.0f);
                    UTinyBuildUtilities.PurgeDirectory(installLocation);
                }
                EditorUtility.DisplayProgressBar($"{UTinyConstants.ApplicationName} Runtime", "Installing new runtime...", 0.5f);
                UTinyBuildUtilities.UnzipFile(sourcePackage.FullName, installLocation.Parent);
                File.WriteAllText(versionFile.FullName, $"{sourcePackage.FullName} install time: {DateTime.UtcNow.ToString()}");

#if UNITY_EDITOR_OSX
                // TODO: figure out why UnzipFile does not preserve executable bits in some cases
                // chmod +x any native executables here
                UTinyBuildUtilities.RunInShell("chmod +x cwebp moz-cjpeg pngcrush",
                                               new ShellProcessArgs()
                {
                    WorkingDirectory = new DirectoryInfo(GetToolDirectory("images/osx")),
                    ExtraPaths       = "/bin".AsEnumerable(), // adding this folder just in case, but should be already in $PATH
                    ThrowOnError     = false
                });
#endif

                Debug.Log($"Installed {UTinyConstants.ApplicationName} runtime at: {installLocation.FullName}");
            }
            finally
            {
                EditorUtility.ClearProgressBar();
            }
        }
Beispiel #3
0
            private static void ExportJpgOptimized(string path, string name, Texture2D texture, int quality, ICollection <FileInfo> output)
            {
                var colorFilePath = $"{Path.Combine(path, name)}.jpg";

                var outputTexture = CopyTexture(texture, TextureFormat.RGB24);
                //var tempPngInputPath = Path.Combine(Application.temporaryCachePath, "webp-input.png");
                var tempPngInputPath = $"{Path.Combine(path, name)}-input.png";

                File.WriteAllBytes(tempPngInputPath, outputTexture.EncodeToPNG());

                quality = Math.Max(0, Math.Min(100, quality));

                // this will build progressive jpegs by default; -baseline stops this.
                // progressive results in better compression
                UTinyBuildUtilities.RunInShell(
                    $"moz-cjpeg -quality {quality} -outfile \"{colorFilePath}\" \"{tempPngInputPath}\"",
                    new ShellProcessArgs()
                {
                    ExtraPaths = ImageUtilsPath().AsEnumerable()
                });

                File.Delete(tempPngInputPath);

                output.Add(new FileInfo(colorFilePath));

                if (ReallyHasAlpha(texture))
                {
                    // @TODO Optimization by reusing the texture above
                    var outputAlphaTexture = CopyTexture(texture, TextureFormat.RGBA32);

                    var pixels = outputAlphaTexture.GetPixels32();

                    // broadcast alpha to color
                    for (var i = 0; i < pixels.Length; i++)
                    {
                        pixels[i].r = pixels[i].a;
                        pixels[i].g = pixels[i].a;
                        pixels[i].b = pixels[i].a;
                        pixels[i].a = 255;
                    }

                    outputAlphaTexture.SetPixels32(pixels);
                    outputAlphaTexture.Apply();

                    //var pngCrushInputPath = Path.Combine(Application.temporaryCachePath, "alpha-input.png");
                    var pngCrushInputPath = $"{Path.Combine(path, name)}_a-input.png";
                    var alphaFilePath     = $"{Path.Combine(path, name)}_a.png";

                    using (var stream = new FileStream(pngCrushInputPath, FileMode.Create))
                    {
                        var bytes = outputAlphaTexture.EncodeToPNG();
                        stream.Write(bytes, 0, bytes.Length);
                    }

                    // convert to 8-bit grayscale png
                    UTinyBuildUtilities.RunInShell(
                        $"pngcrush -s -c 0 \"{pngCrushInputPath}\" \"{alphaFilePath}\"",
                        new ShellProcessArgs()
                    {
                        ExtraPaths = ImageUtilsPath().AsEnumerable()
                    });

                    output.Add(new FileInfo(alphaFilePath));
                }
            }
        public static UTinyBuildResults Build(UTinyBuildOptions options)
        {
            if (options?.Project == null || options.Destination == null)
            {
                throw new ArgumentException($"{UTinyConstants.ApplicationName}: invalid build options provided", nameof(options));
            }

            var buildStart = DateTime.Now;

            var           results = new UTinyBuildResults();
            IUTinyBuilder builder = null;

            switch (options.Platform)
            {
            case UTinyPlatform.HTML5:
                builder = new UTinyHTML5Builder();
                break;

            default:
                throw new ArgumentException($"{UTinyConstants.ApplicationName}: build platform not supported", nameof(options));
            }

            try
            {
                EditorUtility.DisplayProgressBar(ProgressBarTitle, "Build started for " + options.Platform.ToString(),
                                                 0.0f);

                var destFolder = options.Destination;
                destFolder.Create();

                // BUILD = <DEST>/PLATFORM/CONFIG
                var buildFolder = new DirectoryInfo(GetBuildDirectory(options.Project, options.Platform, options.Configuration));

                results.OutputFolder = buildFolder;

                UTinyBuildUtilities.PurgeDirectory(buildFolder);
                buildFolder.Create();

                options.Destination = results.BinaryFolder = buildFolder;

                var idlFile = new FileInfo(Path.Combine(buildFolder.FullName, "generated.cs"));
                UTinyIDLGenerator.GenerateIDL(options.Project, idlFile);

                var distFolder = GetRuntimeDistFolder();

                var bindGem = new FileInfo(Path.Combine(
                                               distFolder.FullName, "bindgem/BindGem/bin/Release/BindGem.exe"));

                var exeName = "\"" + bindGem.FullName + "\"";

                // always call bindgem with mono for consistency
                exeName = "mono " + exeName;

                // reference the core runtime file
                var bindReferences = $"-r \"{RuntimeDefsAssemblyPath}\"";

                UTinyBuildUtilities.RunInShell(
                    $"{exeName} -j {bindReferences} -o bind-generated {idlFile.Name}",
                    new ShellProcessArgs()
                {
                    WorkingDirectory = buildFolder,
                    ExtraPaths       = TinyPreferences.MonoDirectory.AsEnumerable()
                });

                // @TODO Perform a full refresh before building

                builder.Build(options, results);

                results.BuildReport.Update();

                Debug.Log($"{UTinyConstants.ApplicationName} project generated at: {results.BinaryFolder.FullName}");

                TinyEditorAnalytics.SendBuildEvent(options.Project, results, DateTime.Now - buildStart);
                return(results);
            }
            catch (Exception ex)
            {
                TinyEditorAnalytics.SendException("BuildPipeline.Build", ex);
                throw;
            }
            finally
            {
                EditorUtility.ClearProgressBar();
                UTinyEditorUtility.RepaintAllWindows();
            }
        }