210 lines
8.8 KiB
C#
210 lines
8.8 KiB
C#
|
using System.Collections.Generic;
|
|||
|
using System.IO;
|
|||
|
using System.Text;
|
|||
|
using Games.Scene;
|
|||
|
using GCGame.Table;
|
|||
|
using UnityEditor;
|
|||
|
using UnityEditor.SceneManagement;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
public class ScenePointUtility : EditorWindow
|
|||
|
{
|
|||
|
public const string tablePosition = "_Test/role.txt";
|
|||
|
public static readonly char[] seperateChars = {'\t'};
|
|||
|
private int _fubenId;
|
|||
|
private int _minLevel = 0;
|
|||
|
private int _maxLevel = 1024;
|
|||
|
private int _missionId = -1;
|
|||
|
private string _lastError;
|
|||
|
private List<ScenePoint> _scenePoints;
|
|||
|
|
|||
|
[MenuItem("Scene/Scene Point Window")]
|
|||
|
public static void CreateWindow()
|
|||
|
{
|
|||
|
GetWindow<ScenePointUtility>();
|
|||
|
}
|
|||
|
|
|||
|
private void OnEnable()
|
|||
|
{
|
|||
|
SceneView.onSceneGUIDelegate += OnSceneGui;
|
|||
|
}
|
|||
|
|
|||
|
private void OnDisable()
|
|||
|
{
|
|||
|
SceneView.onSceneGUIDelegate -= OnSceneGui;
|
|||
|
}
|
|||
|
|
|||
|
private void OnGUI()
|
|||
|
{
|
|||
|
GUILayout.Space(20f);
|
|||
|
_fubenId = EditorGUILayout.IntField("场景Id", _fubenId);
|
|||
|
_missionId = EditorGUILayout.IntField("任务Id", _missionId);
|
|||
|
_minLevel = EditorGUILayout.IntField("最小等级", _minLevel);
|
|||
|
_maxLevel = EditorGUILayout.IntField("最大等级", _maxLevel);
|
|||
|
GUILayout.Space(20f);
|
|||
|
if (GUILayout.Button("显示场景", GUILayout.Width(200f)))
|
|||
|
{
|
|||
|
var fubenList = EditorTableManager.GetTable<Tab_Fuben>();
|
|||
|
var fuben = fubenList.Find(a => a.Id == _fubenId);
|
|||
|
var sceneId = _fubenId;
|
|||
|
if (fuben != null)
|
|||
|
{
|
|||
|
sceneId = fuben.GetSceneIdbyIndex(0);
|
|||
|
if (sceneId < 0)
|
|||
|
sceneId = _fubenId;
|
|||
|
}
|
|||
|
var sceneList = EditorTableManager.GetTable<Tab_SceneClass>();
|
|||
|
var scene = sceneList.Find(a => a.SceneID == sceneId);
|
|||
|
if (scene == null)
|
|||
|
{
|
|||
|
_lastError = string.Format("无法获得场景,FubenId = {0},SceneId = {1}", _fubenId, sceneId);
|
|||
|
}
|
|||
|
else if (!string.IsNullOrEmpty(scene.ResName))
|
|||
|
{
|
|||
|
var scenePath = SceneDisassembly.sceneRoot.Open(scene.ResName + SceneDisassembly.sceneExtension);
|
|||
|
EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Single);
|
|||
|
var tablePath = Application.dataPath.Open(tablePosition);
|
|||
|
if (!File.Exists(tablePath))
|
|||
|
{
|
|||
|
_lastError = "文件不存在 " + tablePath;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
var lines = File.ReadAllLines(tablePath);
|
|||
|
if (lines.Length < 2)
|
|||
|
{
|
|||
|
_lastError = "文件没有内容 " + tablePath;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
var titles = lines[0].Split(seperateChars);
|
|||
|
var nameId = titles.FindIndex(a => a == "角色名");
|
|||
|
var levelId = titles.FindIndex(a => a == "等级");
|
|||
|
var xId = titles.FindIndex(a => a == "坐标x");
|
|||
|
var zId = titles.FindIndex(a => a == "坐标z");
|
|||
|
var targetId = titles.FindIndex(a => a == "场景id");
|
|||
|
if (nameId < 0)
|
|||
|
{
|
|||
|
_lastError = "无法获得角色名";
|
|||
|
return;
|
|||
|
}
|
|||
|
if (levelId < 0)
|
|||
|
{
|
|||
|
_lastError = "无法获得等级";
|
|||
|
return;
|
|||
|
}
|
|||
|
if (xId < 0)
|
|||
|
{
|
|||
|
_lastError = "无法获得坐标x";
|
|||
|
return;
|
|||
|
}
|
|||
|
if (zId < 0)
|
|||
|
{
|
|||
|
_lastError = "无法获得坐标z";
|
|||
|
return;
|
|||
|
}
|
|||
|
if (targetId < 0)
|
|||
|
{
|
|||
|
_lastError = "无法获得场景id";
|
|||
|
return;
|
|||
|
}
|
|||
|
var missionSegment = -1;
|
|||
|
if (_missionId >= 0)
|
|||
|
{
|
|||
|
missionSegment = titles.FindIndex(a => a == "任务id");
|
|||
|
if (missionSegment < 0)
|
|||
|
{
|
|||
|
_lastError = "无法获得任务id";
|
|||
|
return;
|
|||
|
}
|
|||
|
}
|
|||
|
var max = Mathf.Max(nameId, levelId, xId, zId, targetId, missionSegment);
|
|||
|
var sceneText = _fubenId.ToString();
|
|||
|
_scenePoints = new List<ScenePoint>();
|
|||
|
for (var i = 1; i < lines.Length; i++)
|
|||
|
if (!string.IsNullOrEmpty(lines[i]))
|
|||
|
{
|
|||
|
var segments = lines[i].Split(seperateChars);
|
|||
|
if (segments.Length > max &&
|
|||
|
segments[targetId].Trim() == sceneText)
|
|||
|
{
|
|||
|
var charName = segments[nameId];
|
|||
|
var level = segments[levelId];
|
|||
|
var x = segments[xId];
|
|||
|
var z = segments[zId];
|
|||
|
int posX;
|
|||
|
int posZ;
|
|||
|
int levelValue;
|
|||
|
if (int.TryParse(x, out posX) &&
|
|||
|
int.TryParse(z, out posZ) &&
|
|||
|
int.TryParse(level, out levelValue))
|
|||
|
{
|
|||
|
if (levelValue >= _minLevel && levelValue <= _maxLevel)
|
|||
|
{
|
|||
|
if (_missionId >= 0)
|
|||
|
{
|
|||
|
int missionId;
|
|||
|
if (!int.TryParse(segments[missionSegment], out missionId) || _missionId != missionId)
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
var pos = new Vector2(posX, posZ).InsertY() * 0.01f;
|
|||
|
var ray = new Ray(pos + Vector3.up * 300f, Vector3.down);
|
|||
|
RaycastHit hit;
|
|||
|
if (Physics.Raycast(ray, out hit, float.PositiveInfinity,
|
|||
|
ActiveScene.terrainLayMask))
|
|||
|
pos = hit.point;
|
|||
|
_scenePoints.Add(new ScenePoint(charName + " Lv" + level, pos));
|
|||
|
var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
|
|||
|
var collider = sphere.GetComponent<Collider>();
|
|||
|
if (collider != null)
|
|||
|
DestroyImmediate(collider);
|
|||
|
sphere.name = charName + " " + pos;
|
|||
|
var meshRenderer = sphere.GetComponent<MeshRenderer>();
|
|||
|
meshRenderer.sharedMaterial = null;
|
|||
|
sphere.transform.position = pos;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
GUILayout.Space(20f);
|
|||
|
if (!string.IsNullOrEmpty(_lastError))
|
|||
|
EditorGUILayout.HelpBox(_lastError, MessageType.Error);
|
|||
|
GUILayout.Space(20f);
|
|||
|
if (_scenePoints == null)
|
|||
|
EditorGUILayout.HelpBox("没有当前场景数据", MessageType.Warning);
|
|||
|
else
|
|||
|
EditorGUILayout.HelpBox("当前场景点数量" + _scenePoints.Count, MessageType.Info);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
private void OnSceneGui(SceneView sceneView)
|
|||
|
{
|
|||
|
if (_scenePoints != null)
|
|||
|
for (var i = 0; i < _scenePoints.Count; i++)
|
|||
|
{
|
|||
|
var point = _scenePoints[i];
|
|||
|
Handles.color = Color.blue;
|
|||
|
Handles.Label(point.point + Vector3.up, point.info);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
private struct ScenePoint
|
|||
|
{
|
|||
|
public readonly string info;
|
|||
|
public readonly Vector3 point;
|
|||
|
|
|||
|
public ScenePoint(string info, Vector3 point)
|
|||
|
{
|
|||
|
this.info = info;
|
|||
|
this.point = point;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|