163 lines
5.2 KiB
C#
163 lines
5.2 KiB
C#
|
|
using UnityEngine;
|
|
using EasyInject.Attributes;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.AI;
|
|
using Unity.VisualScripting;
|
|
using System;
|
|
|
|
|
|
[GameObjectBean]
|
|
[AddComponentMenu("WXDC/Container/ContainerManage")]
|
|
public class ContainersManage : MonoBehaviour, IApplcationShutdown
|
|
{
|
|
[Autowired]
|
|
private EventCompent eventCompent;
|
|
|
|
[SerializeField]
|
|
private List<GameObject> _source = new();
|
|
[SerializeField]
|
|
private GameObject defualtContainer;
|
|
[SerializeField]
|
|
private List<Container> _hideContainers = new();
|
|
[SerializeField]
|
|
private float speed;
|
|
|
|
|
|
|
|
[SerializeField]
|
|
private float agentHight;
|
|
[SerializeField]
|
|
private float agentWidth;
|
|
|
|
|
|
/// <summary>
|
|
/// 根据模型代码加载
|
|
/// </summary>
|
|
public Container CreateContainer(Vector3 position,ContainerEntity entity, LayerMask layerMask)
|
|
{
|
|
Container container = CreateContainer(entity);
|
|
container.transform.parent = null;
|
|
container.transform.position = position;
|
|
container.gameObject.layer = layerMask;
|
|
foreach (Transform child in container.GetComponentsInChildren<Transform>())
|
|
{
|
|
child.gameObject.layer = layerMask;
|
|
}
|
|
return container;
|
|
}
|
|
private Container CreateContainer(ContainerEntity entity)
|
|
{
|
|
//容器类型为空
|
|
if (string.IsNullOrEmpty(entity.containerType))
|
|
{
|
|
throw new System.Exception("容器类型 为空");
|
|
}
|
|
|
|
|
|
if (_hideContainers.Count > 0)
|
|
{
|
|
lock (_hideContainers)
|
|
{
|
|
Container containerObject = _hideContainers.Find(v => string.Equals(v.ContainerData.containerType, entity.containerType));
|
|
if (containerObject != null)
|
|
{
|
|
ApplicationBoot.Instance.AddBean(containerObject, entity.containerCode);
|
|
containerObject.name = entity.containerCode;
|
|
containerObject.ContainerData = entity;
|
|
return containerObject;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
GameObject obj = _source.Find(v => string.Equals(entity.containerType, v.name));
|
|
if (obj != null)
|
|
{
|
|
Container container = ApplicationBoot.Instance.CreateGameObjectAsBean<Container>(obj, entity.containerCode);
|
|
container.name = entity.containerCode;
|
|
container.ContainerData = entity;
|
|
container.transform.localPosition = obj.transform.localPosition;
|
|
container.Agent = container.GetComponent<NavMeshAgent>();
|
|
container.Agent.updateRotation = false;
|
|
|
|
eventCompent.MouseClickEvents += container.MouseSingleClick;
|
|
eventCompent.MouseDoubleClickEvents += container.MouseDoubleClick;
|
|
return container;
|
|
}
|
|
else
|
|
{
|
|
Container container = ApplicationBoot.Instance.CreateGameObjectAsBean<Container>(defualtContainer, entity.containerCode);
|
|
container.name = entity.containerCode;
|
|
container.ContainerData = entity;
|
|
container.transform.localPosition = obj.transform.localPosition;
|
|
container.Agent = container.GetComponent<NavMeshAgent>();
|
|
container.Agent.updateRotation = false;
|
|
|
|
eventCompent.MouseClickEvents += container.MouseSingleClick;
|
|
eventCompent.MouseDoubleClickEvents += container.MouseDoubleClick;
|
|
return container;
|
|
}
|
|
|
|
}
|
|
throw new System.Exception(string.Format("生成容器类型:【{code}】失败"));
|
|
}
|
|
public Container CreateContainer(Vector3 localPosition,Transform parent, ContainerEntity entity,LayerMask layerMask)
|
|
{
|
|
Container container = CreateContainer(entity);
|
|
container.transform.parent = parent;
|
|
container.transform.localPosition = localPosition;
|
|
foreach (Transform child in container.GetComponentsInChildren<Transform>())
|
|
{
|
|
child.gameObject.layer = layerMask;
|
|
}
|
|
return container;
|
|
}
|
|
public Container CreateContainer(Transform parent, ContainerEntity entity)
|
|
{
|
|
Container container = CreateContainer(entity);
|
|
container.transform.parent = parent;
|
|
return container;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取IOC容器中的托盘实例
|
|
/// </summary>
|
|
/// <param name="containerCode">容器代码也是bean名称</param>
|
|
/// <returns></returns>
|
|
public Container GetContainerByCode(string containerCode)
|
|
{
|
|
return ApplicationBoot.Instance.GetBean<Container>(containerCode);
|
|
}
|
|
/// <summary>
|
|
/// 从场景中移除该容器
|
|
/// </summary>
|
|
/// <param name="container"></param>
|
|
public void RemoveContainer(Container container)
|
|
{
|
|
|
|
try
|
|
{
|
|
//只从ioc移除
|
|
ApplicationBoot.Instance.DeleteGameObjBean(container, container.name, false);
|
|
//隐藏
|
|
container.gameObject.SetActive(false);
|
|
container.enabled = false;
|
|
//添加到隐藏队列
|
|
_hideContainers.Add(container);
|
|
}catch (Exception e)
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
public void Shutdown()
|
|
{
|
|
_hideContainers.Clear();
|
|
_source.Clear();
|
|
}
|
|
|
|
}
|