description | 一个描述导入的摄像机属性和动画的LightDescription。 |
light | 模型导入器生成的灯光。 |
animations | 模型导入器生成的动画片段。 |
将此函数添加到子类中,以在从模型导入器导入灯光时接收通知。
仅当 ModelImporter.ImportLights 为 true 时,Unity 才会调用此函数。此函数使您可以在模型导入过程中控制灯光属性和动画。LightDescription 结构包含导入文件中的所有灯光数据。您可以使用它来填充灯光、其 GameObject 和动画片段。
using System.Collections.Generic; using UnityEditor; using UnityEngine; using UnityEditor.AssetImporters;
public class SetupLightFromCameraDescription : AssetPostprocessor { void OnPreprocessLightDescription(LightDescription description, Light light, AnimationClip[] clips) { var modelImporter = assetImporter as ModelImporter; var globalScale = modelImporter.globalScale;
description.TryGetProperty("LightType", out int lightType);
switch (lightType) { case 0: light.type = LightType.Point; break; case 1: light.type = LightType.Directional; break; case 2: light.type = LightType.Spot; break; case 3: light.type = LightType.Rectangle; break; }
description.TryGetProperty("Intensity", out float intensity); description.TryGetProperty("Color", out Vector4 color); description.TryGetProperty("CastShadows", out int castShadows); description.TryGetProperty("FarAttenuationEnd", out float farAttenuationEnd); description.TryGetProperty("OuterAngle", out float outerAngle);
light.intensity = intensity * 0.01f; light.color = color; light.shadows = castShadows == 1 ? LightShadows.Hard : LightShadows.None;
if (farAttenuationEnd > 0) light.range = farAttenuationEnd * globalScale;
if (outerAngle > 0) light.spotAngle = outerAngle; } }