protected static T CreateAsset <T>(string assetName) where T : BaseSettings
        {
            // Try to load an existing settings asset at the path specified in EditorPrefs, or fallback to a default path
            string assetsRoot       = Path.Combine(Constants.AssetsDirectoryName, Constants.RootDirectoryName);
            string settingsRoot     = Path.Combine(assetsRoot, Constants.SettingsDirectoryName);
            string defaultAssetPath = Path.Combine(settingsRoot, string.Format("{0}.asset", assetName));
            string prefsAssetPath   = EditorPrefs.HasKey(SettingsPrefsKey) ?
                                      EditorPrefs.GetString(SettingsPrefsKey, defaultAssetPath) :
                                      defaultAssetPath;
            string assetPath = File.Exists(prefsAssetPath) ? prefsAssetPath : defaultAssetPath;

            T instance = AssetDatabase.LoadAssetAtPath <T>(assetPath) as T;

            if (instance == null)
            {
                Debug.Log("SuperUnityBuild: Creating settings file - " + defaultAssetPath);
                instance      = CreateInstance <T>();
                instance.name = assetName;

                AssetDatabaseUtility.EnsureDirectoriesExist();

                AssetDatabase.CreateAsset(instance, defaultAssetPath);
            }

            return(instance);
        }
Exemple #2
0
        private void DrawActionList(SerializedProperty property, List <Type> actionTypes)
        {
            if (actionTypes.Count > 0)
            {
                EditorGUILayout.BeginHorizontal();

                string[] buildActionNameList = new string[actionTypes.Count];
                for (int i = 0; i < buildActionNameList.Length; i++)
                {
                    buildActionNameList[i] = actionTypes[i].Name;
                }

                index = EditorGUILayout.Popup(index, buildActionNameList, UnityBuildGUIUtility.popupStyle, GUILayout.ExpandWidth(true));
                if (GUILayout.Button("Add Build Action", GUILayout.ExpandWidth(false), GUILayout.MaxWidth(150)) && index < actionTypes.Count)
                {
                    Type addedType = actionTypes[index];

                    int addedIndex = list.arraySize;
                    list.InsertArrayElementAtIndex(addedIndex);
                    list.serializedObject.ApplyModifiedProperties();

                    BuildAction[] buildActions;
                    if (property.name.ToUpper().Contains("PRE"))
                    {
                        buildActions = BuildSettings.preBuildActions.buildActions;
                    }
                    else
                    {
                        buildActions = BuildSettings.postBuildActions.buildActions;
                    }

                    buildActions[addedIndex]            = ScriptableObject.CreateInstance(addedType) as BuildAction;
                    buildActions[addedIndex].name       = addedType.Name;
                    buildActions[addedIndex].actionName = addedType.Name;
                    buildActions[addedIndex].filter     = new BuildFilter();

                    AssetDatabase.AddObjectToAsset(buildActions[addedIndex], BuildSettings.instance);
                    AssetDatabaseUtility.ImportAsset(AssetDatabase.GetAssetPath(BuildSettings.instance));

                    index = 0;
                }

                EditorGUILayout.EndHorizontal();
            }
            else
            {
                EditorGUILayout.HelpBox("No Build Actions found.", MessageType.Info);
            }
        }
Exemple #3
0
        private void DrawPlatforms(SerializedProperty property)
        {
            for (int i = 0; i < list.arraySize; i++)
            {
                SerializedProperty listEntry = list.GetArrayElementAtIndex(i);

                BuildPlatform buildPlatform = listEntry.objectReferenceValue as BuildPlatform;
                if (buildPlatform == null)
                {
                    list.DeleteArrayElementAtIndex(i);
                    --i;
                    continue;
                }

                SerializedObject serializedBuildPlatform = new SerializedObject(buildPlatform);

                EditorGUILayout.BeginHorizontal();
                bool show = listEntry.isExpanded;

                UnityBuildGUIUtility.DropdownHeader(buildPlatform.platformName, ref show, false, GUILayout.ExpandWidth(true));

                listEntry.isExpanded = show;

                if (GUILayout.Button("X", UnityBuildGUIUtility.helpButtonStyle))
                {
                    List <BuildPlatform> buildPlatforms = BuildSettings.platformList.platforms;

                    // Destroy underlying object.
                    ScriptableObject.DestroyImmediate(buildPlatforms[i], true);
                    AssetDatabaseUtility.ImportAsset(AssetDatabase.GetAssetPath(BuildSettings.instance));

                    // Remove object reference from list.
                    // TODO: Why do I need to call this twice? First call nulls reference, second one then deletes null entry.
                    list.DeleteArrayElementAtIndex(i);
                    list.DeleteArrayElementAtIndex(i);
                    show = false;
                }

                EditorGUILayout.EndHorizontal();

                if (show && buildPlatform.enabled)
                {
                    EditorGUILayout.BeginVertical(UnityBuildGUIUtility.dropdownContentStyle);
                    buildPlatform.Draw(serializedBuildPlatform);
                    EditorGUILayout.EndVertical();
                }
            }
        }
Exemple #4
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();
        }
Exemple #5
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(position, label, property);

            EditorGUILayout.BeginHorizontal();
            property.serializedObject.Update();

            bool show = property.isExpanded;

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

            UnityBuildGUIUtility.HelpButton("Parameter-Details#build-platforms");
            EditorGUILayout.EndHorizontal();

            platformList = fieldInfo.GetValue(property.serializedObject.targetObject) as BuildPlatformList;
            PopulateAvailablePlatforms();
            list = property.FindPropertyRelative("platforms");
            list.serializedObject.Update();

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

                DrawPlatforms(property);

                if (list.arraySize > 0)
                {
                    GUILayout.Space(20);
                }

                // Draw all available platforms.
                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                index = EditorGUILayout.Popup(index, availablePlatformNameList.ToArray(), UnityBuildGUIUtility.popupStyle, GUILayout.ExpandWidth(false), GUILayout.MaxWidth(250));
                if (GUILayout.Button("Add Platform", GUILayout.ExpandWidth(false), GUILayout.MaxWidth(150)))
                {
                    BuildPlatform addedBuildPlatform = ScriptableObject.CreateInstance(availablePlatformTypeList[index]) as BuildPlatform;
                    platformList.platforms.Add(addedBuildPlatform);

                    AssetDatabase.AddObjectToAsset(addedBuildPlatform, BuildSettings.instance);
                    AssetDatabaseUtility.ImportAsset(AssetDatabase.GetAssetPath(BuildSettings.instance));

                    platformList.platforms[platformList.platforms.Count - 1].enabled = true;

                    for (int i = platformList.platforms.Count - 1; i >= 0; i--)
                    {
                        if (!platformList.platforms[i].enabled)
                        {
                            platformList.platforms.RemoveAt(i);
                        }
                    }
                }
                GUILayout.FlexibleSpace();
                GUILayout.EndHorizontal();

                EditorGUILayout.EndVertical();
            }

            EditorGUI.EndProperty();
        }
Exemple #6
0
        private void DrawActions(SerializedProperty property)
        {
            for (int i = 0; i < list.arraySize; i++)
            {
                SerializedProperty listEntry = list.GetArrayElementAtIndex(i);

                BuildAction buildAction = listEntry.objectReferenceValue as BuildAction;
                if (buildAction == null)
                {
                    list.DeleteArrayElementAtIndex(i);
                    --i;
                    continue;
                }

                SerializedObject serializedBuildAction = new SerializedObject(buildAction);

                EditorGUILayout.BeginHorizontal();
                bool show = listEntry.isExpanded;

                buildAction.actionEnabled = EditorGUILayout.Toggle(buildAction.actionEnabled, GUILayout.Width(15));
                EditorGUI.BeginDisabledGroup(!buildAction.actionEnabled);
                UnityBuildGUIUtility.DropdownHeader(buildAction.actionName, ref show, false, GUILayout.ExpandWidth(true));
                EditorGUI.EndDisabledGroup();
                listEntry.isExpanded = show;

                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))
                {
                    BuildAction[] buildActions;
                    if (property.name.ToUpper().Contains("PRE"))
                    {
                        buildActions = BuildSettings.preBuildActions.buildActions;
                    }
                    else
                    {
                        buildActions = BuildSettings.postBuildActions.buildActions;
                    }

                    // Destroy underlying object.
                    ScriptableObject.DestroyImmediate(buildActions[i], true);
                    AssetDatabaseUtility.ImportAsset(AssetDatabase.GetAssetPath(BuildSettings.instance));

                    // Remove object reference from list.
                    // TODO: Why do I need to call this twice? First call nulls reference, second one then deletes null entry.
                    list.DeleteArrayElementAtIndex(i);
                    list.DeleteArrayElementAtIndex(i);
                    show = false;
                }

                EditorGUILayout.EndHorizontal();

                if (show && buildAction.actionEnabled)
                {
                    EditorGUILayout.BeginVertical(UnityBuildGUIUtility.dropdownContentStyle);
                    buildAction.Draw(serializedBuildAction);
                    EditorGUILayout.EndVertical();
                }
            }
        }