コード例 #1
0
        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);
        }
コード例 #2
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();
        }