1_20对象池优化+车辆AI优化完成

master
GamerHJD 3 years ago
parent 04791121b2
commit 2a26e1ddf3

@ -561,7 +561,7 @@ Rigidbody:
m_Drag: 0
m_AngularDrag: 0.05
m_UseGravity: 1
m_IsKinematic: 0
m_IsKinematic: 1
m_Interpolate: 0
m_Constraints: 0
m_CollisionDetection: 0
@ -581,6 +581,7 @@ MonoBehaviour:
dirveState: 0
drivetime: 0
checktime: 0
driveDir: {x: 0, y: 0, z: 0}
carId: 0
FrontCar: {fileID: 0}
--- !u!65 &3545319385294605599

@ -107,9 +107,9 @@ NavMeshSettings:
serializedVersion: 2
agentTypeID: 0
agentRadius: 0.5
agentHeight: 0.5
agentHeight: 1
agentSlope: 54.2
agentClimb: 0.5
agentClimb: 0.47
ledgeDropHeight: 1
maxJumpAcrossDistance: 0
minRegionArea: 2
@ -133,6 +133,7 @@ GameObject:
- component: {fileID: 1209345}
- component: {fileID: 1209346}
- component: {fileID: 1209347}
- component: {fileID: 1209348}
m_Layer: 0
m_Name: GameRoot
m_TagString: Untagged
@ -198,6 +199,19 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: e56da8d69e03c384db0ad13d6a573d60, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &1209348
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1209343}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 276988240dddf884db6425b92b8a1a1b, type: 3}
m_Name:
m_EditorClassIdentifier:
ResourceDir: Prefab
--- !u!1 &11606201
GameObject:
m_ObjectHideFlags: 0
@ -5745,7 +5759,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 886090459}
m_RootOrder: 14
m_RootOrder: 13
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &232080615
GameObject:
@ -13705,7 +13719,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 886090459}
m_RootOrder: 11
m_RootOrder: 10
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &597147489
GameObject:
@ -17277,7 +17291,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 886090459}
m_RootOrder: 13
m_RootOrder: 12
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &760658245
GameObject:
@ -20597,7 +20611,6 @@ Transform:
- {fileID: 1874726873}
- {fileID: 225378088}
- {fileID: 1950848507}
- {fileID: 991795215}
- {fileID: 1090238321}
- {fileID: 595968184}
- {fileID: 2133317671}
@ -22296,36 +22309,6 @@ MeshFilter:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 987048212}
m_Mesh: {fileID: 4300010, guid: 830250c7afc461b4da411ae9af734920, type: 3}
--- !u!1 &991795214
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 991795215}
m_Layer: 0
m_Name: p (15)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &991795215
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 991795214}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -333.09, y: 793.08, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 886090459}
m_RootOrder: 9
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &997329562
GameObject:
m_ObjectHideFlags: 0
@ -24788,7 +24771,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 886090459}
m_RootOrder: 10
m_RootOrder: 9
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1092173166
GameObject:
@ -35248,7 +35231,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 886090459}
m_RootOrder: 15
m_RootOrder: 14
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1567858278
GameObject:
@ -48890,7 +48873,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 886090459}
m_RootOrder: 12
m_RootOrder: 11
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &2137858935
GameObject:

Binary file not shown.

@ -47,7 +47,7 @@ public class GameRoot : MonoBehaviour
{
// pt.Update();
roadSys.RoadStateUpdate(1.5f);//每n秒检测路段状态
// roadSys.RoadStateUpdate(1.5f);//每n秒检测路段状态
infoWnd.UpdateTime();
}

@ -62,14 +62,14 @@ public class CarSys : MonoBehaviour
}
//车辆AI移动函数
public IEnumerator CarMove(string lineName, NavMeshAgent agent)
public IEnumerator CarMove(string lineName, Drive drive)
{
if (lineName != "" && agent != null)
if (lineName != "" && drive != null)
{
Transform moveListParent = null;
MoveList.TryGetValue(lineName, out moveListParent);
Drive drive = agent.GetComponent<Drive>();
NavMeshAgent agent = drive.agent;
//添加前车检测器
GameObject go =Resources.Load<GameObject>("Prefab/DistanceCheck");
GameObject go_c= Instantiate(go, drive.transform);
@ -86,11 +86,13 @@ public class CarSys : MonoBehaviour
//路径方向
Vector3 Dir = (moveList[i].transform.position - drive.transform.position).normalized;
drive.driveDir = Dir;
drive.dir_trans = moveList[i];
if (moveList[i] != null)
{
//向移动列表支点移动
agent.SetDestination(moveList[i].position);
//携程检测距离与路况
while (true)
{
@ -131,7 +133,8 @@ public class CarSys : MonoBehaviour
//回收车辆
carList.Remove(drive.carId);
Destroy(agent.gameObject);
// Destroy(agent.gameObject);
ObjectPool.Instance.Unspawn(agent.gameObject);
GameRoot.Instance.infoWnd.SetCarOutText(1);
}
@ -238,10 +241,17 @@ public class CarSys : MonoBehaviour
// 随机创建车辆种类
int carIndex = Random.Range(0, carTypeList.Length - 1);
//实例化车辆
GameObject car = GameObject.Instantiate(carTypeList[carIndex], parent.position, Quaternion.identity, parent);
car.transform.eulerAngles = parent.eulerAngles;
//GameObject car = GameObject.Instantiate(carTypeList[carIndex], parent.position, Quaternion.identity, parent);
//car.transform.eulerAngles = parent.eulerAngles;
//对象池优化创建实例
GameObject car = ObjectPool.Instance.Spawn(carTypeList[carIndex].name, parent);
//NavAgent赋值
NavMeshAgent _agent = car.GetComponent<NavMeshAgent>();
//car驾驶类赋值
Drive drive = car.GetComponent<Drive>();
drive.agent = _agent;
drive.carId = CarID;
drive.checktime = checktime;
carList.Add(CarID, drive.gameObject);
@ -256,10 +266,10 @@ public class CarSys : MonoBehaviour
//统计数量更新
GameRoot.Instance.infoWnd.SetCarInText(1);//+1
//执行寻路AI
NavMeshAgent agent = car.GetComponent<NavMeshAgent>();
if (agent != null)
if (drive.agent != null)
{
StartCoroutine(CarMove(parent.name, agent));
StartCoroutine(CarMove(parent.name, drive));
}
}

@ -25,33 +25,35 @@ public class Drive : MonoBehaviour
public int carId = 0;
public GameObject FrontCar=null;
private NavMeshAgent agent;
public NavMeshAgent agent;
private void OnEnable()
{
agent = this.GetComponent<NavMeshAgent>();
}
public Transform dir_trans = null;//检测路径点用
private void FixedUpdate()
{
switch (dirveState)
if (agent != null)
{
case DriveState.Stop:
agent.speed = 0;
agent.velocity = Vector3.zero;
break;
case DriveState.PointOver:
agent.velocity = driveDir * 10;
break;
case DriveState.Turn:
agent.speed = 20;
agent.velocity = driveDir * 15;
break;
case DriveState.Line:
agent.speed = 20;
agent.velocity = driveDir * 20;
break;
switch (dirveState)
{
case DriveState.Stop:
agent.speed = 0;
agent.velocity = Vector3.zero;
break;
case DriveState.PointOver:
agent.velocity = driveDir * 10;
break;
case DriveState.Turn:
agent.speed = 20;
agent.velocity = driveDir * 15;
break;
case DriveState.Line:
agent.speed = 20;
agent.velocity = driveDir * 20;
break;
}
}
}
}

@ -0,0 +1,23 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class MonoSingleton<T> : MonoBehaviour
where T: MonoBehaviour
{
private static T m_instance;
public static T Instance
{
get
{
return m_instance;
}
}
protected virtual void Awake()
{
m_instance = this as T;
}
}

@ -0,0 +1,12 @@
using System.Collections;
using System.Collections.Generic;
public interface IReusable {
//取出时候调用
void OnSpawn();
//回收调用
void OnUnSpawn();
}

@ -0,0 +1,70 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectPool : MonoSingleton<ObjectPool> {
/// <summary>
/// 资源目录
/// </summary>
public string ResourceDir = "";
Dictionary<string, SubPool> m_pools = new Dictionary<string, SubPool>();
//取出物体
public GameObject Spawn(string name,Transform trans) {
SubPool pool = null;
if (!m_pools.ContainsKey(name)) {
RegieterNew(name,trans);
}
pool = m_pools[name];
return pool.Spawn(trans);
}
public GameObject Spawn(GameObject go, Transform trans)
{
SubPool pool = null;
if (!m_pools.ContainsKey(go.name))
{
RegieterNew(go.name, trans);
}
pool = m_pools[go.name];
return pool.Spawn(trans);
}
//回收物体
public void Unspawn(GameObject go) {
SubPool pool = null;
foreach (var p in m_pools.Values) {
if (p.Contain(go)) {
pool = p;
break;
}
}
pool.UnSpawn(go);
}
//回收所有
public void UnspawnAll() {
foreach (var p in m_pools.Values) {
p.UnspawnAll();
}
}
//清除所有
public void Clear()
{
m_pools.Clear();
}
//新建一个池子
void RegieterNew(string names,Transform trans) {
//资源目录
string path = ResourceDir + "/" + names;
//生成预制体
GameObject go = Resources.Load<GameObject>(path);
//新建一池子
SubPool pool = new SubPool(go);
m_pools.Add(pool.Name,pool);
}
}

@ -0,0 +1,10 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class ReusableObject : MonoBehaviour, IReusable
{
public abstract void OnSpawn();
public abstract void OnUnSpawn();
}

@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
public class SubPool
{
//集合
List<GameObject> m_objecs = new List<GameObject>();
//预设
GameObject m_prefab;
//名字
public string Name
{
get
{
return m_prefab.name;
}
}
public SubPool( GameObject go)
{
m_prefab = go;
}
//取出物体
public GameObject Spawn(Transform m_parent)
{
GameObject go = null;
foreach (var obj in m_objecs)
{
if (!obj.activeSelf)
{
go = obj;
}
}
if (go == null)
{
go = GameObject.Instantiate<GameObject>(m_prefab, m_parent.position,Quaternion.identity,m_parent);
go.transform.eulerAngles = m_parent.eulerAngles;
m_objecs.Add(go);
}
else
{
go.transform.SetParent(m_parent);
go.transform.position = m_parent.position;
go.transform.eulerAngles = m_parent.eulerAngles;
}
go.SetActive(true);
go.SendMessage("OnSpawn", SendMessageOptions.DontRequireReceiver);
return go;
}
//回收物体
public void UnSpawn(GameObject go)
{
if (Contain(go))
{
go.SendMessage("OnUnSpawn", SendMessageOptions.DontRequireReceiver);
go.SetActive(false);
}
}
//回收所有
public void UnspawnAll()
{
foreach (var obj in m_objecs)
{
if (obj.activeSelf)
{
UnSpawn(obj);
}
}
}
//判断是否属于list里边
public bool Contain(GameObject go) {
return m_objecs.Contains(go);
}
}
Loading…
Cancel
Save