/*---------------------------------------
*| Describe :
*| Author : 人生若只如初見
*| Version : 1.0
*| Time : 2022.12.26 12:08
*---------------------------------------
*/
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneToBuildingSettingAuto : MonoBehaviour
{
/// <summary>
/// 程序启动时检测、提示
/// </summary>
[InitializeOnLoadMethod]
private static void InitializeOnload()
{
//EditorApplication.playModeStateChanged -= EditorApplication_playModeStateChanged;
//EditorApplication.playModeStateChanged += EditorApplication_playModeStateChanged;
}
private static void EditorApplication_playModeStateChanged(PlayModeStateChange playModeStateChange)
{
switch (playModeStateChange)
{
//当退出Editor模式时 检测
case PlayModeStateChange.ExitingEditMode:
{
ScenesInBuildActiveSceneCheck();
break;
}
}
}
/// <summary>
/// 检测当前启动的场景 然后添加至BuildingSetting
/// </summary>
private static void ScenesInBuildActiveSceneCheck()
{
var scenesList = EditorBuildSettings.scenes.ToList();
var activeScene = SceneManager.GetActiveScene();
var isSceneInBuildSettings = scenesList.Count((c) => c.path == activeScene.path) == 1;
if (!isSceneInBuildSettings)
{
if (EditorUtility.DisplayDialog("Add Scene to Scenes in Build", $"The current scene was not found in the scenes" +
$" in build and a {nameof(SceneToBuildingSettingAuto)} instance was found with scene management enabled! Clients will not be able " +
$"to synchronize to this scene unless it is added to the scenes in build list.\n\nWould you like to add it now?",
"Yes", "No - Continue"))
{
scenesList.Add(new EditorBuildSettingsScene(activeScene.path, true));
EditorBuildSettings.scenes = scenesList.ToArray();
}
}
}
}
public static class SceneTools
{
[MenuItem("OTools/AddStateScenesToBuildingSetting")]
private static void AddStateScenesToBuildingSetting()
{
var dir = "Assets/StateScenes";
//获取指定路径下面的所有资源文件
if (Directory.Exists(dir))
{
DirectoryInfo direction = new DirectoryInfo(dir);
FileInfo[] files = direction.GetFiles("*.unity", SearchOption.AllDirectories);
Debug.Log(files.Length);
var scenesList = EditorBuildSettings.scenes.ToList();
for (int i = 0; i < files.Length; i++)
{
if (files[i].Name.EndsWith(".meta") || !files[i].Name.EndsWith(".unity"))
{
continue;
}
//Debug.Log("Name:" + files[i].Name);
//Debug.Log("FullName:" + files[i].FullName);
//Debug.Log("DirectoryName:" + files[i].DirectoryName);
var name = files[i].Name;
var path = files[i].FullName.Replace("\\", "/");
path = path.Replace("//", "/").Trim();
path = path.Replace("///", "/").Trim();
path = path.Replace("\\\\", "/").Trim();
var index = path.IndexOf("Assets");
path = path.Remove(0, index);
var isSceneInBuildSettings = scenesList.Count((c) => c.path == path) == 1;
if (!isSceneInBuildSettings)
{
var targetScene = new EditorBuildSettingsScene(path, true);
if (EditorUtility.DisplayDialog("Add Scene to Scenes in Build", $"[{name}]Would you like to add it now?",
"Yes", "No - Continue"))
{
try
{
scenesList.Add(targetScene);
}
catch (System.Exception e)
{
Debug.LogError($"add to BuildingSetting faild : {e.Message} [{path}->{targetScene.path}]");
throw e;
}
}
}
EditorBuildSettings.scenes = scenesList.ToArray();
}
}
}
[MenuItem("OTools/ClearBuildingSettingScene")]
private static void ClearBuildingSettingScene()
{
if (EditorUtility.DisplayDialog("Clear Scene From Scenes in Build", $"Are you sure to clear now?",
"Yes", "No - Continue"))
{
try
{
var scenesList = EditorBuildSettings.scenes.ToList();
scenesList.Clear();
EditorBuildSettings.scenes = scenesList.ToArray();
Debug.Log("Clear Scene From Scenes Successful!");
//EditorUtility.DisplayDialog("", "Clear Scene From Scenes Successful", "OK");
}
catch (System.Exception e)
{
Debug.LogError($"Clear Scene From Scenes in Build faild : {e.Message}");
throw e;
}
}
}
}
#endif