Version: 2022.3

AssetPostprocessor.OnPreprocessCameraDescription(CameraDescription,Camera, AnimationClip[])

切换到手册

参数

description A CameraDescription that describes the imported camera properties and animations.
camera The camera generated by the Model Importer.
animations Model Importer 生成的动画剪辑。

描述

Add this function to a subclass to recieve a notification when a camera is imported from a Model Importer.

Unity only calls this function when ModelImporter.ImportCameras is true. This function gives you control over camera properties and animations during the model import process. The CameraDescription structure contains all the camera data from the imported file. You can use it to populate the camera, its GameObject, and animation clips.

// 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; } }