示例#1
0
        public UAvatarPreview(AnimationClip clip, GameObject gameObject)
        {
            var asmUnityEditor    = Assembly.LoadFrom(InternalEditorUtility.GetEditorAssemblyPath());
            var avatarPreviewType = asmUnityEditor.GetType("UnityEditor.AvatarPreview");

            Assert.IsNotNull(instance        = Activator.CreateInstance(avatarPreviewType, new object[] { null, clip }));
            Assert.IsNotNull(fi_m_PreviewDir = avatarPreviewType.GetField("m_PreviewDir", BindingFlags.NonPublic | BindingFlags.Instance));
            Assert.IsNotNull(fi_m_ZoomFactor = avatarPreviewType.GetField("m_ZoomFactor", BindingFlags.NonPublic | BindingFlags.Instance));
            Assert.IsNotNull(dg_set_fps      = EditorCommon.CreateSetFieldDelegate <int>(avatarPreviewType.GetField("fps")));
            Assert.IsNotNull(dg_get_m_SourcePreviewMotion = EditorCommon.CreateGetFieldDelegate <AnimationClip>(avatarPreviewType.GetField("m_SourcePreviewMotion", BindingFlags.NonPublic | BindingFlags.Instance)));
            Assert.IsNotNull(dg_get_IKOnFeet           = (Func <bool>)Delegate.CreateDelegate(typeof(Func <bool>), instance, avatarPreviewType.GetProperty("IKOnFeet").GetGetMethod()));
            Assert.IsNotNull(dg_get_Animator           = (Func <Animator>)Delegate.CreateDelegate(typeof(Func <Animator>), instance, avatarPreviewType.GetProperty("Animator").GetGetMethod()));
            Assert.IsNotNull(dg_get_ShowIKOnFeetButton = (Func <bool>)Delegate.CreateDelegate(typeof(Func <bool>), instance, avatarPreviewType.GetProperty("ShowIKOnFeetButton").GetGetMethod()));
            Assert.IsNotNull(dg_set_ShowIKOnFeetButton = (Action <bool>)Delegate.CreateDelegate(typeof(Action <bool>), instance, avatarPreviewType.GetProperty("ShowIKOnFeetButton").GetSetMethod()));
            Assert.IsNotNull(dg_get_PreviewObject      = (Func <GameObject>)Delegate.CreateDelegate(typeof(Func <GameObject>), instance, avatarPreviewType.GetProperty("PreviewObject").GetGetMethod()));
            Assert.IsNotNull(dg_get_animationClipType  = (Func <ModelImporterAnimationType>)Delegate.CreateDelegate(typeof(Func <ModelImporterAnimationType>), instance, avatarPreviewType.GetProperty("animationClipType").GetGetMethod()));
            Assert.IsNotNull(dg_DoPreviewSettings      = (Action)Delegate.CreateDelegate(typeof(Action), instance, avatarPreviewType.GetMethod("DoPreviewSettings")));
            Assert.IsNotNull(dg_DoAvatarPreview        = (Action <Rect, GUIStyle>)Delegate.CreateDelegate(typeof(Action <Rect, GUIStyle>), instance, avatarPreviewType.GetMethod("DoAvatarPreview")));
#if UNITY_2018_2_OR_NEWER
            Assert.IsNotNull(dg_OnDestroy = (Action)Delegate.CreateDelegate(typeof(Action), instance, avatarPreviewType.GetMethod("OnDisable")));
#else
            Assert.IsNotNull(dg_OnDestroy = (Action)Delegate.CreateDelegate(typeof(Action), instance, avatarPreviewType.GetMethod("OnDestroy")));
#endif
            Assert.IsNotNull(dg_SetPreview         = (Action <GameObject>)Delegate.CreateDelegate(typeof(Action <GameObject>), instance, avatarPreviewType.GetMethod("SetPreview", BindingFlags.NonPublic | BindingFlags.Instance)));
            Assert.IsNotNull(pi_OnAvatarChangeFunc = avatarPreviewType.GetProperty("OnAvatarChangeFunc"));

            uAnimatorController   = new UAnimatorController();
            uAnimatorStateMachine = new UAnimatorStateMachine();
            uAnimatorState        = new UAnimatorState();

            {
                var fi_timeControl = avatarPreviewType.GetField("timeControl");
                uTimeControl             = new UTimeControl(fi_timeControl.GetValue(instance));
                uTimeControl.startTime   = 0f;
                uTimeControl.stopTime    = clip.length;
                uTimeControl.currentTime = 0f;
            }
            uAvatarPreviewSelection = new UAvatarPreviewSelection(asmUnityEditor);

            pi_OnAvatarChangeFunc.SetValue(instance, Delegate.CreateDelegate(pi_OnAvatarChangeFunc.PropertyType, this, GetType().GetMethod("OnAvatarChangeFunc", BindingFlags.NonPublic | BindingFlags.Instance)), null);
            dg_set_fps(instance, (int)clip.frameRate);
            dg_SetPreview(gameObject);

            AnimationUtility.onCurveWasModified += OnCurveWasModified;
        }