U3D中的又一个坑

时间:2021-04-05 22:24:30
 using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine; public class animImport : AssetPostprocessor
{ //fbx动画导入前的处理,对动画集进行切分,转成单个的动画子集
void OnPreprocessAnimation()
{
var modelImporter = assetImporter as ModelImporter;
var anims = new ModelImporterClipAnimation[];
for (var i = ; i < anims.Length; ++i)
{
anims[i] = new ModelImporterClipAnimation();
anims[i].takeName = "hello-" + i;
anims[i].name = "hello-" + i;
} //错误写法
//这里的clipAnimations是个属性,对它赋值时会调用它的set方法,该方法会检测数组的每个元素,有一个为NULL就报错,示例如下:
modelImporter.clipAnimations = new ModelImporterClipAnimation[]; //有10个元素的数组,每个都是NULL,运行时报错 //正确写法
//modelImporter.clipAnimations =操作一旦执行,对clipAnimations中的任何元素的更改都不再起作用,必须在此操作前执行更改
modelImporter.clipAnimations = anims;
for (var i = ; i < modelImporter.clipAnimations.Length; ++i)
{
//anims[i].name更改了,但modelImporter.clipAnimations[i].name没更改,
//虽然语法上它们仍指向同一变量,应该是内部特殊处理
anims[i].name = "ani-" + i;
anims[i].takeName = "ani-" + i;
} }
}