Guest User

Untitled

a guest
Dec 15th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEngine;
  6.  
  7. public class BuildSettingScenesUpdater : AssetPostprocessor
  8. {
  9. private const string sceneDir = "Scenes";
  10. private const string initialLoadScene = "Scenes/Title.unity";
  11.  
  12. public static void OnPostprocessAllAssets(
  13. string[] importedAssets,
  14. string[] deletedAssets,
  15. string[] movedAssets,
  16. string[] movedFromAssetPaths)
  17. {
  18. if (!CheckExists())
  19. return;
  20.  
  21. var assets = importedAssets
  22. .Union(deletedAssets)
  23. .Union(movedAssets)
  24. .Union(movedFromAssetPaths);
  25.  
  26. if (CheckInSceneDir(assets))
  27. {
  28. Create();
  29. }
  30. }
  31.  
  32. // Sceneディレクトリ以下のアセットが編集されたか
  33. private static bool CheckInSceneDir(IEnumerable<string> assets)
  34. {
  35. return assets.Any(asset => Path.GetDirectoryName(asset) == GetAssetsPath(sceneDir));
  36. }
  37.  
  38. // 存在チェック
  39. private static bool CheckExists()
  40. {
  41. string sceneDirFullPath = GetFullPath(sceneDir);
  42. string initialLoadSceneFullPath = GetFullPath(initialLoadScene);
  43.  
  44. if (!Directory.Exists(sceneDirFullPath))
  45. {
  46. Debug.LogError("Not Found Dir :" + sceneDirFullPath);
  47. return false;
  48. }
  49.  
  50. if (!File.Exists(initialLoadSceneFullPath))
  51. {
  52. Debug.LogError("Not Found Inital Load Scene : " + initialLoadSceneFullPath);
  53. return false;
  54. }
  55.  
  56. return true;
  57. }
  58.  
  59. private static void Create()
  60. {
  61. string sceneDirAssetsPath = GetAssetsPath(sceneDir);
  62. string initialLoadSceneAssetsPath = GetAssetsPath(initialLoadScene);
  63.  
  64. var scenes = AssetDatabase.FindAssets("t:Scene", new string[] {sceneDirAssetsPath})
  65. .Select(guid => AssetDatabase.GUIDToAssetPath(guid))
  66. .OrderBy(path => path)
  67. .Where(path => path != initialLoadSceneAssetsPath)
  68. .Select(path => new EditorBuildSettingsScene(path, true))
  69. .ToList();
  70.  
  71. // 初回に呼び込まれて欲しいシーンを先頭に配置する
  72. scenes.Insert(0, new EditorBuildSettingsScene(initialLoadSceneAssetsPath, true));
  73.  
  74. EditorBuildSettings.scenes = scenes.ToArray();
  75. AssetDatabase.SaveAssets();
  76.  
  77. Debug.Log("Created BuildSettings.");
  78. }
  79.  
  80. private static string GetFullPath(string path)
  81. {
  82. return Application.dataPath + "/" + path;
  83. }
  84.  
  85. private static string GetAssetsPath(string path)
  86. {
  87. return "Assets/" + path;
  88. }
  89. }
Add Comment
Please, Sign In to add comment