2022_1_10 完成了拥挤度表示的初步设计

master
GamerHJD 3 years ago
parent 2a614e0fbf
commit c4cafe2996

@ -9,13 +9,12 @@ Material:
m_PrefabAsset: {fileID: 0}
m_Name: ColorRoad
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords: _ALPHABLEND_ON
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: 3000
stringTagMap:
RenderType: Transparent
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
@ -60,19 +59,19 @@ Material:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 10
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 2
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 5
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 0.25495756, g: 0.9529412, b: 0.10196078, a: 0.53333336}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}

@ -497,9 +497,10 @@ GameObject:
- component: {fileID: 5940721742220947037}
- component: {fileID: 1317834824}
- component: {fileID: 2997252462601936660}
- component: {fileID: 4806489167474073388}
m_Layer: 0
m_Name: Car_1
m_TagString: Untagged
m_TagString: Car
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
@ -541,7 +542,7 @@ NavMeshAgent:
m_AngularSpeed: 1000
m_StoppingDistance: 0
m_AutoTraverseOffMeshLink: 1
m_AutoBraking: 0
m_AutoBraking: 1
m_AutoRepath: 1
m_Height: 2
m_BaseOffset: 0
@ -560,6 +561,22 @@ BoxCollider:
serializedVersion: 2
m_Size: {x: 3, y: 1, z: 10}
m_Center: {x: 0, y: 0.74, z: 6.99}
--- !u!54 &4806489167474073388
Rigidbody:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5940721742220849789}
serializedVersion: 2
m_Mass: 1
m_Drag: 0
m_AngularDrag: 0.05
m_UseGravity: 1
m_IsKinematic: 1
m_Interpolate: 0
m_Constraints: 0
m_CollisionDetection: 0
--- !u!1 &5940721742220849791
GameObject:
m_ObjectHideFlags: 0

File diff suppressed because it is too large Load Diff

@ -1,9 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dirve : MonoBehaviour
{
}

@ -5,7 +5,13 @@ using UnityEngine;
public class GameRoot : MonoBehaviour
{
public static GameRoot Instance = null;
[HideInInspector]
public CarSys carSys;
[HideInInspector]
public RoadSys roadSys;
//信息窗口
public InfoWnd infoWnd;
private void Awake()
{
@ -14,11 +20,15 @@ public class GameRoot : MonoBehaviour
//车辆系统初始化
carSys = GetComponent<CarSys>();
carSys.Init();
//车辆流量监控初始化
roadSys = GetComponent<RoadSys>();
roadSys.Init();
}
private void Update()
{
carSys.CreatCar(5);
carSys.CreatCar(0.5f);//每n秒随机出发点创建汽车
roadSys.RoadStateUpdate(3);//每n秒检测路段状态
}
}

@ -10,10 +10,9 @@ using UnityEngine.AI;
/// </summary>
public class CarSys : MonoBehaviour
{
//计时器
private float timer = 0f;
//车辆生成间隔
public float bornTime = 10;
//车辆种类列表
public GameObject[] carList;
@ -59,8 +58,10 @@ public class CarSys : MonoBehaviour
{
if (lineName != "" && agent != null)
{
Transform moveListParent = null;
MoveList.TryGetValue(lineName,out moveListParent);
if (moveListParent != null)
{
Transform[] moveList=new Transform[moveListParent.childCount];
@ -70,6 +71,7 @@ public class CarSys : MonoBehaviour
moveList[i] = moveListParent.GetChild(i);
if (moveList[i] != null)
{
//向移动列表支点移动
agent.speed = 20;
agent.SetDestination(moveList[i].position);
@ -77,8 +79,9 @@ public class CarSys : MonoBehaviour
while (true)
{
//*********************//
yield return new WaitForEndOfFrame();
yield return new WaitForSeconds(0.02f);
float distance = Vector3.Distance(agent.transform.position, moveList[i].position);
if(distance <= 40f)
{
agent.speed = 10;
@ -124,8 +127,11 @@ public class CarSys : MonoBehaviour
if (parent != null)
{
int carIndex = Random.Range(0, carList.Length-1);
//实例化车辆
GameObject car = GameObject.Instantiate(carList[carIndex],parent.position,Quaternion.identity,parent);
car.transform.localEulerAngles = parent.localEulerAngles;
//统计数量更新
GameRoot.Instance.infoWnd.SetCarText(1);//+1
//执行寻路AI
NavMeshAgent agent = car.GetComponent<NavMeshAgent>();
if (agent != null)

@ -0,0 +1,104 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RoadSys : MonoBehaviour
{
/// <summary>
/// 路段检测数据字典
/// </summary>
public Dictionary<string, int> roadCheckerDict = new Dictionary<string, int>();
/// <summary>
/// 2D地图中路段状态的对象的字典
/// </summary>
public Dictionary<string, GameObject> roadStateDict = new Dictionary<string, GameObject>();
private float timer = 0;
public void Init()
{
InitChecker();
}
//检索检测器和2D地图路段状态器
public void InitChecker()
{
//检测器
GameObject[] checkers = GameObject.FindGameObjectsWithTag("CarFlowChecker");
for (int i = 0; i < checkers.Length; i++)
{
if (checkers[i] != null)
{
roadCheckerDict.Add(checkers[i].name, 0);//初始化阶段车辆数量默认0
}
}
///状态器
GameObject[] states= GameObject.FindGameObjectsWithTag("CarFlowState");
for (int i = 0; i < states.Length; i++)
{
if (states[i] != null)
{
roadStateDict.Add(states[i].name, states[i]);//初始化阶段车辆数量默认0
}
}
}
/// <summary>
/// 路段字典键值修改
/// </summary>
/// <param name="roadName"></param>
/// <param name="value"></param>
public void SetCheckerValue(string roadName,int value)
{
if (roadCheckerDict.ContainsKey(roadName))
{
//改变值
int numble = roadCheckerDict[roadName];
numble += value;
//加入字典
roadCheckerDict.Remove(roadName);
roadCheckerDict.Add(roadName, numble);
}
}
/// <summary>
/// 路况更新函数
/// </summary>
/// <param name="time">间隔时间</param>
public void RoadStateUpdate(float time)
{
timer += Time.deltaTime;
if (timer >= time)
{
foreach (var item in roadCheckerDict)
{
GameObject state_object = null;
//获取到状态表示器
if(roadStateDict.TryGetValue(item.Key, out state_object))
{
int numble = item.Value;//监控的车辆数量
if (numble >= 0 && numble <= 6)//绿色状态
{
state_object.GetComponent<MeshRenderer>().material.color = Color.green;
}
else if(numble > 6 && numble <= 12)//黄色状态
{
state_object.GetComponent<MeshRenderer>().material.color = Color.yellow;
}
else if (numble > 12 )//红色拥堵
{
state_object.GetComponent<MeshRenderer>().material.color = Color.red;
}
}
else
{
Debug.LogError("没有获取到状态表示器:" + item.Key);
}
}
timer = 0;
}
}
}

@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 路段车辆流量检测器
/// </summary>
public class RoadChecker : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Car")
{
GameRoot.Instance.roadSys.SetCheckerValue(this.gameObject.name,1);
// Debug.Log(this.gameObject.name+"+1");
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Car")
{
GameRoot.Instance.roadSys.SetCheckerValue(this.gameObject.name, -1);
// Debug.Log(this.gameObject.name + "-1");
}
}
}

@ -0,0 +1,23 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InfoWnd : MonoBehaviour
{
public Text carTxt;
public Text humanTxt;
public void SetCarText(int Addvalue)
{
int n = int.Parse(carTxt.text) + Addvalue;
carTxt.text = n.ToString();
}
public void SetHumanText(int Addvalue)
{
int n = int.Parse(humanTxt.text) + Addvalue;
humanTxt.text = n.ToString();
}
}

@ -3,6 +3,7 @@
"com.unity.collab-proxy": "1.2.16",
"com.unity.ide.rider": "1.1.4",
"com.unity.ide.vscode": "1.2.3",
"com.unity.recorder": "3.0.3",
"com.unity.test-framework": "1.1.19",
"com.unity.textmeshpro": "2.1.1",
"com.unity.timeline": "1.2.17",

@ -30,6 +30,15 @@
"dependencies": {},
"url": "https://packages.unity.cn"
},
"com.unity.recorder": {
"version": "3.0.3",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.timeline": "1.0.0"
},
"url": "https://packages.unity.cn"
},
"com.unity.test-framework": {
"version": "1.1.19",
"depth": 0,

@ -6,6 +6,9 @@ TagManager:
tags:
- MoveList
- BornPoint
- CarFlowChecker
- Car
- CarFlowState
layers:
- Default
- TransparentFX

Loading…
Cancel
Save