NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/GameWrapper/Level/LevelEditor.cs

135 lines
3.6 KiB
C#
Raw Normal View History

2024-02-21 13:06:50 +08:00
using PhxhSDK.AOT;
2024-09-23 13:52:58 +08:00
using UnityEngine;
2023-11-30 15:47:49 +08:00
using PhxhSDK.Phxh;
2024-09-23 13:52:58 +08:00
using Gameplay.Level;
2025-08-11 15:52:55 +08:00
using PhxhSDK;
2023-11-21 15:04:56 +08:00
using Sirenix.OdinInspector;
2024-09-23 13:52:58 +08:00
using Debug = UnityEngine.Debug;
2023-11-21 15:04:56 +08:00
#if UNITY_EDITOR
using UnityEditor;
2024-09-23 13:52:58 +08:00
using UnityEngine.SceneManagement;
2023-11-21 15:04:56 +08:00
[ExecuteAlways]
2025-05-16 19:22:52 +08:00
#endif
2023-11-21 15:04:56 +08:00
public class LevelEditor : MonoBehaviour
{
private const string LEVEL_DATA_NAME = "LevelData";
2023-11-22 12:23:18 +08:00
2023-11-21 15:04:56 +08:00
public LevelData LevelData { get; private set; }
public TextAsset LevelDataJson
{
get => _levelDataJson;
2023-12-21 19:08:04 +08:00
set
2023-11-21 15:04:56 +08:00
{
var data = JsonToLevelData(value);
_levelDataJson = value;
LevelData = data;
}
}
[SerializeField]
[LabelText("关卡数据")]
[OnValueChanged("OnLevelDataJsonChanged")]
private TextAsset _levelDataJson;
private void OnLevelDataJsonChanged()
{
LevelDataJson = _levelDataJson;
}
2023-12-21 19:08:04 +08:00
// [Button("创建关卡数据")]
// [Conditional("UNITY_EDITOR")]
// private void CreateLevelData()
// {
// #if UNITY_EDITOR
// var scene = gameObject.scene;
// var levelDataDir = Path.GetDirectoryName(Constants.LEVEL_CONFIG_FORMAT_PATH);
// var count = AssetDatabase.FindAssets("t:TextAsset", new[] { levelDataDir }).Length;
// var dataName = $"LevelData";
// var dataPath = $"{levelDataDir}/{dataName}.json";
// using (var fileStream = File.Create(dataPath))
// {
// }
//
// AssetDatabase.Refresh();
// var textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(dataPath);
// EditorGUIUtility.PingObject(textAsset);
// if (_levelDataJson == null)
// LevelDataJson = textAsset;
// #endif
// }
2023-11-21 15:04:56 +08:00
private LevelData JsonToLevelData(TextAsset jsonText)
{
2023-11-22 12:23:18 +08:00
#if UNITY_EDITOR
2023-11-21 15:04:56 +08:00
LevelData levelData = null;
2025-09-15 16:58:59 +08:00
if (jsonText != null)
2023-11-21 15:04:56 +08:00
{
2025-09-15 16:58:59 +08:00
try
2023-11-21 15:04:56 +08:00
{
2025-09-15 16:58:59 +08:00
var jsonStr = jsonText.text;
if (string.IsNullOrEmpty(jsonStr))
{
levelData = new();
}
else
{
var path = AssetDatabase.GetAssetPath(jsonText);
levelData = JsonHelper.LoadJson<LevelData>(path);
}
2023-11-21 15:04:56 +08:00
}
2025-09-15 16:58:59 +08:00
catch (UnassignedReferenceException)
2023-11-21 15:04:56 +08:00
{
2025-09-15 16:58:59 +08:00
Debug.LogWarning($"LevelEditor: _levelDataJson 未被赋值请在Inspector中分配TextAsset资源");
levelData = new();
2023-11-21 15:04:56 +08:00
}
}
return levelData;
2023-11-22 15:06:22 +08:00
#else
return null;
2023-11-22 12:23:18 +08:00
#endif
2023-11-21 15:04:56 +08:00
}
private void Awake()
{
2025-08-11 15:52:55 +08:00
if (BusyWrapper.isBusy)
return;
2023-11-21 15:04:56 +08:00
LevelData = JsonToLevelData(_levelDataJson);
}
private void OnDestroy()
{
2025-08-11 15:52:55 +08:00
if (BusyWrapper.isBusy)
2024-02-21 13:06:50 +08:00
return;
2023-11-21 15:04:56 +08:00
SaveLevelData();
}
2023-11-22 15:06:22 +08:00
2023-11-21 15:04:56 +08:00
public void SaveLevelData()
{
2025-08-11 15:52:55 +08:00
if (BusyWrapper.isBusy)
return;
2025-10-14 13:03:04 +08:00
if (Application.isPlaying)
return;
2023-11-22 12:23:18 +08:00
#if UNITY_EDITOR
2024-09-23 13:52:58 +08:00
var currentScene = gameObject.scene;
2025-10-14 12:06:02 +08:00
if (currentScene.name.Contains("_PostProcess"))
{
DebugUtil.LogError("当前在后处理场景,当前数据不会保存,请在原场景处理关卡数据");
return;
}
2025-05-16 19:07:21 +08:00
2024-09-23 13:52:58 +08:00
if (_levelDataJson != null && !Application.isPlaying && currentScene == SceneManager.GetActiveScene())
2023-11-21 15:04:56 +08:00
{
var path = AssetDatabase.GetAssetPath(_levelDataJson);
JsonHelper.SaveJson(path, LevelData);
AssetDatabase.Refresh();
2024-10-28 16:26:20 +08:00
DebugUtil.Log($"保存关卡数据成功!");
2023-11-21 15:04:56 +08:00
}
2023-11-22 12:23:18 +08:00
#endif
2023-11-21 15:04:56 +08:00
}
}