Skip to content

Instantly share code, notes, and snippets.

@lycoris102
Created December 13, 2017 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lycoris102/553a0e60cbaa6b462dc18cba01b127f7 to your computer and use it in GitHub Desktop.
Save lycoris102/553a0e60cbaa6b462dc18cba01b127f7 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class BuildSettingScenesUpdater : AssetPostprocessor
{
private const string sceneDir = "Scenes";
private const string initialLoadScene = "Scenes/Title.unity";
public static void OnPostprocessAllAssets(
string[] importedAssets,
string[] deletedAssets,
string[] movedAssets,
string[] movedFromAssetPaths)
{
if (!CheckExists())
return;
var assets = importedAssets
.Union(deletedAssets)
.Union(movedAssets)
.Union(movedFromAssetPaths);
if (CheckInSceneDir(assets))
{
Create();
}
}
// Sceneディレクトリ以下のアセットが編集されたか
private static bool CheckInSceneDir(IEnumerable<string> assets)
{
return assets.Any(asset => Path.GetDirectoryName(asset) == GetAssetsPath(sceneDir));
}
// 存在チェック
private static bool CheckExists()
{
string sceneDirFullPath = GetFullPath(sceneDir);
string initialLoadSceneFullPath = GetFullPath(initialLoadScene);
if (!Directory.Exists(sceneDirFullPath))
{
Debug.LogError("Not Found Dir :" + sceneDirFullPath);
return false;
}
if (!File.Exists(initialLoadSceneFullPath))
{
Debug.LogError("Not Found Inital Load Scene : " + initialLoadSceneFullPath);
return false;
}
return true;
}
private static void Create()
{
string sceneDirAssetsPath = GetAssetsPath(sceneDir);
string initialLoadSceneAssetsPath = GetAssetsPath(initialLoadScene);
var scenes = AssetDatabase.FindAssets("t:Scene", new string[] {sceneDirAssetsPath})
.Select(guid => AssetDatabase.GUIDToAssetPath(guid))
.OrderBy(path => path)
.Where(path => path != initialLoadSceneAssetsPath)
.Select(path => new EditorBuildSettingsScene(path, true))
.ToList();
// 初回に呼び込まれて欲しいシーンを先頭に配置する
scenes.Insert(0, new EditorBuildSettingsScene(initialLoadSceneAssetsPath, true));
EditorBuildSettings.scenes = scenes.ToArray();
AssetDatabase.SaveAssets();
Debug.Log("Created BuildSettings.");
}
private static string GetFullPath(string path)
{
return Application.dataPath + "/" + path;
}
private static string GetAssetsPath(string path)
{
return "Assets/" + path;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment