Esempio n. 1
0
        private static void ConfigureEnvironment(BuildReleaseType releaseType, BuildPlatform platform, BuildArchitecture architecture, BuildDistribution distribution, DateTime buildTime)
        {
            // Switch to target build platform
            EditorUserBuildSettings.SwitchActiveBuildTarget(platform.targetGroup, architecture.target);

            // Apply defines
            string preBuildDefines = PlayerSettings.GetScriptingDefineSymbolsForGroup(platform.targetGroup);
            string buildDefines    = GenerateDefaultDefines(releaseType, platform, architecture, distribution);

            buildDefines = MergeDefines(preBuildDefines, buildDefines);

            PlayerSettings.SetScriptingDefineSymbolsForGroup(platform.targetGroup, buildDefines);

            // Set target settings
            PlayerSettings.companyName = releaseType.companyName;
            PlayerSettings.productName = releaseType.productName;

            // Set bundle info
            PlayerSettings.SetApplicationIdentifier(platform.targetGroup, releaseType.bundleIdentifier);

            // Apply build variant
            platform.ApplyVariant();

            // Generate BuildConstants
            BuildConstantsGenerator.Generate(buildTime, BuildSettings.productParameters.buildVersion, releaseType, platform, architecture, distribution);

            // Refresh scene list to make sure nothing has been deleted or moved
            releaseType.sceneList.Refresh();
        }
Esempio n. 2
0
        public static string GenerateDefaultDefines(BuildReleaseType releaseType, BuildPlatform buildPlatform, BuildArchitecture arch, BuildDistribution dist)
        {
            List <string> defines = new List <string>();

            if (releaseType != null)
            {
                defines.Add(BUILD_TYPE + SanitizeCodeString(releaseType.typeName.ToUpper().Replace(" ", "")));
            }

            if (buildPlatform != null)
            {
                defines.Add(BUILD_PLATFORM + SanitizeCodeString(buildPlatform.platformName.ToUpper().Replace(" ", "")));
            }

            if (arch != null)
            {
                defines.Add(BUILD_ARCH + SanitizeCodeString(arch.name.ToUpper().Replace(" ", "")));
            }

            if (dist != null)
            {
                defines.Add(BUILD_DIST + SanitizeCodeString(dist.distributionName.ToUpper().Replace(" ", "")));
            }

            if (releaseType != null && !string.IsNullOrEmpty(releaseType.customDefines))
            {
                string[] customDefines = releaseType.customDefines.Split(';', ',');
                for (int i = 0; i < customDefines.Length; i++)
                {
                    defines.Add(SanitizeCodeString(customDefines[i]).ToUpper());
                }
            }

            return(string.Join(";", defines.ToArray()));
        }
Esempio n. 3
0
        public static string ResolvePath(string prototype, BuildReleaseType releaseType, BuildPlatform buildPlatform, BuildArchitecture arch, BuildDistribution dist, DateTime buildTime)
        {
            StringBuilder sb = new StringBuilder(prototype);

            sb.Replace("$YEAR", buildTime.ToString("yyyy"));
            sb.Replace("$MONTH", buildTime.ToString("MM"));
            sb.Replace("$DAY", buildTime.ToString("dd"));
            sb.Replace("$TIME", buildTime.ToString("hhmmss"));

            string archName = arch.name;

            if (buildPlatform.variants != null && buildPlatform.variants.Length > 0)
            {
                archName += " (" + buildPlatform.variantKey.Replace(",", ", ") + ")";
            }

            sb.Replace("$RELEASE_TYPE", SanitizeFolderName(releaseType.typeName));
            sb.Replace("$PLATFORM", SanitizeFolderName(buildPlatform.platformName));
            sb.Replace("$ARCHITECTURE", SanitizeFolderName(archName));
            sb.Replace("$DISTRIBUTION", SanitizeFolderName(dist != null ? dist.distributionName : BuildConstantsGenerator.NONE));
            sb.Replace("$VERSION", SanitizeFolderName(BuildSettings.productParameters.buildVersion));
            sb.Replace("$BUILD", BuildSettings.productParameters.buildCounter.ToString());
            sb.Replace("$PRODUCT_NAME", SanitizeFolderName(releaseType.productName));

            return(sb.ToString());
        }
Esempio n. 4
0
 /// <summary>
 /// This will be executed before/after each individual player is built.
 /// </summary>
 public virtual void PerBuildExecute(
     BuildReleaseType releaseType,
     BuildPlatform platform,
     BuildArchitecture architecture,
     BuildDistribution distribution,
     System.DateTime buildTime, ref BuildOptions options, string configKey, string buildPath)
 {
 }
Esempio n. 5
0
        public static string GenerateBuildPath(string prototype, BuildReleaseType releaseType, BuildPlatform buildPlatform, BuildArchitecture arch, BuildDistribution dist, DateTime buildTime)
        {
            string resolvedProto = ResolvePath(prototype, releaseType, buildPlatform, arch, dist, buildTime);
            string buildPath     = Path.Combine(BuildSettings.basicSettings.baseBuildFolder, resolvedProto);

            buildPath = Path.GetFullPath(buildPath).TrimEnd('\\').TrimEnd('/');

            return(buildPath);
        }
Esempio n. 6
0
        public static void SetEditorBuildSettingsScenes(BuildReleaseType releaseType)
        {
            // Create EditorBuildSettingsScene instances from release type scene list
            List <EditorBuildSettingsScene> editorBuildSettingsScenes = releaseType.sceneList.GetSceneFileList()
                                                                        .Select(path => new EditorBuildSettingsScene(path, true))
                                                                        .ToList();

            // Set the Build Settings scene list
            EditorBuildSettings.scenes = editorBuildSettingsScenes.ToArray();
        }
Esempio n. 7
0
 private static void PerformPostBuild(
     BuildReleaseType releaseType,
     BuildPlatform platform,
     BuildArchitecture architecture,
     BuildDistribution distribution,
     DateTime buildTime, ref BuildOptions options, string configKey, string buildPath)
 {
     PerformBuildActions(
         releaseType,
         platform,
         architecture,
         distribution,
         buildTime, ref options, configKey, buildPath, BuildSettings.postBuildActions.buildActions, "Post-"
         );
 }
Esempio n. 8
0
        private static void PerformPostBuild(
            BuildReleaseType releaseType,
            BuildPlatform platform,
            BuildArchitecture architecture,
            BuildDistribution distribution,
            DateTime buildTime, ref BuildOptions options, string configKey, string buildPath)
        {
            BuildAction[] buildActions = BuildSettings.postBuildActions.buildActions;

            if (buildActions != null)
            {
                for (int i = 0; i < buildActions.Length; i++)
                {
                    BuildAction action = buildActions[i];

                    // Check if execute method has been overriden.
                    MethodInfo m = action.GetType().GetMethod("PerBuildExecute");

                    if (m.GetBaseDefinition().DeclaringType != m.DeclaringType && action.actionType == BuildAction.ActionType.PerPlatform)
                    {
                        // Check build filter and execute if true.
                        if (action.filter == null || action.filter.Evaluate(releaseType, platform, architecture, distribution, configKey) && action.actionEnabled)
                        {
                            BuildNotificationList.instance.AddNotification(new BuildNotification(
                                                                               BuildNotification.Category.Notification,
                                                                               string.Format("Performing Post-Build Action ({0}/{1}).", i + 1, buildActions.Length), string.Format("{0}: {1}", action.actionName, configKey),
                                                                               true, null));

                            action.PerBuildExecute(releaseType, platform, architecture, distribution, buildTime, ref options, configKey, buildPath);
                        }
                        else
                        {
                            BuildNotificationList.instance.AddNotification(new BuildNotification(
                                                                               BuildNotification.Category.Notification,
                                                                               string.Format("Skipping Post-Build Action ({0}/{1}).", i + 1, buildActions.Length), string.Format("{0}: {1}", action.actionName, configKey),
                                                                               true, null));
                        }
                    }
                }
            }
        }
Esempio n. 9
0
        public static void Generate(
            DateTime buildTime,
            string currentVersion = "",
            BuildReleaseType currentReleaseType        = null,
            BuildPlatform currentBuildPlatform         = null,
            BuildArchitecture currentBuildArchitecture = null,
            BuildDistribution currentBuildDistribution = null)
        {
            // Find the BuildConstants file.
            string currentFilePath = FindFile();
            string filePath        = !string.IsNullOrEmpty(currentFilePath) ? currentFilePath : Path.Combine(Constants.AssetsDirectoryName, DefaultFilePath);

            // Generate strings
            string versionString      = currentVersion;
            string releaseTypeString  = currentReleaseType == null ? NONE : SanitizeString(currentReleaseType.typeName);
            string platformString     = currentBuildPlatform == null ? NONE : SanitizeString(currentBuildPlatform.platformName);
            string archString         = currentBuildArchitecture == null ? NONE : SanitizeString(currentBuildArchitecture.name);
            string distributionString = currentBuildDistribution == null ? NONE : SanitizeString(currentBuildDistribution.distributionName);

            // Delete any existing version.
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            // Create a buffer that we'll use to check for any duplicated names.
            List <string> enumBuffer = new List <string>();

            AssetDatabaseUtility.EnsureDirectoriesExist();

            using (StreamWriter writer = new StreamWriter(filePath))
            {
                // Start of file and class.
                writer.WriteLine("// This file is auto-generated. Do not modify or move this file.");
                writer.WriteLine();
                writer.WriteLine("public static class BuildConstants");
                writer.WriteLine("{");

                // Write ReleaseType enum.
                writer.WriteLine("    public enum ReleaseType");
                writer.WriteLine("    {");
                writer.WriteLine("        {0},", NONE);
                enumBuffer.Add(NONE);
                foreach (BuildReleaseType releaseType in BuildSettings.releaseTypeList.releaseTypes)
                {
                    string addedString = SanitizeString(releaseType.typeName);

                    if (!enumBuffer.Contains(addedString))
                    {
                        enumBuffer.Add(addedString);
                        writer.WriteLine("        {0},", addedString);
                    }
                }
                writer.WriteLine("    }");
                writer.WriteLine();

                // Validate ReleaseType string.
                if (!enumBuffer.Contains(releaseTypeString))
                {
                    releaseTypeString = NONE;
                }

                // Write Platform enum.
                enumBuffer.Clear();
                writer.WriteLine("    public enum Platform");
                writer.WriteLine("    {");
                writer.WriteLine("        {0},", NONE);
                enumBuffer.Add(NONE);
                foreach (BuildPlatform platform in BuildSettings.platformList.platforms)
                {
                    string addedString = SanitizeString(platform.platformName);

                    if (platform.enabled && !enumBuffer.Contains(addedString))
                    {
                        enumBuffer.Add(addedString);
                        writer.WriteLine("        {0},", addedString);
                    }
                }
                writer.WriteLine("    }");
                writer.WriteLine();

                // Validate Platform string.
                if (!enumBuffer.Contains(platformString))
                {
                    platformString = NONE;
                }

                // Write Architecture enum.
                enumBuffer.Clear();
                writer.WriteLine("    public enum Architecture");
                writer.WriteLine("    {");
                writer.WriteLine("        {0},", NONE);
                enumBuffer.Add(NONE);
                foreach (BuildPlatform platform in BuildSettings.platformList.platforms)
                {
                    if (platform.enabled)
                    {
                        foreach (BuildArchitecture arch in platform.architectures)
                        {
                            string addedString = SanitizeString(arch.name);

                            if (arch.enabled && !enumBuffer.Contains(addedString))
                            {
                                enumBuffer.Add(addedString);
                                writer.WriteLine("        {0},", addedString);
                            }
                        }
                    }
                }
                writer.WriteLine("    }");
                writer.WriteLine();

                // Validate Architecture string.
                if (!enumBuffer.Contains(archString))
                {
                    archString = NONE;
                }

                // Write Distribution enum.
                enumBuffer.Clear();
                writer.WriteLine("    public enum Distribution");
                writer.WriteLine("    {");
                writer.WriteLine("        {0},", NONE);
                enumBuffer.Add(NONE);
                foreach (BuildPlatform platform in BuildSettings.platformList.platforms)
                {
                    if (platform.enabled)
                    {
                        foreach (BuildDistribution dist in platform.distributionList.distributions)
                        {
                            string addedString = SanitizeString(dist.distributionName);

                            if (dist.enabled && !enumBuffer.Contains(addedString))
                            {
                                enumBuffer.Add(addedString);
                                writer.WriteLine("        {0},", addedString);
                            }
                        }
                    }
                }
                writer.WriteLine("    }");
                writer.WriteLine();

                // Validate Distribution string.
                if (!enumBuffer.Contains(distributionString))
                {
                    distributionString = NONE;
                }

                // Write current values.
                writer.WriteLine("    public static readonly System.DateTime buildDate = new System.DateTime({0});", buildTime.Ticks);
                writer.WriteLine("    public const string version = \"{0}\";", versionString);
                writer.WriteLine("    public const ReleaseType releaseType = ReleaseType.{0};", releaseTypeString);
                writer.WriteLine("    public const Platform platform = Platform.{0};", platformString);
                writer.WriteLine("    public const Architecture architecture = Architecture.{0};", archString);
                writer.WriteLine("    public const Distribution distribution = Distribution.{0};", distributionString);

                // End of class.
                writer.WriteLine("}");
                writer.WriteLine();
            }

            // Refresh AssetDatabse so that changes take effect.
            AssetDatabase.Refresh();
        }
Esempio n. 10
0
        public bool ParseKeychain(string keychain, out BuildReleaseType releaseType, out BuildPlatform platform, out BuildArchitecture architecture, out BuildDistribution distribution)
        {
            bool success = false;

            string[] keys      = keychain.Split('/');
            int      keyCount  = keys.Length;
            int      targetKey = 0;

            releaseType  = null;
            platform     = null;
            architecture = null;
            distribution = null;

            // Parse release type.
            if (keyCount > targetKey)
            {
                for (int i = 0; i < BuildSettings.releaseTypeList.releaseTypes.Length; i++)
                {
                    BuildReleaseType rt = BuildSettings.releaseTypeList.releaseTypes[i];

                    if (keys[targetKey] == rt.typeName)
                    {
                        releaseType = rt;
                        break;
                    }
                }
            }

            if (releaseType == null)
            {
                return(false);
            }

            // Parse platform.
            if (keyCount > ++targetKey)
            {
                // Scan ahead and try to parse a variant key.
                string variantKey = "";
                if (keys[targetKey + 1].Contains("("))
                {
                    int startIndex = keys[targetKey + 1].IndexOf('(');
                    int endIndex   = keys[targetKey + 1].IndexOf(')');
                    variantKey = keys[targetKey + 1].Substring(startIndex + 1, endIndex - startIndex - 1);

                    keys[targetKey + 1] = keys[targetKey + 1].Remove(startIndex).Trim();
                }

                for (int i = 0; i < BuildSettings.platformList.platforms.Count; i++)
                {
                    BuildPlatform p = BuildSettings.platformList.platforms[i];

                    if (keys[targetKey] == p.platformName && p.variantKey == variantKey)
                    {
                        platform = p;
                        break;
                    }
                }
            }

            if (platform == null)
            {
                return(false);
            }

            // Parse architecture.
            if (platform.architectures.Length == 1)
            {
                // Only one architecture, so it won't even appear in dictionary. Just get it directly.
                ++targetKey;
                architecture = platform.architectures[0];
                success      = true;
            }
            else if (keyCount > ++targetKey)
            {
                for (int i = 0; i < platform.architectures.Length; i++)
                {
                    BuildArchitecture arch = platform.architectures[i];

                    if (keys[targetKey] == arch.name)
                    {
                        architecture = arch;
                        success      = true;
                        break;
                    }
                }
            }

            if (architecture == null)
            {
                return(false);
            }

            // TODO: Parse variants.

            // Parse distribution.
            if (keyCount > ++targetKey)
            {
                success = false;
                for (int i = 0; i < platform.distributionList.distributions.Length; i++)
                {
                    BuildDistribution dist = platform.distributionList.distributions[i];

                    if (keys[targetKey] == dist.distributionName)
                    {
                        distribution = dist;
                        success      = true;
                        break;
                    }
                }
            }

            return(success);
        }
Esempio n. 11
0
        private static bool BuildPlayer(string notification, BuildReleaseType releaseType, BuildPlatform platform, BuildArchitecture architecture, BuildDistribution distribution, DateTime buildTime, BuildOptions options, string configKey)
        {
            bool success = true;

            if (options == BuildOptions.None)
            {
                options = releaseType.buildOptions;
            }

            // Generate build path
            string buildPath = GenerateBuildPath(BuildSettings.basicSettings.buildPath, releaseType, platform, architecture, distribution, buildTime);
            string binName   = string.Format(architecture.binaryNameFormat, SanitizeFileName(releaseType.productName));

            // Save current environment settings
            string preBuildDefines          = PlayerSettings.GetScriptingDefineSymbolsForGroup(platform.targetGroup);
            string preBuildCompanyName      = PlayerSettings.companyName;
            string preBuildProductName      = PlayerSettings.productName;
            string preBuildBundleIdentifier = PlayerSettings.GetApplicationIdentifier(platform.targetGroup);

            // Configure environment settings to match the build configuration
            ConfigureEnvironment(releaseType, platform, architecture, distribution, buildTime);

            // Pre-build actions
            PerformPreBuild(releaseType, platform, architecture, distribution, buildTime, ref options, configKey, buildPath);

            // Report build starting
            BuildNotificationList.instance.AddNotification(new BuildNotification(
                                                               BuildNotification.Category.Notification,
                                                               notification, configKey,
                                                               true, null));

            // Build player
            FileUtil.DeleteFileOrDirectory(buildPath);

            string error = "";

            UnityEditor.Build.Reporting.BuildReport buildReport = BuildPipeline.BuildPlayer(releaseType.sceneList.GetSceneFileList(), Path.Combine(buildPath, binName), architecture.target, options);
            if (buildReport.summary.result == UnityEditor.Build.Reporting.BuildResult.Failed)
            {
                error = buildReport.summary.totalErrors + " occurred.";
            }

            if (!string.IsNullOrEmpty(error))
            {
                success = false;

                BuildNotificationList.instance.AddNotification(new BuildNotification(
                                                                   BuildNotification.Category.Error,
                                                                   "Build Failed:", configKey.ToString() + "\n" + error,
                                                                   true, null));
            }

            // Post-build actions
            PerformPostBuild(releaseType, platform, architecture, distribution, buildTime, ref options, configKey, buildPath);

            // Restore pre-build environment settings
            PlayerSettings.SetScriptingDefineSymbolsForGroup(platform.targetGroup, preBuildDefines);
            PlayerSettings.companyName = preBuildCompanyName;
            PlayerSettings.productName = preBuildProductName;
            PlayerSettings.SetApplicationIdentifier(platform.targetGroup, preBuildBundleIdentifier);

            return(success);
        }
Esempio n. 12
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(position, label, property);

            EditorGUILayout.BeginHorizontal();

            bool show = property.isExpanded;

            UnityBuildGUIUtility.DropdownHeader("Scene List", ref show, false, GUILayout.ExpandWidth(true));
            property.isExpanded = show;

            EditorGUILayout.EndHorizontal();

            // Refresh all scene lists.
            for (int i = 0; i < BuildSettings.releaseTypeList.releaseTypes.Length; i++)
            {
                BuildReleaseType rt = BuildSettings.releaseTypeList.releaseTypes[i];
                rt.sceneList.Refresh();
            }

            list = property.FindPropertyRelative("enabledScenes");

            if (show)
            {
                EditorGUILayout.BeginVertical(UnityBuildGUIUtility.dropdownContentStyle);

                SerializedProperty platformProperty;
                string             fileGUID;
                string             filePath;
                string             sceneName = "N/A";
                if (list.arraySize > 0)
                {
                    platformProperty = list.GetArrayElementAtIndex(0);
                    fileGUID         = platformProperty.FindPropertyRelative("fileGUID").stringValue;
                    filePath         = AssetDatabase.GUIDToAssetPath(fileGUID);
                    sceneName        = Path.GetFileNameWithoutExtension(filePath);
                }

                EditorGUILayout.BeginHorizontal();

                show = list.isExpanded;
                UnityBuildGUIUtility.DropdownHeader(string.Format("Scenes ({0}) (First Scene: {1})", list.arraySize, sceneName), ref show, false, GUILayout.ExpandWidth(true));
                list.isExpanded = show;

                EditorGUILayout.EndHorizontal();

                if (show)
                {
                    for (int i = 0; i < list.arraySize; i++)
                    {
                        platformProperty = list.GetArrayElementAtIndex(i);
                        fileGUID         = platformProperty.FindPropertyRelative("fileGUID").stringValue;
                        filePath         = AssetDatabase.GUIDToAssetPath(fileGUID);
                        sceneName        = Path.GetFileNameWithoutExtension(filePath);

                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.TextArea(sceneName + " (" + filePath + ")");

                        EditorGUI.BeginDisabledGroup(i == 0);
                        if (GUILayout.Button("↑↑", UnityBuildGUIUtility.helpButtonStyle))
                        {
                            list.MoveArrayElement(i, 0);
                        }
                        if (GUILayout.Button("↑", UnityBuildGUIUtility.helpButtonStyle))
                        {
                            list.MoveArrayElement(i, i - 1);
                        }
                        EditorGUI.EndDisabledGroup();

                        EditorGUI.BeginDisabledGroup(i == list.arraySize - 1);
                        if (GUILayout.Button("↓", UnityBuildGUIUtility.helpButtonStyle))
                        {
                            list.MoveArrayElement(i, i + 1);
                        }
                        EditorGUI.EndDisabledGroup();

                        if (GUILayout.Button("X", UnityBuildGUIUtility.helpButtonStyle))
                        {
                            list.DeleteArrayElementAtIndex(i);
                        }

                        property.serializedObject.ApplyModifiedProperties();

                        EditorGUILayout.EndHorizontal();
                    }
                }

                GUILayout.Space(20);

                Rect dropArea = GUILayoutUtility.GetRect(0, 50.0f, GUILayout.ExpandWidth(true));
                GUI.Box(dropArea, "Drag and Drop scene files here to add to list.", UnityBuildGUIUtility.dragDropStyle);
                Event currentEvent = Event.current;

                switch (currentEvent.type)
                {
                case EventType.DragUpdated:
                case EventType.DragPerform:
                    if (dropArea.Contains(currentEvent.mousePosition))
                    {
                        DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                        if (currentEvent.type == EventType.DragPerform)
                        {
                            DragAndDrop.AcceptDrag();

                            foreach (Object obj in DragAndDrop.objectReferences)
                            {
                                if (obj.GetType() == typeof(SceneAsset))
                                {
                                    string objFilepath = AssetDatabase.GetAssetPath(obj);
                                    string objGUID     = AssetDatabase.AssetPathToGUID(objFilepath);
                                    AddScene(objGUID);
                                }
                            }

                            if (list.arraySize >= AUTO_COLLAPSE_SIZE)
                            {
                                list.isExpanded = false;
                            }
                        }
                    }
                    break;
                }

                if (GUILayout.Button("Clear Scene List", GUILayout.ExpandWidth(true)))
                {
                    list.ClearArray();
                }

                if (GUILayout.Button("Add Scene Files from Build Settings", GUILayout.ExpandWidth(true)))
                {
                    GetSceneFilesFromBuildSettings();
                }

                if (GUILayout.Button("Add Scene File Directory", GUILayout.ExpandWidth(true)))
                {
                    GetSceneFileDirectory("Add Scene Files");
                }

                if (GUILayout.Button("Set First Scene by File", GUILayout.ExpandWidth(true)))
                {
                    SetFirstSceneByFile();
                }

                list.serializedObject.ApplyModifiedProperties();
                property.serializedObject.ApplyModifiedProperties();

                EditorGUILayout.EndVertical();
            }

            EditorGUI.EndProperty();
        }