Unity - 自动添加Scene到Build中

using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

[InitializeOnLoad]
public class AutoSceneInBuild : Editor {

	private static readonly string scenesDir = "/Scenes";
	static AutoSceneInBuild() {

		RefreshAllScene();
	}

	[MenuItem("Tool/BuildMainScene")]
	static void RefreshAllScene() {
		string path = Path.Combine(Application.dataPath, scenesDir);
		string[] files = Directory.GetFiles(path, "*.unity", SearchOption.AllDirectories);
		var oriScenes = EditorBuildSettings.scenes.ToList();
		var oriLen = oriScenes.Count;
		for (int i = 0; i < files.Length; ++i) {
			int index = files[i].IndexOf("Assets");
			string _path = files[i].Remove(0, index);

			var exist = false;
			foreach (var item in oriScenes) {
				if (item.path == _path) {
					exist = true;
					break;
				}
			}
			if (!exist) {
				oriScenes.Add(new EditorBuildSettingsScene(_path, true));
			}
		}
		EditorBuildSettings.scenes = oriScenes.ToArray();
	}
}

你可能感兴趣的:(Unity,unity,游戏引擎)