261 lines
7.6 KiB
C#
261 lines
7.6 KiB
C#
#if UNITY_EDITOR
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Numerics;
|
||
using Newtonsoft.Json;
|
||
using Script.EditorScript;
|
||
using Script.RuntimeScript.GameLogic;
|
||
|
||
using UnityEditor;
|
||
using UnityEditor.SceneManagement;
|
||
#endif
|
||
|
||
using UnityEngine;
|
||
[ExecuteInEditMode]
|
||
public class ClampEditorController : MonoBehaviour
|
||
{
|
||
#if UNITY_EDITOR
|
||
|
||
public GameObject PropContainer;
|
||
public GameObject CubeContainer;
|
||
public GameObject BroadContainer;
|
||
public RandomPropsTemp RandomPropsTemp;
|
||
|
||
public void ExportData()
|
||
{
|
||
BroadScript[] allBroad = BroadContainer.GetComponentsInChildren<BroadScript>();
|
||
PropScript[] allProp = PropContainer.GetComponentsInChildren<PropScript>();
|
||
CubeScript[] allCube = CubeContainer.GetComponentsInChildren<CubeScript>();
|
||
|
||
string boxPositionStr = GetPosStr(PropContainer.transform.position);
|
||
string allBroadStr = allBroad.Length > 0 ? "[" : string.Empty;
|
||
string allPropStr = allProp.Length > 0 ? "[" : string.Empty;
|
||
string allCubeStr = allCube.Length > 0 ? "[" : string.Empty;
|
||
string allTempStr = RandomPropsTemp.ResultTemps.Count > 0 ? "[" : String.Empty;
|
||
for (int i = 0; i < allBroad.Length; i++)
|
||
{
|
||
if (i == allBroad.Length - 1)
|
||
{
|
||
allBroadStr += allBroad[i].ToJson() + "]";
|
||
}
|
||
else
|
||
{
|
||
allBroadStr += allBroad[i].ToJson() + ",";
|
||
}
|
||
}
|
||
for (int i = 0; i < allProp.Length; i++)
|
||
{
|
||
if (i == allProp.Length - 1)
|
||
{
|
||
allPropStr += allProp[i].ToJson() + "]";
|
||
}
|
||
else
|
||
{
|
||
allPropStr += allProp[i].ToJson() + ",";
|
||
}
|
||
}
|
||
for (int i = 0; i < allCube.Length; i++)
|
||
{
|
||
if (i == allCube.Length - 1)
|
||
{
|
||
allCubeStr += allCube[i].ToJson() + "]";
|
||
}
|
||
else
|
||
{
|
||
allCubeStr += allCube[i].ToJson() + ",";
|
||
}
|
||
}
|
||
|
||
for (int i = 0; i < RandomPropsTemp.ResultTemps.Count; i++)
|
||
{
|
||
List<PropTemp> propTemps = RandomPropsTemp.ResultTemps[i];
|
||
|
||
string tempStr = propTemps.Count > 0 ? "[" : String.Empty;
|
||
for (int j = 0; j < propTemps.Count; j++)
|
||
{
|
||
if (j == propTemps.Count - 1)
|
||
{
|
||
tempStr += propTemps[j].ToJson() + "]";
|
||
}
|
||
else
|
||
{
|
||
tempStr += propTemps[j].ToJson() + ",";
|
||
}
|
||
}
|
||
if (i == RandomPropsTemp.ResultTemps.Count - 1)
|
||
{
|
||
allTempStr += tempStr + "]";
|
||
}
|
||
else
|
||
{
|
||
allTempStr += tempStr + ",";
|
||
}
|
||
|
||
}
|
||
|
||
SceneObjectList all = new SceneObjectList()
|
||
{
|
||
BoxPositionJson = boxPositionStr,
|
||
AllBroadJson = allBroadStr,
|
||
AllPropJson = allPropStr,
|
||
AllCubeJson = allCubeStr,
|
||
AllTempJson = allTempStr
|
||
};
|
||
// 序列化为JSON
|
||
|
||
var activeScene = EditorSceneManager.GetActiveScene();
|
||
Debug.Log("Current Scene Name: " + activeScene.name);
|
||
|
||
string json = JsonConvert.SerializeObject(all);
|
||
string url = $"ABPackage/pkgDownloads/EventDigging/DgCommonResource/ClampJson/{activeScene.name}.json";
|
||
if (File.Exists(url))
|
||
{
|
||
File.Delete(url);
|
||
}
|
||
string filePath = Path.Combine(Application.dataPath, url);
|
||
File.WriteAllText(filePath, json);
|
||
|
||
Debug.Log("Scene exported to " + filePath);
|
||
AssetDatabase.Refresh();
|
||
}
|
||
public void CheckData()
|
||
{
|
||
PropScript[] allProp = PropContainer.GetComponentsInChildren<PropScript>();
|
||
CubeScript[] allCube = CubeContainer.GetComponentsInChildren<CubeScript>();
|
||
Dictionary<Vector2Int, int> keyValuePairs = new Dictionary<Vector2Int, int>();
|
||
CubeScript cubeScript = null;
|
||
Dictionary<int, int> prop = new Dictionary<int, int>();
|
||
for (int i = 0; i < allCube.Length; i++)
|
||
{
|
||
cubeScript = allCube[i];
|
||
if (cubeScript.PropId > 0)
|
||
{
|
||
if (prop.ContainsKey(cubeScript.PropId))
|
||
{
|
||
prop[cubeScript.PropId]++;
|
||
}
|
||
else
|
||
{
|
||
prop.Add(cubeScript.PropId, 1);
|
||
}
|
||
}
|
||
int Row = (int)cubeScript.transform.localPosition.x;
|
||
int Col = (int)cubeScript.transform.localPosition.y;
|
||
Vector2Int vector2Int = new Vector2Int(Row, Col);
|
||
if (!cubeScript.IsBorder)
|
||
{
|
||
if (keyValuePairs.ContainsKey(vector2Int))
|
||
{
|
||
Debug.LogError($"土块位置重复:{Row}--{Col}");
|
||
}
|
||
else
|
||
{
|
||
keyValuePairs.Add(vector2Int, 1);
|
||
}
|
||
GameObject prefab = PrefabUtility.GetCorrespondingObjectFromSource(cubeScript.gameObject) as GameObject;
|
||
CubeScript cubeScriptP = prefab.GetComponent<CubeScript>();
|
||
if (cubeScriptP.DoubleClick != cubeScript.DoubleClick)
|
||
{
|
||
Debug.LogError($"DoubleClick不一致:{Row}--{Col}");
|
||
}
|
||
}
|
||
|
||
}
|
||
for (int i = 0; i < allProp.Length; i++)
|
||
{
|
||
int PropId = allProp[i].PropId;
|
||
if (PropId > 0)
|
||
{
|
||
if (prop.ContainsKey(PropId))
|
||
{
|
||
prop[PropId]--;
|
||
}
|
||
else
|
||
{
|
||
prop.Add(PropId, -1);
|
||
}
|
||
}
|
||
}
|
||
|
||
InspectorEntry[] RandomPropsList = RandomPropsTemp.RandomPropsList;
|
||
for (int i = 0; i < RandomPropsList.Length; i++)
|
||
{
|
||
int PropId = RandomPropsList[i].PropId;
|
||
if (RandomPropsList[i].PropId > 0)
|
||
{
|
||
if (prop.ContainsKey(PropId))
|
||
{
|
||
prop[PropId]++;
|
||
}
|
||
else
|
||
{
|
||
prop.Add(PropId, 1);
|
||
}
|
||
}
|
||
}
|
||
foreach (var item in prop)
|
||
{
|
||
if (item.Value > 0)
|
||
{
|
||
Debug.LogError($"棋盘上 {item.Key} + {item.Value}");
|
||
}
|
||
else if (item.Value < 0)
|
||
{
|
||
Debug.LogError($"棋盘上 {item.Key} {item.Value}");
|
||
}
|
||
}
|
||
}
|
||
private string GetPosStr(UnityEngine.Vector3 pos)
|
||
{
|
||
var serializablePos = new SerializableVector3(pos);
|
||
var settings = new JsonSerializerSettings
|
||
{
|
||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
|
||
};
|
||
return JsonConvert.SerializeObject(serializablePos, settings);
|
||
}
|
||
#endif
|
||
}
|
||
|
||
[System.Serializable]
|
||
public class SerializableVector3
|
||
{
|
||
public float x;
|
||
public float y;
|
||
public float z;
|
||
|
||
public SerializableVector3(float x, float y, float z)
|
||
{
|
||
this.x = x;
|
||
this.y = y;
|
||
this.z = z;
|
||
}
|
||
|
||
public SerializableVector3(UnityEngine.Vector3 vector)
|
||
{
|
||
this.x = vector.x;
|
||
this.y = vector.y;
|
||
this.z = vector.z;
|
||
}
|
||
|
||
public UnityEngine.Vector3 ToVector3()
|
||
{
|
||
return new UnityEngine.Vector3(x, y, z);
|
||
}
|
||
}
|
||
|
||
|
||
[System.Serializable]
|
||
[ExecuteInEditMode]
|
||
public class SceneObjectList
|
||
{
|
||
public string BoxPositionJson;
|
||
public string AllBroadJson;
|
||
public string AllPropJson;
|
||
public string AllCubeJson;
|
||
public string AllTempJson;
|
||
}
|
||
|