场景:出生点配置文件支持PB格式

This commit is contained in:
fatiao 2025-05-05 16:08:08 +08:00
parent 7946c50836
commit 597159c872
8 changed files with 287 additions and 37 deletions

Binary file not shown.

View File

@ -1,7 +1,6 @@
fileFormatVersion: 2
guid: 4d16c9e544c37774bbfcf8856121f3ed
folderAsset: yes
DefaultImporter:
guid: b53ea590566bc7043a7475837d6a1db0
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:

View File

@ -2,6 +2,7 @@
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor.SceneManagement;
using Mono.Xml;
using System.Security;
@ -43,12 +44,21 @@ public class BattleLevelUtil : EditorWindow
{
SaveCfg();
}
else if(GUILayout.Button("加载当前场景的出生点配置PB格式"))
{
LoadCfg_PB();
}
else if(GUILayout.Button("保存当前场景的出生点配置PB格式"))
{
SaveCfg_PB();
}
if(!s_openAddPoint)
{
if (GUILayout.Button("开启增加出生点"))
{
s_openAddPoint = true;
SceneView.duringSceneGui -= OnSceneGUI;
SceneView.duringSceneGui += OnSceneGUI;
}
}
@ -57,6 +67,7 @@ public class BattleLevelUtil : EditorWindow
if (GUILayout.Button("关闭增加出生点"))
{
s_openAddPoint = false;
SceneView.duringSceneGui -= OnSceneGUI;
}
}
@ -109,6 +120,7 @@ public class BattleLevelUtil : EditorWindow
//添加一个新的点
public void AddNewSpawnPoint(Vector3 pos)
{
Debug.Log("[AddNewSpawnPoint] " + pos.ToString());
GameObject go = new GameObject("MonsterSpawnPoint" + (spawnPointList.Count+1));
SpawnPoint sp = go.AddComponent<SpawnPoint>();
sp.type = SpawnPointType.Monster;
@ -260,6 +272,113 @@ public class BattleLevelUtil : EditorWindow
AssetDatabase.Refresh();
}
void LoadCfg_PB()
{
string cfgFileName = "Born_" + EditorLevelName + ".bytes";
string filePath = Application.dataPath + "/Content/Xml/" + cfgFileName;
if (!FileSystem.Exists(filePath))
{
Debug.LogError("未生成过场景的出生点配置文件");
return;
}
var path = Constants.XmlConfig + "/" + cfgFileName;
TextAsset ta = AssetDatabase.LoadAssetAtPath<TextAsset>(path);
if(ta == null)
{
Debug.LogError("配置文件有问题");
return;
}
ClearAll();
spawnPointCfgList.Clear();
spawnPointList.Clear();
using (var stream = new MemoryStream(ta.bytes))
{
var _XMLSpawnPoints = ProtoBuf.Serializer.Deserialize<XMLSpawnPoints>(stream);
for (int idx = 0; idx < _XMLSpawnPoints.SpawnPointList.Count; idx++)
{
var _XMLSpawnPoint = _XMLSpawnPoints.SpawnPointList[idx];
SpawnPointCfg spCfg = new SpawnPointCfg();
spCfg.LoadCfgPB(_XMLSpawnPoint);
spawnPointCfgList.Add(spCfg);
}
for(int idx =0; idx < spawnPointCfgList.Count;idx++)
{
SpawnPointCfg spCfg = spawnPointCfgList[idx];
SpawnPoint sp = CreateSpawnPointGo(spCfg, idx + 1);
}
}
}
void SaveCfg_PB()
{
string cfgFileName = "Born_" + EditorLevelName + ".bytes";
string filePath = Application.dataPath + "/Content/Xml/" + cfgFileName;
XMLSpawnPoints _XMLSpawnPoints = new XMLSpawnPoints() { SpawnPointList = new List<XMLSpawnPoint>() };
for(int idx = 0; idx < spawnPointCfgList.Count; idx++)
{
SpawnPointCfg cfg = spawnPointCfgList[idx];
var _Points = new List<XMLPoint>();
foreach (var point in cfg.PointList)
{
_Points.Add(new XMLPoint()
{
NpcId = point.npcId,
Px = point.pos.x,
Py = point.pos.y,
Pz = point.pos.z,
Rx = point.rot.x,
Ry = point.rot.y,
Rz = point.rot.z,
});
}
var _Transfer = new XMLTransfer()
{
Px = cfg.TransferPos.x,
Py = cfg.TransferPos.y,
Pz = cfg.TransferPos.z,
Rx = cfg.TransferRot.x,
Ry = cfg.TransferRot.y,
Rz = cfg.TransferRot.z
};
var _Camera = new XMLCamera()
{
Far = cfg.CamFar,
Px = cfg.BossCamPos.x,
Py = cfg.BossCamPos.y,
Pz = cfg.BossCamPos.z,
Rx = cfg.BossCamRot.x,
Ry = cfg.BossCamRot.y,
Rz = cfg.BossCamRot.z
};
var _XMLSpawnPoint = new XMLSpawnPoint()
{
Type = (int)cfg.PointType,
ActorReadyDist = cfg.ActorReadyDist,
HSpace = cfg.HorSpace,
VSpace = cfg.VerSpace,
Px = cfg.Pos.x,
Py = cfg.Pos.y,
Pz = cfg.Pos.z,
Rx = cfg.Rot.x,
Ry = cfg.Rot.y,
Rz = cfg.Rot.z,
Points = _Points,
Transfer = _Transfer,
Camera = _Camera
};
_XMLSpawnPoints.SpawnPointList.Add(_XMLSpawnPoint);
}
using (FileStream pbPFileStream = new FileStream(filePath, FileMode.Create))
{
ProtoBuf.Serializer.Serialize(pbPFileStream, _XMLSpawnPoints);
}
AssetDatabase.Refresh();
}
SpawnPoint CreateSpawnPointGo(SpawnPointCfg cfg, int cnt)
{
GameObject go = new GameObject("MonsterSpawnPoint" + cnt);

View File

@ -458,6 +458,37 @@ MeshFilter:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 60284184}
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
--- !u!1 &122130945
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 122130946}
m_Layer: 0
m_Name: Terrain
m_TagString: Terrain
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &122130946
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 122130945}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &124695045
GameObject:
m_ObjectHideFlags: 0
@ -865993,3 +866024,4 @@ SceneRoots:
- {fileID: 2481092490094669243}
- {fileID: 881939324}
- {fileID: 44999133}
- {fileID: 122130946}

View File

@ -44,6 +44,11 @@ public class ConfigMgr : Singleton<ConfigMgr>
{
get { return mXmlConfigDict; }
}
private Dictionary<string, byte[]> mPBConfigDict;
public Dictionary<string, byte[]> PBConfigDict
{
get { return mPBConfigDict; }
}
public override void Init()
{
@ -53,6 +58,7 @@ public class ConfigMgr : Singleton<ConfigMgr>
ConfigDictionary = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>(19);
ConfigLongthDictionary = new Dictionary<string, int>(19);
mXmlConfigDict = new Dictionary<string, string>();
mPBConfigDict = new Dictionary<string, byte[]>();
GetConfigAsset().Forget();
var testStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<BattleFields>\n <BattleField battlePos=\"0;0;0\" battleForward=\"-8.742278E-08;0;-1\" battleRot=\"0;180;0\" battleDist=\"2\" spawnDist=\"28\">\n <CamCfg moveSpeed=\"10\" pos=\"0;3;20\" rotSpeed=\"150\" fovSpeed=\"60\" fov=\"75\" angle=\"0;0;0\">\n </CamCfg>\n <CamCfg moveSpeed=\"10\" pos=\"0;3;-20\" rotSpeed=\"150\" fovSpeed=\"60\" fov=\"75\" angle=\"0;180;0\">\n </CamCfg>\n </BattleField>\n <TeamBorn camRot=\"20;180;0\" p4=\"1.2;0;-58\" p0=\"1.63;2.38;-62\" forward=\"0;0;0\" camFov=\"50\" p1=\"-0.98;2.38;-62\" p3=\"-1.85;0.5;-61.5\" p2=\"2.45;0.5;-61.5\" camPos=\"0.33;6;-49\" p5=\"-0.55;0;-58\">\n </TeamBorn>\n</BattleFields>";
@ -255,8 +261,15 @@ public class ConfigMgr : Singleton<ConfigMgr>
{
CommonUtil.UnzipFiles(zipBytes, (fileName, fileBytes) =>
{
string text = Encoding.UTF8.GetString(UnicodeUtil.ConvertToNonBOM(fileBytes));
mXmlConfigDict.Add(Path.GetFileNameWithoutExtension(fileName), text);
if (fileName.EndsWith(".xml"))
{
string text = Encoding.UTF8.GetString(UnicodeUtil.ConvertToNonBOM(fileBytes));
mXmlConfigDict.Add(Path.GetFileNameWithoutExtension(fileName), text);
}
else if (fileName.EndsWith(".bytes"))
{
mPBConfigDict[Path.GetFileNameWithoutExtension(fileName)] = fileBytes;
}
});
m_bInitXMLFinished = true;
CheckCfgOk();
@ -462,6 +475,11 @@ public class ConfigMgr : Singleton<ConfigMgr>
mXmlConfigDict.TryGetValue(fileName, out cfg);
return cfg;
}
public bool GetPBCfg(string fileName, out byte[] cfg)
{
return mPBConfigDict.TryGetValue(fileName, out cfg);
}
public async UniTask<string> GetXmlCfgAsync(string fileName)
{

View File

@ -8,6 +8,7 @@ public enum SpawnPointType
{
Monster = 1, //小怪出生点
Boss = 2, //boss出生点
Item = 3, //物品出生点
}
[Serializable]
@ -117,6 +118,54 @@ public class SpawnPointCfg
mPointList = new List<MonsterSpawnLoc>();
}
public void LoadCfgPB(XMLSpawnPoint spNode)
{
mPointList.Clear();
if (spNode == null) return;
if (spNode.Points == null) return;
mType = (SpawnPointType)spNode.Type;
mPos = new Vector3(spNode.Px, spNode.Py, spNode.Pz);
mRot = new Vector3(spNode.Rx, spNode.Ry, spNode.Rz);
mActorDist = spNode.ActorReadyDist;
mHorSpace = spNode.HSpace;
mVerSpace = spNode.VSpace;
if (spNode.Type == (int)SpawnPointType.Monster)
{
for (int idx = 0; idx < spNode.Points.Count; idx++)
{
var pointNode = spNode.Points[idx];
MonsterSpawnLoc loc = new MonsterSpawnLoc();
loc.pos = new Vector3(pointNode.Px, pointNode.Py, pointNode.Pz);
loc.rot = new Vector3(pointNode.Rx, pointNode.Ry, pointNode.Rz);
loc.npcId = pointNode.NpcId;
mPointList.Add(loc);
}
}
else if (spNode.Type == (int)SpawnPointType.Boss)
{
for (int idx = 0; idx < spNode.Points.Count; idx++)
{
var pointNode = spNode.Points[idx];
MonsterSpawnLoc loc = new MonsterSpawnLoc();
loc.pos = new Vector3(pointNode.Px, pointNode.Py, pointNode.Pz);
loc.rot = new Vector3(pointNode.Rx, pointNode.Ry, pointNode.Rz);
loc.npcId = pointNode.NpcId;
mPointList.Add(loc);
}
mTransferPos = new Vector3(spNode.Transfer.Px, spNode.Transfer.Py, spNode.Transfer.Pz);
mTransferRot = new Vector3(spNode.Transfer.Rx, spNode.Transfer.Ry, spNode.Transfer.Rz);
mBossCamPos = new Vector3(spNode.Camera.Px, spNode.Camera.Py, spNode.Camera.Pz);
mBossCamRot = new Vector3(spNode.Camera.Rx, spNode.Camera.Ry, spNode.Camera.Rz);
}
else
{
}
}
public void LoadCfgXml(SecurityElement spNode)
{
mPointList.Clear();

View File

@ -2351,6 +2351,7 @@ public class Fighter : LogicTransform
ForceSync(Go.transform);
Go.name = name;
Debug.Log($"[Fighter] create fighter {name}");
}
bool ProcessDamage(SkillHitFighterInfo hitInfo)

View File

@ -2,6 +2,7 @@
using System.Collections;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
@ -188,6 +189,7 @@ public class LogicBattleScene : BaseBattleScene
// DebugHelper.LogError("mCurMonsterSpawnPointIdx 从pathList中获取的下表小于0"+ mCurMonsterSpawnPointIdx);
// idx = 0;
// }
Debug.Log($"_SelectSpawnPointCount: {_SelectSpawnPointCount}");
var idx = _SelectSpawnPointCount;
idx = idx % mSpawnPointList.Count;
mCurrentMonsterSpawnPoint = mSpawnPointList[idx];
@ -347,9 +349,30 @@ public class LogicBattleScene : BaseBattleScene
private void LoadMapSpawnPointCfg_PB(string sceneName)
{
var xmlSceneConfigName = $"Born_{sceneName}";
var pbSceneConfigName = $"Born_{sceneName}";
mSpawnPointList.Clear();
mBossSpawnPointList.Clear();
byte[] pbBytes;
var isExist = ConfigMgr.Instance.GetPBCfg(pbSceneConfigName, out pbBytes);
using (var stream = new MemoryStream(pbBytes))
{
var _XMLSpawnPoints = ProtoBuf.Serializer.Deserialize<XMLSpawnPoints>(stream);
for (int idx = 0; idx < _XMLSpawnPoints.SpawnPointList.Count; idx++)
{
var _XMLSpawnPoint = _XMLSpawnPoints.SpawnPointList[idx];
SpawnPointCfg spCfg = new SpawnPointCfg();
spCfg.LoadCfgPB(_XMLSpawnPoint);
if (spCfg.PointType == SpawnPointType.Boss)
{
mBossSpawnPointList.Add(spCfg);
}
else
{
mSpawnPointList.Add(spCfg);
}
}
}
/*
foreach (var XMLSpawnPointsCfg in ConfigMgr.Instance.AllXMLSpawnPointsCfg.XMLSpawnPointsCfgs)
{
if (XMLSpawnPointsCfg.CfgName == xmlSceneConfigName)
@ -416,49 +439,58 @@ public class LogicBattleScene : BaseBattleScene
break;
}
}
*/
}
private void LoadMapSpawnPointCfg(string sceneName)
{
string ta = ConfigMgr.Instance.GetXmlCfg("Born_" + sceneName);
if (ta == null)
byte[] pbBytes;
var isPBCfgExist = ConfigMgr.Instance.GetPBCfg("Born_" + sceneName, out pbBytes);
if (isPBCfgExist)
{
DebugHelper.LogError(sceneName + " 出生点文件不存在");
return;
LoadMapSpawnPointCfg_PB(sceneName);
}
mSpawnPointList.Clear();
mBossSpawnPointList.Clear();
Mono.Xml.SecurityParser doc = new Mono.Xml.SecurityParser();
try
else
{
doc.LoadXml(ta);
}
catch (System.Exception e)
{
DebugHelper.Assert(false, "exception = {0}", e.Message);
return;
}
System.Security.SecurityElement root = doc.SelectSingleNode("SpawnPoints");
if (root == null || root.Children == null) return;
for (int idx = 0; idx < root.Children.Count; idx++)
{
var spNode = root.Children[idx] as System.Security.SecurityElement;
SpawnPointCfg spCfg = new SpawnPointCfg();
spCfg.LoadCfgXml(spNode);
if (spCfg.PointType == SpawnPointType.Boss)
string ta = ConfigMgr.Instance.GetXmlCfg("Born_" + sceneName);
if (ta == null)
{
mBossSpawnPointList.Add(spCfg);
DebugHelper.LogError(sceneName + " 出生点文件不存在");
return;
}
else
mSpawnPointList.Clear();
mBossSpawnPointList.Clear();
Mono.Xml.SecurityParser doc = new Mono.Xml.SecurityParser();
try
{
mSpawnPointList.Add(spCfg);
doc.LoadXml(ta);
}
catch (System.Exception e)
{
DebugHelper.Assert(false, "exception = {0}", e.Message);
return;
}
System.Security.SecurityElement root = doc.SelectSingleNode("SpawnPoints");
if (root == null || root.Children == null) return;
for (int idx = 0; idx < root.Children.Count; idx++)
{
var spNode = root.Children[idx] as System.Security.SecurityElement;
SpawnPointCfg spCfg = new SpawnPointCfg();
spCfg.LoadCfgXml(spNode);
if (spCfg.PointType == SpawnPointType.Boss)
{
mBossSpawnPointList.Add(spCfg);
}
else
{
mSpawnPointList.Add(spCfg);
}
}
}
//DebugHelper.LogError("----------------LoadMapSpawnPointCfg----------------");
}