U3D中的又一个坑
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEditor; 4 using UnityEngine; 5 6 public class animImport : AssetPostprocessor 7 { 8 9 //fbx动画导入前的处理,对动画集进行切分,转成单个的动画子集 10 void OnPreprocessAnimation() 11 { 12 var modelImporter = assetImporter as ModelImporter; 13 var anims = new ModelImporterClipAnimation[10]; 14 for (var i = 0; i < anims.Length; ++i) 15 { 16 anims[i] = new ModelImporterClipAnimation(); 17 anims[i].takeName = "hello-" + i; 18 anims[i].name = "hello-" + i; 19 } 20 21 //错误写法 22 //这里的clipAnimations是个属性,对它赋值时会调用它的set方法,该方法会检测数组的每个元素,有一个为NULL就报错,示例如下: 23 modelImporter.clipAnimations = new ModelImporterClipAnimation[10]; //有10个元素的数组,每个都是NULL,运行时报错 24 25 //正确写法 26 //modelImporter.clipAnimations =操作一旦执行,对clipAnimations中的任何元素的更改都不再起作用,必须在此操作前执行更改 27 modelImporter.clipAnimations = anims; 28 for (var i = 0; i < modelImporter.clipAnimations.Length; ++i) 29 { 30 //anims[i].name更改了,但modelImporter.clipAnimations[i].name没更改, 31 //虽然语法上它们仍指向同一变量,应该是内部特殊处理 32 anims[i].name = "ani-" + i; 33 anims[i].takeName = "ani-" + i; 34 } 35 36 } 37 }