说明 | 描述导入的相机属性和动画的 CameraDescription。 |
相机 | 由模型导入器生成的相机。 |
动画 | 由模型导入器生成的动画剪辑。 |
将此函数添加到子类以在从模型导入器导入相机时接收通知。
仅当 ModelImporter.importCameras 为真时,Unity 才调用此函数。此函数让您在模型导入过程中控制相机属性和动画。CameraDescription 结构包含从导入文件中的所有相机数据。您可以使用它填充相机、其 GameObject 和动画剪辑。
// The following code example shows how Unity uses this function to set up a perspective camera from an imported FBX file.
using System.Collections.Generic; using UnityEditor; using UnityEngine; using UnityEditor.AssetImporters;
public class SetupCameraFromCameraDescription : AssetPostprocessor { void OnPreprocessCameraDescription(CameraDescription description, Camera camera, AnimationClip[] clips) { var modelImporter = assetImporter as ModelImporter; var globalScale = modelImporter.globalScale;
description.TryGetProperty("NearPlane", out float nearPlane); description.TryGetProperty("FarPlane", out float farPlane); description.TryGetProperty("ApertureMode", out int apertureMode); description.TryGetProperty("FilmWidth", out float filmWidth); description.TryGetProperty("FilmHeight", out float filmHeight); description.TryGetProperty("FocalLength", out float focalLength);
if (nearPlane >= 0) camera.nearClipPlane = nearPlane * globalScale; if (farPlane >= 0) camera.farClipPlane = farPlane * globalScale;
float fov = 0; switch (apertureMode) { case 0: description.TryGetProperty("FieldOfViewY", out fov); camera.fieldOfView = fov; break; case 1: description.TryGetProperty("FieldOfView", out float alphaH); alphaH /= 2; float focal = (filmWidth * 25.4f) / 2 / Mathf.Tan(alphaH * Mathf.Deg2Rad); float alphaV = Mathf.Atan((filmHeight * 25.4f) / 2 / focal) * Mathf.Rad2Deg; camera.fieldOfView = alphaV * 2; break; case 2: description.TryGetProperty("FieldOfView", out fov); camera.fieldOfView = fov; break; case 3:
if (focalLength != .0f) { float apertureWidth = filmHeight * 25.4f; fov = Mathf.Atan((apertureWidth / focalLength) * 0.5f) * 2.0f * Mathf.Rad2Deg; } break; }
camera.fieldOfView = fov; } }