Version: 2022.1
LanguageEnglish
  • C#

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

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Parameters

description A CameraDescription that describes the imported camera properties and animations.
camera The camera generated by the Model Importer.
animations The animation clips generated by the Model Importer.

Description

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; } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961