路线添加标签中间版本(有时候路线生成会有问题)

This commit is contained in:
jwp
2025-06-26 21:25:53 +08:00
parent 86d98bb4b5
commit c907c80a85
6 changed files with 103 additions and 29 deletions

View File

@ -21243,6 +21243,7 @@ MonoBehaviour:
_passedPersonForbiddenNodesList:
NormalGreenColor: {r: 0, g: 0, b: 0, a: 0}
HighlightedGreenColor: {r: 0, g: 0, b: 0, a: 0}
orgin: {fileID: 0}
--- !u!4 &1901280852
Transform:
m_ObjectHideFlags: 0
@ -24421,6 +24422,7 @@ MonoBehaviour:
_passedPersonForbiddenNodesList:
NormalGreenColor: {r: 0.22753048, g: 1, b: 0, a: 1}
HighlightedGreenColor: {r: 1, g: 1, b: 1, a: 1}
orgin: {fileID: 0}
--- !u!4 &2139036575
Transform:
m_ObjectHideFlags: 0

View File

@ -3,12 +3,14 @@ using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class PathLineAnimator : MonoBehaviour
{
private static IEnumerator enumerator;
public static void Play(
MonoBehaviour caller,
PathType pathType,
Transform origin,
Transform mover,
LineRenderer lineRenderer,
@ -17,7 +19,7 @@ public class PathLineAnimator : MonoBehaviour
{
if (enumerator != null)
caller.StopCoroutine(enumerator);
enumerator = DrawLineCoroutine(mover, lineRenderer, unsortedNodes, moveSpeed);
enumerator = DrawLineCoroutine(pathType,mover, lineRenderer, unsortedNodes, moveSpeed);
caller.StartCoroutine(enumerator);
}
@ -96,6 +98,7 @@ public class PathLineAnimator : MonoBehaviour
private static IEnumerator DrawLineCoroutine(
PathType pathType,
Transform mover,
LineRenderer lineRenderer,
List<MapGraph.Node> rawPoints,
@ -127,9 +130,11 @@ public class PathLineAnimator : MonoBehaviour
lineRenderer.positionCount = 2;
lineRenderer.SetPosition(0, points[0].position);
lineRenderer.SetPosition(1, points[0].position);
EventManager.TriggerShowCameraDetailEvent(points[0].camera_id);
var cameraID = points[0].camera_id;
EventManager.TriggerShowCameraDetailEvent(pathType,cameraID);
while (currentIndex < points.Count - 1)
{
Vector3 start = points[currentIndex].position;
Vector3 end = points[currentIndex + 1].position;
float t = 0f;
@ -149,8 +154,11 @@ public class PathLineAnimator : MonoBehaviour
yield return null;
}
Debug.Log($"cameraID:{cameraID}");
currentIndex++;
EventManager.TriggerShowCameraDetailEvent(points[currentIndex].camera_id);
cameraID = points[currentIndex].camera_id;
EventManager.TriggerShowCameraDetailEvent(pathType,cameraID);
if (currentIndex < points.Count - 1)
{
lineRenderer.positionCount++;
@ -158,6 +166,9 @@ public class PathLineAnimator : MonoBehaviour
}
lineRenderer.SetPosition(lineRenderer.positionCount - 1, points[^1].position);
mover.position = points[^1].position;
yield return new WaitForSeconds(1f);
cameraID = points[currentIndex].camera_id;
EventManager.TriggerShowCameraDetailEvent(pathType,cameraID);
}

View File

@ -12,7 +12,7 @@ public class EventManager
public static Action<List<Transform>> RefreshCarCameraNodesEvent;
public static Action<List<Transform>> RefreshPersonCameraNodesEvent;
public static Action<int>ShowCameraDetailEvent;
public static Action<PathType,int>ShowCameraDetailEvent;
public static Action RefreshPersonPathEvent;
public static Action RefreshCarPathEvent;
@ -23,9 +23,9 @@ public class EventManager
public static Action<bool> LoginSuccessEvent;
public static void TriggerShowCameraDetailEvent(int index)
public static void TriggerShowCameraDetailEvent(PathType pathType,int index)
{
ShowCameraDetailEvent?.Invoke(index); //触发事件
ShowCameraDetailEvent?.Invoke(pathType,index); //触发事件
}
public static void TriggerLoginSuccessEvent(bool isSuccess)

View File

@ -1,6 +1,11 @@
using System.Collections.Generic;
using UnityEngine;
public enum PathType
{
Person,
Vehicle
}
/// <summary>
/// 模拟摄像头路径规划:通过摄像头拍到的节点推断行走轨迹,避开未被拍到的不可达节点。
/// 同时使用 LineRenderer 连线路径。
@ -28,7 +33,7 @@ public class LineManger : MonoBehaviour
private Transform mover;
public Transform orgin;
private PathType _pathType = PathType.Person;
public static LineManger Instance;
private void Awake()
{
@ -197,14 +202,16 @@ public class LineManger : MonoBehaviour
private void RefreshPersonPathEvent()
{
if(_personMustList.Count==0)return;
CreatePathWithMustPoints(_personMustList,new List<int>(),out _passedPersonForbiddenNodesList);
_pathType= PathType.Person;
CreatePathWithMustPoints(_pathType,_personMustList,new List<int>(),out _passedPersonForbiddenNodesList);
SetLineSpriteRender();
}
private void RefreshCarPathEvent()
{
if(_carMustList.Count==0)return;
CreatePathWithMustPoints(_carMustList,new List<int>(),out _passedCarForbiddenNodesList);
_pathType= PathType.Vehicle;
CreatePathWithMustPoints(_pathType,_carMustList,new List<int>(),out _passedCarForbiddenNodesList);
SetLineSpriteRender();
}
@ -303,7 +310,7 @@ public class LineManger : MonoBehaviour
}
public void CreatePathWithMustPoints(List<int> mustPassIDs, List<int> mustAvoidIDs, out List<int> cameraList)
public void CreatePathWithMustPoints(PathType pathType,List<int> mustPassIDs, List<int> mustAvoidIDs, out List<int> cameraList)
{
List<string> mustPassNodes = new List<string>();
List<string> mustAvoidNodes = new List<string>();
@ -315,11 +322,11 @@ public class LineManger : MonoBehaviour
{
mustAvoidNodes.Add(_cameraMappingDic[mustAvoidIDs[i]]);
}
CreatePathWithMustPoints(mustPassNodes, mustAvoidNodes,out cameraList);
CreatePathWithMustPoints(pathType,mustPassNodes, mustAvoidNodes,out cameraList);
}
private void CreatePathWithMustPoints(List<string> mustPassIDs, List<string> mustAvoidIDs,out List<int>cameraList)
private void CreatePathWithMustPoints(PathType pathType,List<string> mustPassIDs, List<string> mustAvoidIDs,out List<int>cameraList)
{
cameraList = new List<int>();
lineRenderer.positionCount = 0;
@ -329,6 +336,8 @@ public class LineManger : MonoBehaviour
{
var node = _map.GetNode(id);
node.camera_id = GetCameraId(id);
Debug.Log($"origin id:{id}node.camera_id:{node.camera_id}");
}
foreach (var id in mustPassIDs)
@ -342,9 +351,13 @@ public class LineManger : MonoBehaviour
var node = _map.GetNode(id);
if (node != null) mustAvoidNodes.Add(node);
}
List<MapGraph.Node> forbiddenPassed;
var path = PathFinder.FindPathThroughNodes(mustPassNodes, mustAvoidNodes, out forbiddenPassed);
if (path == null) return;
if(mustPassNodes.Count<2)return;
var path = PathFinder.FindPathThroughNodes(mustPassNodes, mustAvoidNodes, out var forbiddenPassed);
for (int i = 0; i < path.Count; i++)
{
Debug.Log($"path camera_id:{path[i].camera_id}");
}
if (path == null||path.Count<2) return;
foreach (var item in _cameraMappingDic)
{
@ -363,7 +376,7 @@ public class LineManger : MonoBehaviour
{
Debug.LogWarning($"注意:路径中经过了以下不可达节点:{string.Join(", ", forbiddenPassed.ConvertAll(n => n.id))}");
}
PathLineAnimator.Play(this, orgin,mover, lineRenderer, path, moveSpeed: 3f);
PathLineAnimator.Play(this, pathType,orgin,mover, lineRenderer, path, moveSpeed: 3f);
}
else
{
@ -1066,6 +1079,12 @@ public class LineManger : MonoBehaviour
_cameraMappingDic.Add(36, "HH");
#endregion
#region
#endregion
#region Level Dic
@ -1116,6 +1135,26 @@ public class LineManger : MonoBehaviour
#endregion
}
public void GetSameNodeCameraId(int cameraID, out List<int> cameraList)
{
cameraList = new List<int>();
if(!_cameraMappingDic.TryGetValue(cameraID, out var cameraName))return;
foreach(var item in _cameraMappingDic)
{
int id = item.Key;
if (cameraName.Equals(item.Value))
{
cameraList.Add(id);
}
}
}
public bool EqualCameraId(int cameraID1, int cameraID2)
{
return _cameraMappingDic[cameraID1] == _cameraMappingDic[cameraID2];
}
/// <summary>
/// 创建带有可达/不可达状态的地图图结构
/// </summary>

View File

@ -95,6 +95,7 @@ public class PathFinder
// 尝试绕过不可达点
var path = FindPathInternal(start, end, new HashSet<MapGraph.Node>());
// 如果失败,允许穿越 mustAvoid 节点
if (path == null)
{
@ -135,10 +136,13 @@ public class PathFinder
private static List<MapGraph.Node> ReconstructPath(Dictionary<MapGraph.Node, MapGraph.Node> cameFrom, MapGraph.Node current)
{
List<MapGraph.Node> totalPath = new List<MapGraph.Node> { current };
while (cameFrom.ContainsKey(current))
{
Debug.Log($"current:{current.camera_id}");
current = cameFrom[current];
Debug.Log($"current1:{current.camera_id}");
totalPath.Insert(0, current);
}
return totalPath;

View File

@ -94,24 +94,42 @@ public class NodeDetailControl : MonoBehaviour
}
//TODO 根据传入的类型不同,用不同的画线,目前是没有区分,只有人物的画线
private void ShowCameraDetailEvent(int cameraId)
private void ShowCameraDetailEvent(PathType pathType,int cameraId)
{
// if (_carResultGosDic.ContainsKey(cameraId))
// {
// var carResultGo = _carResultGosDic[cameraId];
// if (carResultGo != null)
// {
// carResultGo.SetActive(true);
// }
// }
Debug.Log($"ShowCameraDetailEvent cameraId:{cameraId}");
if (_personResultGosDic.TryGetValue(cameraId, out var personResultGo))
if (pathType == PathType.Vehicle)
{
if (personResultGo != null)
Debug.Log($"ShowCameraDetailEvent cameraId:{cameraId}");
LineManger.Instance.GetSameNodeCameraId(cameraId,out List<int>sameNodeCameraIds);
for (int i = 0; i < sameNodeCameraIds.Count; i++)
{
personResultGo.SetActive(true);
if (_carResultGosDic.TryGetValue(sameNodeCameraIds[i], out var carResultGo))
{
if (carResultGo != null)
{
carResultGo.SetActive(true);
}
}
}
}
else if (pathType == PathType.Person)
{
Debug.Log($"ShowCameraDetailEvent cameraId:{cameraId}");
LineManger.Instance.GetSameNodeCameraId(cameraId,out List<int>sameNodeCameraIds);
for (int i = 0; i < sameNodeCameraIds.Count; i++)
{
if (_personResultGosDic.TryGetValue(sameNodeCameraIds[i], out var personResultGo))
{
if (personResultGo != null)
{
personResultGo.SetActive(true);
}
}
}
}
}