Exemplo n.º 1
0
        public static void Build(IBuildsCompleteListener onComplete = null)
        {
            string commandLineBuildPath = null;
            string profileName          = null;
            var    buildActiveTarget    = false;

            if (Application.isBatchMode)
            {
                string[] args = Environment.GetCommandLineArgs();
                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i].EqualsIgnoringCase("-profileName"))
                    {
                        if (i + 1 == args.Length || args[i + 1].StartsWith("-"))
                        {
                            throw new Exception("-profileName needs to be followed by a profile name.");
                        }
                        profileName = args[++i];
                    }
                    else if (args[i].EqualsIgnoringCase("-output"))
                    {
                        if (i + 1 == args.Length || args[i + 1].StartsWith("-"))
                        {
                            throw new Exception("-output needs to be followed by a path.");
                        }
                        commandLineBuildPath = args[++i];
                    }
                    else if (args[i].EqualsIgnoringCase("-buildTarget"))
                    {
                        // Unity will validate the value of the -buildTarget option
                        buildActiveTarget = true;
                    }
                }
            }

            BuildProfile profile = null;

            if (Application.isBatchMode && profileName != null)
            {
                profile = BuildProfile.Find(profileName);
                if (profile == null)
                {
                    var err = "Build profile named '" + profileName + "' cloud not be found.";
                    if (onComplete != null)
                    {
                        onComplete.OnComplete(false, new[] { ProfileBuildResult.Error(null, err) });
                    }
                    else
                    {
                        Debug.LogError(err);
                    }
                    return;
                }

                Debug.Log("Building " + profile.name + ", selected from command line.");
            }

            if (profile == null)
            {
                if (EditorProfile.Instance.ActiveProfile == null)
                {
                    var err = "No profile specified and not active profile set: Nothing to build";
                    if (onComplete != null)
                    {
                        onComplete.OnComplete(false, new[] { ProfileBuildResult.Error(null, err) });
                    }
                    else
                    {
                        Debug.LogError(err);
                    }
                    return;
                }
                profile = EditorProfile.Instance.ActiveProfile;
                Debug.Log("Building active profile.");
            }

            // Throw if command line build failed to cause non-zero exit code
            if (Application.isBatchMode && onComplete == null)
            {
                onComplete = ScriptableObject.CreateInstance <CommandLineBuildsCompleteListener>();
            }

            BuildRunner.Job[] jobs;
            if (buildActiveTarget)
            {
                jobs = new[] {
                    new BuildRunner.Job(profile, EditorUserBuildSettings.activeBuildTarget, commandLineBuildPath)
                };
            }
            else
            {
                var targets = profile.BuildTargets.ToArray();
                jobs = new BuildRunner.Job[targets.Length];
                for (int i = 0; i < targets.Length; i++)
                {
                    jobs[i] = new BuildRunner.Job()
                    {
                        profile    = profile,
                        target     = targets[i],
                        outputPath = commandLineBuildPath,
                    };
                }
            }

            var runner = ScriptableObject.CreateInstance <BuildRunner>();

            runner.Run(jobs, onComplete, TrimmerPrefs.RestoreActiveBuildTarget && !Application.isBatchMode);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Entry point for Unity Cloud builds.
        /// </summary>
        /// <remarks>
        /// If you don't configure anything, Unity Cloud Build will build without a
        /// profile, all Options will use their default value and will be removed.
        ///
        /// Add the name of the Build Profile you want to build with to the target name
        /// in Unity Cloud Build, enclosed in double underscores, e.g. `__Profile Name__`.
        /// Note that since the target name can contain only alphanumeric characters,
        /// spaces, dashes and underscores, those characters cannot appear in the profile
        /// name either.
        ///
        /// Also note that the ability for Options to set build options is limited,
        /// currently only setting a custom  scene list is supported.
        /// </remarks>
        public static void UnityCloudBuild(BuildManifestObject manifest)
        {
            BuildType = TrimmerBuildType.CloudBuild;

            Debug.Log("UnityCloudBuild: Parsing profile name...");

            // Get profile name from could build target name
            string targetName;

            if (!manifest.TryGetValue("cloudBuildTargetName", out targetName))
            {
                Debug.LogError("Could not get target name from cloud build manifest.");
            }
            else
            {
                var match = Regex.Match(targetName, @"__([\w\-. ]+)__");
                if (match.Success)
                {
                    targetName = match.Groups[1].Value;
                    Debug.Log("Parsed build profile name from target name: " + targetName);
                }
            }

            Debug.Log("UnityCloudBuild: Looking for profile...");

            BuildProfile buildProfile = null;

            if (!string.IsNullOrEmpty(targetName))
            {
                buildProfile = BuildProfile.Find(targetName);
                if (buildProfile == null)
                {
                    Debug.LogError("Build Profile named '" + targetName + "' could not be found.");
                    return;
                }
            }

            if (buildProfile == null)
            {
                Debug.LogWarning("No Build Profile selected. Add the Build Profile enclosed in double underscores (__) to the target name.");
                return;
            }

            Debug.Log("UnityCloudBuild: Running PrepareBuild callbacks...");

            // Prepare build
            var options = GetDefaultOptions(EditorUserBuildSettings.activeBuildTarget);

            currentProfile = buildProfile;

            // Run options' PrepareBuild
            foreach (var option in GetCurrentEditProfile().OrderBy(o => o.PostprocessOrder))
            {
                if ((option.Capabilities & OptionCapabilities.ConfiguresBuild) == 0)
                {
                    continue;
                }
                var inclusion = buildProfile == null ? OptionInclusion.Remove : buildProfile.GetInclusionOf(option, options.target);
                options = option.PrepareBuild(options, inclusion);
            }

            // Cloud Build doesn't allow changing BuildPlayerOptions.extraScriptingDefines,
            // so we have to apply scripting define symbols to player settings
            ApplyScriptingDefineSymbolsToPlayerSettings(buildProfile, options.target);

            Debug.Log("UnityCloudBuild: Apply scenes...");

            // Apply scenes
            if (options.scenes != null && options.scenes.Length > 0)
            {
                var scenes = new EditorBuildSettingsScene[options.scenes.Length];
                for (int i = 0; i < scenes.Length; i++)
                {
                    scenes[i] = new EditorBuildSettingsScene(
                        options.scenes[i],
                        true
                        );
                }
                EditorBuildSettings.scenes = scenes;
            }

            OptionHelper.currentBuildOptions = options;
            Debug.Log("UnityCloudBuild: Done!");
        }