91 lines
3.1 KiB
C#
91 lines
3.1 KiB
C#
/// <summary>
|
|
///********************************************************
|
|
/// 脚本功能:交互Manager
|
|
/// 创建人: GD
|
|
/// 创建时间: 2023/09/20 14:12
|
|
///********************************************************
|
|
/// </summary>
|
|
using LitJson;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using ZXKFramework;
|
|
public class InteractionManager : MonoBehaviour
|
|
{
|
|
Transform root;
|
|
/// <summary>
|
|
/// 所有交互的物体
|
|
/// </summary>
|
|
public Dictionary<string, GameObject> _allInteraction;
|
|
OperatingType type;
|
|
public void Init(OperatingType type)
|
|
{
|
|
root = GameObject.Find("Root").transform;
|
|
Game.Instance.res.Load<TextAsset>(MVC.GetModel<DongWuYiXue.Main.GameModel>().mainData.folder + "/ExcelData/ExcelToJson/BaseData.json", args => {
|
|
string interactionObjs = "";
|
|
List<JsonData> data = JsonMapper.ToObject<List<JsonData>>(args.text);
|
|
for (int i = 0; i < data.Count; i++)
|
|
{
|
|
interactionObjs += data[i]["obj"].ToString() + "|";
|
|
}
|
|
this.type = type;
|
|
_allInteraction = new Dictionary<string, GameObject>();
|
|
InitGetOrAddComponent(interactionObjs);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 开启物体的可交互
|
|
/// </summary>
|
|
public void EnableInteraction(string name)
|
|
{
|
|
if (_allInteraction[name].TryGetComponent(out Collider collider))
|
|
{
|
|
collider.enabled = true;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 关闭物体的可交互
|
|
/// </summary>
|
|
public void DisableInteraction(string name)
|
|
{
|
|
if (_allInteraction[name].TryGetComponent(out Collider collider))
|
|
{
|
|
collider.enabled = false;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 根据不同平台初始化
|
|
/// </summary>
|
|
public void InitGetOrAddComponent(string value)
|
|
{
|
|
if (string.IsNullOrEmpty(value)) return;
|
|
string[] allInteraction = value.Split('|');
|
|
switch (type)
|
|
{
|
|
case OperatingType.FirstPerson:
|
|
case OperatingType.Hardware:
|
|
case OperatingType.Touch:
|
|
for (int j = 0; j < allInteraction.Length; j++)
|
|
{
|
|
if (!string.IsNullOrEmpty(allInteraction[j]))
|
|
{
|
|
if (!_allInteraction.ContainsKey(allInteraction[j]))
|
|
{
|
|
GameObject go = root.FindFirst($"{allInteraction[j]}");
|
|
if (go == null) continue;
|
|
if (go.TryGetComponent(out Collider collider))
|
|
{
|
|
go.GetOrAddComponent<PC_DragEvent>();
|
|
go.GetOrAddComponent<PC_DownUpEvent>();
|
|
go.GetOrAddComponent<PC_TriggerEvent>();
|
|
go.GetOrAddComponent<PC_ShowHideEvent>();
|
|
go.GetOrAddComponent<ResetPosRot>();
|
|
}
|
|
_allInteraction.Add(allInteraction[j], go);
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|