Ejemplo n.º 1
0
        private AnimatorControllerLayer CreateVolumeLayer(string rootName, string paramName, AnimationClip volumeSet)
        {
            AnimatorControllerLayer layer = new AnimatorControllerLayer {
                name          = paramName,
                defaultWeight = 1,
                stateMachine  = new AnimatorStateMachine()
                {
                    name = paramName
                }
            };
            AnimatorStateMachine stateMachine = layer.stateMachine;

            AnimatorState waitState = stateMachine.AddState("Wait");

            AnimatorState toggleState = stateMachine.AddState(paramName);

            toggleState.timeParameter       = paramName;
            toggleState.timeParameterActive = true;
            toggleState.motion = volumeSet;
            waitState.AddTransition(toggleState).AddCondition(AnimatorConditionMode.Greater, 0, paramName);
            Avatar8Track.CreateAsset(AudioClip, volumeSet, paramName + ".anim");

            return(layer);
        }
Ejemplo n.º 2
0
        public bool BurnTracks(List <AudioClip> libAssets)
        {
            if (null == fxAnimator)
            {
                throw new System.Exception("We need your FX Animator Controller");
            }
            if (null == AudioClip)
            {
                throw new System.Exception("We need an AudioClip from your Song Library");
            }
            if (null == _8TrackObject)
            {
                throw new System.Exception("8Track object was not set");
            }
            _8TrackObject.SetActive(false);

            int childIndex = 0;

            while (true)
            {
                if (0 == _8TrackObject.transform.childCount || childIndex >= _8TrackObject.transform.childCount)
                {
                    break;
                }
                else if (_8TrackObject.transform.GetChild(childIndex).gameObject.name.StartsWith("Track"))
                {
                    Debug.Log("deleting child " + _8TrackObject.transform.GetChild(childIndex).gameObject.name);
                    DestroyImmediate(_8TrackObject.transform.GetChild(childIndex).gameObject);
                }
                else
                {
                    childIndex++;
                }
            }

            RemoveExistingAnimatorLayer(fxAnimator, _8TrackObject.name);
            HandleAnimatorParameters(fxAnimator, _8TrackObject.name);

            //--- --- Volume Animation Controller --- ---
            AnimationClip volumeSet = new AnimationClip {
                name = VolumeEPName
            };

            //--- --- Track Animation Controller --- ---
            AnimatorControllerLayer trackLayer = new AnimatorControllerLayer {
                name          = _8TrackObject.name,
                defaultWeight = 1,
                stateMachine  = new AnimatorStateMachine()
                {
                    name = _8TrackObject.name
                }
            };

            fxAnimator.AddLayer(trackLayer);
            AnimatorStateMachine stateMachine = trackLayer.stateMachine;

            //--- --- No Track --- ---
            int           trackCount   = 0;
            AnimatorState defaultState = stateMachine.AddState("Stop");

            defaultState.speed = 100;
            AnimatorStateTransition anyToWait = stateMachine.AddAnyStateTransition(defaultState);

            anyToWait.AddCondition(AnimatorConditionMode.Equals, trackCount, TrackEPName);
            anyToWait.hasExitTime = true;
            anyToWait.exitTime    = 0;
            anyToWait.duration    = 0;

            AnimatorState loopState = stateMachine.AddState("LoopOff");

            loopState.speed = 100;
            if (!PlayerLoops)
            {
                PersistAnimator(fxAnimator);
                VRCAvatarParameterDriver driver = loopState.AddStateMachineBehaviour <VRCAvatarParameterDriver>();
                driver.parameters.Add(new VRC.SDKBase.VRC_AvatarParameterDriver.Parameter()
                {
                    name  = TrackEPName,
                    value = 0,
                    type  = VRC.SDKBase.VRC_AvatarParameterDriver.ChangeType.Set
                });
            }
            AnimatorStateTransition loopExit = loopState.AddExitTransition();

            loopExit.hasExitTime = true;
            loopExit.exitTime    = 0;
            loopExit.duration    = 0;

            //--- --- All Tracks --- ---
            foreach (Object asset in libAssets)
            {
                if (asset is AudioClip)
                {
                    //Animation setup
                    string trackName = "Track " + ++trackCount;

                    AnimationClip tracktoggle = new AnimationClip {
                        name = trackName
                    };

                    AudioClip clip = asset as AudioClip;

                    tracktoggle.SetCurve(_8TrackObject.name, typeof(GameObject), "m_IsActive", AnimationCurve.Linear(0.0f, 1f, clip.length, 1f));
                    tracktoggle.SetCurve(_8TrackObject.name + "/" + trackName, typeof(GameObject), "m_IsActive", AnimationCurve.Linear(0.0f, 1f, clip.length, 1f));
                    AnimatorState state = stateMachine.AddState(trackName);
                    state.motion = tracktoggle;
                    AnimatorStateTransition toTrack = defaultState.AddTransition(state);
                    toTrack.AddCondition(AnimatorConditionMode.Equals, trackCount, TrackEPName);
                    toTrack.hasExitTime = true;
                    toTrack.exitTime    = 0;
                    toTrack.duration    = 0;
                    AnimatorStateTransition trackOut = state.AddTransition(loopState);
                    trackOut.hasExitTime = true;
                    trackOut.exitTime    = 1;
                    trackOut.duration    = 0;
                    AnimatorStateTransition trackSwitch = state.AddTransition(defaultState);
                    trackSwitch.AddCondition(AnimatorConditionMode.NotEqual, trackCount, TrackEPName);
                    trackSwitch.duration = 0;
                    Avatar8Track.CreateAsset(AudioClip, tracktoggle, trackName + ".anim");

                    //volume
                    volumeSet.SetCurve(_8TrackObject.name + "/" + trackName, typeof(AudioSource), "m_Volume", AnimationCurve.Linear(0.0f, 0f, 0.033333335f, 1f));

                    //Object setup
                    GameObject trackObject = new GameObject(trackName, new System.Type[] { typeof(AudioSource) });
                    trackObject.transform.SetParent(_8TrackObject.transform);
                    trackObject.SetActive(false);
                    //trackObject.AddComponent<AudioSource>();
                    Avatar8Track.CopyComponent(_8TrackObject.GetComponent <AudioSource>(), trackObject.GetComponent <AudioSource>());
                    AudioSource source = trackObject.GetComponent <AudioSource>();
                    source.enabled   = true;
                    source.clip      = clip;
                    trackObject.name = trackName;
                }
            }

            fxAnimator.AddLayer(CreateVolumeLayer(_8TrackObject.name, VolumeEPName, volumeSet));
            PersistAnimator(fxAnimator);

            return(true);
        }