#if NEVER // 使用编译指令确保此文件永远不会被编译
// Unity引擎集成示例代码 - 方法一:直接引用.NET库
//
// 【适用场景】
// - 快速原型开发和单一项目使用
// - 直接将 ThreatSource.dll 复制到 Assets/Plugins/ 目录
// - 不需要复杂的Package管理
//
// 【前置条件】
// 1. 将 ThreatSource.dll 放入 Assets/Plugins/
// 2. 将 data/ 目录复制到 Assets/StreamingAssets/ThreatSource/
// 3. Unity项目设置 Api Compatibility Level 为 .NET Standard 2.1+
//
// 展示如何将Unity引擎与ThreatSource仿真系统集成
// 需要实现的核心功能: 实体同步、事件处理、状态管理
using ThreatSource.Simulation;
using ThreatSource.Data;
using ThreatSource.Missile;
using UnityEngine; // 仅用于示例,实际项目中需要引用真实的Unity命名空间
///
/// Unity引擎适配器类,演示如何将Unity引擎集成到仿真系统中
///
///
/// 该类提供了以下功能:
/// - Unity引擎与仿真系统的双向通信
/// - GameObject与仿真实体的映射和同步
/// - MonoBehaviour生命周期管理
/// - 事件的发布和订阅
/// 本示例代码仅作为参考,展示了基本的集成方法
///
public class UnityThreatSourceAdapter : MonoBehaviour
{
///
/// 仿真管理器实例
///
private ISimulationManager _simulationManager;
///
/// 数据管理器实例
///
private ThreatSourceDataManager _dataManager;
///
/// 实体映射字典:仿真实体ID -> Unity GameObject
///
private Dictionary _entityGameObjects = new Dictionary();
///
/// 导弹预制体路径
///
[SerializeField]
private string missilePrefabPath = "Prefabs/Missile";
///
/// 目标预制体路径
///
[SerializeField]
private string targetPrefabPath = "Prefabs/Target";
///
/// 仿真时间步长
///
[SerializeField]
private float simulationTimeStep = 0.02f;
///
/// Unity生命周期方法:初始化
///
private void Awake()
{
InitializeSimulation();
}
///
/// 初始化仿真系统
///
private void InitializeSimulation()
{
try
{
// 创建仿真管理器和数据管理器
_simulationManager = new SimulationManager();
_dataManager = new ThreatSourceDataManager();
// 启动仿真
_simulationManager.StartSimulation(simulationTimeStep);
// 订阅仿真事件
_simulationManager.Subscribe(OnMissileFireEvent);
_simulationManager.Subscribe(OnMissileExplodeEvent);
_simulationManager.Subscribe(OnFlightPhaseChangeEvent);
Debug.Log("ThreatSource仿真系统初始化成功");
}
catch (System.Exception ex)
{
Debug.LogError($"仿真系统初始化失败: {ex.Message}");
}
}
///
/// Unity生命周期方法:更新
///
private void Update()
{
if (_simulationManager != null)
{
// 更新仿真
_simulationManager.UpdateSimulation();
// 同步实体状态
SyncEntityStates();
}
}
///
/// 同步仿真实体状态到Unity GameObject
///
private void SyncEntityStates()
{
foreach (var kvp in _entityGameObjects)
{
string entityId = kvp.Key;
GameObject gameObject = kvp.Value;
// 获取仿真实体
var entity = _simulationManager.GetEntity(entityId);
if (entity != null && entity.KState != null)
{
// 同步位置
gameObject.transform.position = new Vector3(
entity.KState.Position.X,
entity.KState.Position.Z, // Unity使用Y作为高度
entity.KState.Position.Y
);
// 同步旋转
var orientation = entity.KState.Orientation;
gameObject.transform.rotation = Quaternion.Euler(
orientation.Pitch * Mathf.Rad2Deg,
orientation.Yaw * Mathf.Rad2Deg,
orientation.Roll * Mathf.Rad2Deg
);
}
}
}
///
/// 创建导弹实体
///
/// 导弹类型
/// 初始位置
/// 目标ID
public void CreateMissile(string missileType, Vector3 position, string targetId)
{
try
{
// 从配置创建导弹
var missileData = _dataManager.GetMissile(missileType);
string missileId = $"missile_{System.Guid.NewGuid():N}";
var missile = new InfraredImagingTerminalGuidanceMissile(missileId, missileData)
{
KState = new KinematicState
{
Position = new ThreatSource.Simulation.Vector3(position.x, position.z, position.y),
Velocity = new ThreatSource.Simulation.Vector3(0, 0, 0),
Orientation = new Orientation(0, 0, 0)
}
};
// 注册到仿真系统
_simulationManager.RegisterEntity(missile);
// 在Unity中创建GameObject
var missilePrefab = Resources.Load(missilePrefabPath);
if (missilePrefab != null)
{
var missileGameObject = Instantiate(missilePrefab, position, Quaternion.identity);
missileGameObject.name = missileId;
// 添加导弹控制组件
var missileController = missileGameObject.GetComponent();
if (missileController == null)
{
missileController = missileGameObject.AddComponent();
}
missileController.Initialize(missileId, targetId);
// 建立映射关系
_entityGameObjects[missileId] = missileGameObject;
Debug.Log($"创建导弹: {missileId}, 目标: {targetId}");
}
else
{
Debug.LogError($"无法加载导弹预制体: {missilePrefabPath}");
}
}
catch (System.Exception ex)
{
Debug.LogError($"创建导弹失败: {ex.Message}");
}
}
///
/// 创建目标实体
///
/// 目标ID
/// 位置
/// 速度
public void CreateTarget(string targetId, Vector3 position, Vector3 velocity)
{
try
{
// 创建目标实体
var target = new BaseEquipment(targetId)
{
KState = new KinematicState
{
Position = new ThreatSource.Simulation.Vector3(position.x, position.z, position.y),
Velocity = new ThreatSource.Simulation.Vector3(velocity.x, velocity.z, velocity.y),
Orientation = new Orientation(0, 0, 0)
}
};
// 注册到仿真系统
_simulationManager.RegisterEntity(target);
// 在Unity中创建GameObject
var targetPrefab = Resources.Load(targetPrefabPath);
if (targetPrefab != null)
{
var targetGameObject = Instantiate(targetPrefab, position, Quaternion.identity);
targetGameObject.name = targetId;
// 建立映射关系
_entityGameObjects[targetId] = targetGameObject;
Debug.Log($"创建目标: {targetId}");
}
else
{
Debug.LogError($"无法加载目标预制体: {targetPrefabPath}");
}
}
catch (System.Exception ex)
{
Debug.LogError($"创建目标失败: {ex.Message}");
}
}
///
/// 处理导弹发射事件
///
private void OnMissileFireEvent(MissileFireEvent evt)
{
Debug.Log($"导弹发射事件: {evt.SenderId} -> {evt.TargetId}");
// 可以在这里添加视觉效果,如发射烟雾、声音等
var missileGameObject = GetGameObject(evt.SenderId);
if (missileGameObject != null)
{
// 播放发射特效
var particleSystem = missileGameObject.GetComponentInChildren();
if (particleSystem != null)
{
particleSystem.Play();
}
}
}
///
/// 处理导弹爆炸事件
///
private void OnMissileExplodeEvent(MissileExplodeEvent evt)
{
Debug.Log($"导弹爆炸事件: {evt.SenderId} 在位置 {evt.Position}");
var missileGameObject = GetGameObject(evt.SenderId);
if (missileGameObject != null)
{
// 播放爆炸特效
var explosionPrefab = Resources.Load("Prefabs/Explosion");
if (explosionPrefab != null)
{
Instantiate(explosionPrefab, missileGameObject.transform.position, Quaternion.identity);
}
// 销毁导弹GameObject
Destroy(missileGameObject);
_entityGameObjects.Remove(evt.SenderId);
}
}
///
/// 处理飞行阶段变化事件
///
private void OnFlightPhaseChangeEvent(FlightPhaseChangeEvent evt)
{
Debug.Log($"导弹 {evt.SenderId} 进入 {evt.NewPhase} 阶段");
// 可以根据飞行阶段改变导弹的视觉表现
var missileGameObject = GetGameObject(evt.SenderId);
if (missileGameObject != null)
{
var missileController = missileGameObject.GetComponent();
if (missileController != null)
{
missileController.OnFlightPhaseChanged(evt.NewPhase);
}
}
}
///
/// 根据实体ID获取对应的GameObject
///
private GameObject GetGameObject(string entityId)
{
_entityGameObjects.TryGetValue(entityId, out GameObject gameObject);
return gameObject;
}
///
/// Unity生命周期方法:销毁
///
private void OnDestroy()
{
CleanupSimulation();
}
///
/// 清理仿真系统
///
private void CleanupSimulation()
{
if (_simulationManager != null)
{
_simulationManager.StopSimulation();
_simulationManager = null;
}
// 清理GameObject映射
foreach (var gameObject in _entityGameObjects.Values)
{
if (gameObject != null)
{
Destroy(gameObject);
}
}
_entityGameObjects.Clear();
Debug.Log("ThreatSource仿真系统已清理");
}
}
///
/// 导弹控制器组件
///
public class MissileController : MonoBehaviour
{
private string _missileId;
private string _targetId;
private TrailRenderer _trailRenderer;
public void Initialize(string missileId, string targetId)
{
_missileId = missileId;
_targetId = targetId;
// 获取拖尾渲染器
_trailRenderer = GetComponent();
if (_trailRenderer == null)
{
_trailRenderer = gameObject.AddComponent();
}
}
public void OnFlightPhaseChanged(FlightPhase newPhase)
{
// 根据飞行阶段调整视觉效果
switch (newPhase)
{
case FlightPhase.Boost:
// 助推阶段:显示推进器火焰
break;
case FlightPhase.Midcourse:
// 中段飞行:调整拖尾效果
if (_trailRenderer != null)
{
_trailRenderer.enabled = true;
}
break;
case FlightPhase.Terminal:
// 末段制导:可能改变颜色或效果
if (_trailRenderer != null)
{
_trailRenderer.material.color = Color.red;
}
break;
}
}
}
///
/// 示例使用脚本
///
public class ThreatSourceExample : MonoBehaviour
{
private UnityThreatSourceAdapter _adapter;
private void Start()
{
// 获取适配器组件
_adapter = FindObjectOfType();
if (_adapter == null)
{
_adapter = gameObject.AddComponent();
}
// 延迟创建示例场景
Invoke(nameof(CreateExampleScene), 1.0f);
}
private void CreateExampleScene()
{
// 创建目标
_adapter.CreateTarget("target_001", new Vector3(1000, 0, 100), new Vector3(-50, 0, 0));
// 创建导弹
_adapter.CreateMissile("IR_Missile_Example", Vector3.zero, "target_001");
}
}
#endif // 结束编译指令块