Files
valheim/Assets/_Valheim/Scripts/EditMap/EditMap.cs
2025-07-04 14:16:14 +08:00

191 lines
7.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using DragonLi.Core;
using UnityEngine;
using System.Runtime.Serialization;
namespace Valheim
{
public class EditMap : MonoBehaviour
{
public Place place;
public GameObject Areas;
public GameObject Pets;
public GameObject Enemys;
public GameObject Cages;
public GameObject Revives;
public ObjDoor Door;
public ObjBoss Boss;
/// <summary>
/// 编辑器模式的文本存储
/// </summary>
/// <param name="_path">路径类似于Assets/Resources</param>
/// <param name="_name">名字,需要带后缀</param>
/// <param name="_str">存储的文本</param>
void WindowsSave(string _path, string _name, string _str)
{
#if UNITY_EDITOR
System.IO.Directory.CreateDirectory(_path);
using (System.IO.StreamWriter writer = System.IO.File.CreateText(_path + "/" + _name))
writer.Write(_str);
UnityEditor.AssetDatabase.Refresh();
#endif
}
public void Start()
{
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
{
Debug.Log("开始生成地图");
// // 修改怪物区域id
// for (int i = 0; i < Areas.transform.childCount; i++)
// {
// for (int j = 0; j < Enemys.transform.childCount; j++)
// {
// Transform area = Areas.transform.GetChild(i);
// Transform obj = Enemys.transform.GetChild(j);
// ObjEnemy objEnemy = obj.GetComponent<ObjEnemy>();
// Bounds bounds = area.GetComponent<MeshCollider>().bounds;
// // 判断是否在区域内
// if (bounds.Contains(new Vector3(obj.transform.position.x, 0, obj.transform.position.z)))
// {
// if (objEnemy != null)
// {
// // objEnemy.areaId = area.GetComponent<ObjArea>().AreaId;
// }
// }
// }
// }
// // 修改宠物笼子id
// for (int i = 0; i < Cages.transform.childCount; i++)
// {
// for (int j = 0; j < Pets.transform.childCount; j++)
// {
// Transform cage = Cages.transform.GetChild(i);
// Transform obj = Pets.transform.GetChild(j);
// ObjPet objPet = obj.GetComponent<ObjPet>();
// Bounds bounds = cage.GetComponent<BoxCollider>().bounds;
// // 判断是否在笼子内
// if (bounds.Contains(new Vector3(obj.transform.position.x, 0, obj.transform.position.z)))
// {
// if (objPet != null)
// {
// objPet.cageId = cage.GetComponent<ObjCage>().CageId;
// }
// }
// }
// }
string jsonstr = "{\"Data\":[";
int index = 0;
// 区域
for (int i = 0; i < Areas.transform.childCount; i++)
{
Transform obj = Areas.transform.GetChild(i);
ObjArea objArea = obj.GetComponent<ObjArea>();
if (objArea != null)
{
if (i != 0)
{
jsonstr += ",";
}
index++;
jsonstr += JsonUtility.ToJson(objArea.PreparData());
}
}
// 笼子
for (int i = 0; i < Cages.transform.childCount; i++)
{
Transform obj = Cages.transform.GetChild(i);
ObjCage objCage = obj.GetComponent<ObjCage>();
if (objCage != null)
{
if (index != 0 || i != 0)
{
jsonstr += ",";
}
index++;
jsonstr += JsonUtility.ToJson(objCage.PreparData());
}
}
// 怪物
for (int i = 0; i < Enemys.transform.childCount; i++)
{
Transform obj = Enemys.transform.GetChild(i);
ObjEnemy objEnemy = obj.GetComponent<ObjEnemy>();
if (objEnemy != null)
{
if (index != 0 || i != 0)
{
jsonstr += ",";
}
index++;
jsonstr += JsonUtility.ToJson(objEnemy.PreparData());
}
}
// 宠物
for (int i = 0; i < Pets.transform.childCount; i++)
{
Transform obj = Pets.transform.GetChild(i);
ObjPet objPet = obj.GetComponent<ObjPet>();
if (objPet != null)
{
if (index != 0 || i != 0)
{
jsonstr += ",";
}
index++;
jsonstr += JsonUtility.ToJson(objPet.PreparData());
}
}
// 复活点
for (int i = 0; i < Revives.transform.childCount; i++)
{
Transform obj = Revives.transform.GetChild(i);
ObjRevive objRevive = obj.GetComponent<ObjRevive>();
if (objRevive != null)
{
if (index != 0 || i != 0)
{
jsonstr += ",";
}
index++;
jsonstr += JsonUtility.ToJson(objRevive.PreparData());
}
}
// 门
if (Door != null)
{
if (index != 0)
{
jsonstr += ",";
}
index++;
jsonstr += JsonUtility.ToJson(Door.PreparData());
}
// boss
if (Boss != null)
{
if (index != 0)
{
jsonstr += ",";
}
index++;
jsonstr += JsonUtility.ToJson(Boss.PreparData());
}
jsonstr += "]}";
Debug.Log("数据准备就绪");
WindowsSave("Assets/_Valheim/Resources/Maps", place + ".json", jsonstr);
Debug.Log("地图已保存!");
}, 1);
}
}
}