using UnityEngine; namespace GameWrapper { /// /// 相机信息数据类,用于跨程序集传递相机配置 /// 替代原来的MapData.CameraInfo /// [System.Serializable] public class CameraInfoData { // 边距设置 public float rightMarginInPre = 1.5f; public float leftMarginInPre = 1.5f; public float topMarginInInPre = 0.9f; public float bottomMarginInPre = 0.9f; // FOV设置 public float initialFOV = 30f; /// /// 从MapData.CameraInfo创建CameraInfoData的静态工厂方法 /// /// MapData.CameraInfo实例 /// 对应的CameraInfoData实例 public static CameraInfoData FromMapDataCameraInfo(object cameraInfo) { // 如果传入为null,返回默认值 if (cameraInfo == null) { return null; } // 创建新的实例 var result = new CameraInfoData(); // 使用反射获取属性值 var type = cameraInfo.GetType(); // TODO 优化 // 获取并设置边距 TryGetPropertyValue(type, cameraInfo, "rightMarginInPre", ref result.rightMarginInPre); TryGetPropertyValue(type, cameraInfo, "leftMarginInPre", ref result.leftMarginInPre); TryGetPropertyValue(type, cameraInfo, "topMarginInInPre", ref result.topMarginInInPre); TryGetPropertyValue(type, cameraInfo, "bottomMarginInPre", ref result.bottomMarginInPre); // 获取并设置FOV TryGetPropertyValue(type, cameraInfo, "initialFOV", ref result.initialFOV); return result; } /// /// 尝试通过反射获取对象的属性值 /// private static void TryGetPropertyValue(System.Type type, object obj, string propertyName, ref T targetValue) { try { var property = type.GetProperty(propertyName); if (property != null) { var value = property.GetValue(obj); if (value != null && value is T typedValue) { targetValue = typedValue; } } } catch (System.Exception ex) { Debug.LogWarning($"Failed to get property {propertyName}: {ex.Message}"); } } } }