BuildSettings.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using GameFramework;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Xml;
  11. using UnityEditor;
  12. using UnityEngine;
  13. namespace UnityGameFramework.Editor
  14. {
  15. /// <summary>
  16. /// 构建配置相关的实用函数。
  17. /// </summary>
  18. internal static class BuildSettings
  19. {
  20. private static readonly string s_ConfigurationPath = null;
  21. private static readonly List<string> s_DefaultSceneNames = new List<string>();
  22. private static readonly List<string> s_SearchScenePaths = new List<string>();
  23. static BuildSettings()
  24. {
  25. s_ConfigurationPath = Type.GetConfigurationPath<BuildSettingsConfigPathAttribute>() ?? Utility.Path.GetRegularPath(Path.Combine(Application.dataPath, "GameFramework/Configs/BuildSettings.xml"));
  26. s_DefaultSceneNames.Clear();
  27. s_SearchScenePaths.Clear();
  28. if (!File.Exists(s_ConfigurationPath))
  29. {
  30. return;
  31. }
  32. try
  33. {
  34. XmlDocument xmlDocument = new XmlDocument();
  35. xmlDocument.Load(s_ConfigurationPath);
  36. XmlNode xmlRoot = xmlDocument.SelectSingleNode("UnityGameFramework");
  37. XmlNode xmlBuildSettings = xmlRoot.SelectSingleNode("BuildSettings");
  38. XmlNode xmlDefaultScenes = xmlBuildSettings.SelectSingleNode("DefaultScenes");
  39. XmlNode xmlSearchScenePaths = xmlBuildSettings.SelectSingleNode("SearchScenePaths");
  40. XmlNodeList xmlNodeList = null;
  41. XmlNode xmlNode = null;
  42. xmlNodeList = xmlDefaultScenes.ChildNodes;
  43. for (int i = 0; i < xmlNodeList.Count; i++)
  44. {
  45. xmlNode = xmlNodeList.Item(i);
  46. if (xmlNode.Name != "DefaultScene")
  47. {
  48. continue;
  49. }
  50. string defaultSceneName = xmlNode.Attributes.GetNamedItem("Name").Value;
  51. s_DefaultSceneNames.Add(defaultSceneName);
  52. }
  53. xmlNodeList = xmlSearchScenePaths.ChildNodes;
  54. for (int i = 0; i < xmlNodeList.Count; i++)
  55. {
  56. xmlNode = xmlNodeList.Item(i);
  57. if (xmlNode.Name != "SearchScenePath")
  58. {
  59. continue;
  60. }
  61. string searchScenePath = xmlNode.Attributes.GetNamedItem("Path").Value;
  62. s_SearchScenePaths.Add(searchScenePath);
  63. }
  64. }
  65. catch
  66. {
  67. }
  68. }
  69. /// <summary>
  70. /// 将构建场景设置为默认。
  71. /// </summary>
  72. [MenuItem("Game Framework/Scenes in Build Settings/Default Scenes", false, 20)]
  73. public static void DefaultScenes()
  74. {
  75. HashSet<string> sceneNames = new HashSet<string>();
  76. foreach (string sceneName in s_DefaultSceneNames)
  77. {
  78. sceneNames.Add(sceneName);
  79. }
  80. List<EditorBuildSettingsScene> scenes = new List<EditorBuildSettingsScene>();
  81. foreach (string sceneName in sceneNames)
  82. {
  83. scenes.Add(new EditorBuildSettingsScene(sceneName, true));
  84. }
  85. EditorBuildSettings.scenes = scenes.ToArray();
  86. Debug.Log("Set scenes of build settings to default scenes.");
  87. }
  88. /// <summary>
  89. /// 将构建场景设置为所有。
  90. /// </summary>
  91. [MenuItem("Game Framework/Scenes in Build Settings/All Scenes", false, 21)]
  92. public static void AllScenes()
  93. {
  94. HashSet<string> sceneNames = new HashSet<string>();
  95. foreach (string sceneName in s_DefaultSceneNames)
  96. {
  97. sceneNames.Add(sceneName);
  98. }
  99. string[] sceneGuids = AssetDatabase.FindAssets("t:Scene", s_SearchScenePaths.ToArray());
  100. foreach (string sceneGuid in sceneGuids)
  101. {
  102. string sceneName = AssetDatabase.GUIDToAssetPath(sceneGuid);
  103. sceneNames.Add(sceneName);
  104. }
  105. List<EditorBuildSettingsScene> scenes = new List<EditorBuildSettingsScene>();
  106. foreach (string sceneName in sceneNames)
  107. {
  108. scenes.Add(new EditorBuildSettingsScene(sceneName, true));
  109. }
  110. EditorBuildSettings.scenes = scenes.ToArray();
  111. Debug.Log("Set scenes of build settings to all scenes.");
  112. }
  113. }
  114. }