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

135 lines
3.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using PhxhSDK.AOT;
using UnityEngine;
using PhxhSDK.Phxh;
using Gameplay.Level;
using PhxhSDK;
using Sirenix.OdinInspector;
using Debug = UnityEngine.Debug;
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine.SceneManagement;
[ExecuteAlways]
#endif
public class LevelEditor : MonoBehaviour
{
private const string LEVEL_DATA_NAME = "LevelData";
public LevelData LevelData { get; private set; }
public TextAsset LevelDataJson
{
get => _levelDataJson;
set
{
var data = JsonToLevelData(value);
_levelDataJson = value;
LevelData = data;
}
}
[SerializeField]
[LabelText("关卡数据")]
[OnValueChanged("OnLevelDataJsonChanged")]
private TextAsset _levelDataJson;
private void OnLevelDataJsonChanged()
{
LevelDataJson = _levelDataJson;
}
// [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
// }
private LevelData JsonToLevelData(TextAsset jsonText)
{
#if UNITY_EDITOR
LevelData levelData = null;
if (jsonText != null)
{
try
{
var jsonStr = jsonText.text;
if (string.IsNullOrEmpty(jsonStr))
{
levelData = new();
}
else
{
var path = AssetDatabase.GetAssetPath(jsonText);
levelData = JsonHelper.LoadJson<LevelData>(path);
}
}
catch (UnassignedReferenceException)
{
Debug.LogWarning($"LevelEditor: _levelDataJson 未被赋值请在Inspector中分配TextAsset资源");
levelData = new();
}
}
return levelData;
#else
return null;
#endif
}
private void Awake()
{
if (BusyWrapper.isBusy)
return;
LevelData = JsonToLevelData(_levelDataJson);
}
private void OnDestroy()
{
if (BusyWrapper.isBusy)
return;
SaveLevelData();
}
public void SaveLevelData()
{
if (BusyWrapper.isBusy)
return;
if (Application.isPlaying)
return;
#if UNITY_EDITOR
var currentScene = gameObject.scene;
if (currentScene.name.Contains("_PostProcess"))
{
DebugUtil.LogError("当前在后处理场景,当前数据不会保存,请在原场景处理关卡数据");
return;
}
if (_levelDataJson != null && !Application.isPlaying && currentScene == SceneManager.GetActiveScene())
{
var path = AssetDatabase.GetAssetPath(_levelDataJson);
JsonHelper.SaveJson(path, LevelData);
AssetDatabase.Refresh();
DebugUtil.Log($"保存关卡数据成功!");
}
#endif
}
}