NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/GameWrapper/Camera/CameraInfoData.cs

76 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using UnityEngine;
namespace GameWrapper
{
/// <summary>
/// 相机信息数据类,用于跨程序集传递相机配置
/// 替代原来的MapData.CameraInfo
/// </summary>
[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;
/// <summary>
/// 从MapData.CameraInfo创建CameraInfoData的静态工厂方法
/// </summary>
/// <param name="cameraInfo">MapData.CameraInfo实例</param>
/// <returns>对应的CameraInfoData实例</returns>
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;
}
/// <summary>
/// 尝试通过反射获取对象的属性值
/// </summary>
private static void TryGetPropertyValue<T>(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}");
}
}
}
}