Beispiel #1
0
        /// <summary>
        /// Finds an attribute (property or field) on the target object
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public virtual bool FindAttribute(string propertyName)
        {
            FieldInfo    fieldInfo = null;
            PropertyInfo propInfo  = null;

            TargetAttribute = null;

            propInfo = TargetObject.GetType().GetProperty(propertyName);
            if (propInfo == null)
            {
                fieldInfo = TargetObject.GetType().GetField(propertyName);
            }
            if (propInfo != null)
            {
                TargetAttribute = new MonoAttribute(TargetObject, MonoAttribute.MemberTypes.Property, propInfo, null, propertyName);
            }
            if (fieldInfo != null)
            {
                TargetAttribute = new MonoAttribute(TargetObject, MonoAttribute.MemberTypes.Field, null, fieldInfo, propertyName);
            }
            if (PropertyName == _undefinedString)
            {
                Debug.LogError("FloatController " + this.name + " : you need to pick a property from the Property list");
                return(false);
            }
            if ((propInfo == null) && (fieldInfo == null))
            {
                Debug.LogError("FloatController " + this.name + " couldn't find any property or field named " + propertyName + " on " + TargetObject.name);
                return(false);
            }

            if (TargetAttribute.MemberType == MonoAttribute.MemberTypes.Property)
            {
                TargetAttribute.MemberPropertyInfo = TargetObject.GetType().GetProperty(TargetAttribute.MemberName);
            }
            else if (TargetAttribute.MemberType == MonoAttribute.MemberTypes.Field)
            {
                TargetAttribute.MemberFieldInfo = TargetObject.GetType().GetField(TargetAttribute.MemberName);
            }

            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// On Update, we move our value based on the defined settings
        /// </summary>
        protected virtual void Update()
        {
            _targetObjectLastFrame    = TargetObject;
            _targetAttributeLastFrame = TargetAttribute;

            if ((TargetObject == null) || (TargetAttribute == null) || (!_attributeFound))
            {
                return;
            }

            switch (ControlMode)
            {
            case ControlModes.PingPong:

                if (GetTime() - _lastPingPongPauseAt < PingPongPauseDuration)
                {
                    return;
                }
                PingPong += GetDeltaTime() * _pingPongDirection;

                if (PingPong < 0f)
                {
                    PingPong             = 0f;
                    _pingPongDirection   = -_pingPongDirection;
                    _lastPingPongPauseAt = GetTime();
                }

                if (PingPong > Duration)
                {
                    PingPong             = Duration;
                    _pingPongDirection   = -_pingPongDirection;
                    _lastPingPongPauseAt = GetTime();
                }
                CurrentValue           = MMTween.Tween(PingPong, 0f, Duration, MinValue, MaxValue, Curve);
                CurrentValueNormalized = MMMaths.Remap(CurrentValue, MinValue, MaxValue, 0f, 1f);
                break;

            case ControlModes.Random:
                _elapsedTime          += GetDeltaTime();
                CurrentValueNormalized = Mathf.PerlinNoise(_randomFrequency * _elapsedTime, _randomShift);
                if (RemapNoiseValues)
                {
                    CurrentValue = CurrentValueNormalized;
                    CurrentValue = MMMaths.Remap(CurrentValue, 0f, 1f, RemapNoiseZero, RemapNoiseOne);
                }
                else
                {
                    CurrentValue = (CurrentValueNormalized * 2.0f - 1.0f) * _randomAmplitude;
                }
                break;

            case ControlModes.OneTime:
                if (!_shaking)
                {
                    return;
                }
                _remappedTimeSinceStart = MMMaths.Remap(Time.time - _shakeStartTimestamp, 0f, OneTimeDuration, 0f, 1f);
                CurrentValueNormalized  = OneTimeCurve.Evaluate(_remappedTimeSinceStart);
                CurrentValue            = MMMaths.Remap(CurrentValueNormalized, 0f, 1f, OneTimeRemapMin, OneTimeRemapMax);
                CurrentValue           *= OneTimeAmplitude;
                break;

            case ControlModes.AudioAnalyzer:
                if (AudioAnalyzerMode == AudioAnalyzerModes.Beat)
                {
                    CurrentValue = AudioAnalyzer.Beats[BeatID].CurrentValue * AudioAnalyzerMultiplier;
                }
                else
                {
                    CurrentValue = AudioAnalyzer.NormalizedBufferedBandLevels[NormalizedLevelID] * AudioAnalyzerMultiplier;
                }
                CurrentValueNormalized = Mathf.Clamp(AudioAnalyzer.Beats[BeatID].CurrentValue, 0f, 1f);
                break;

            case ControlModes.Driven:
                CurrentValue           = DrivenLevel;
                CurrentValueNormalized = Mathf.Clamp(CurrentValue, 0f, 1f);
                break;

            case ControlModes.ToDestination:
                if (!_shaking)
                {
                    return;
                }
                _remappedTimeSinceStart = MMMaths.Remap(Time.time - _shakeStartTimestamp, 0f, ToDestinationDuration, 0f, 1f);
                float time = ToDestinationCurve.Evaluate(_remappedTimeSinceStart);
                CurrentValue           = Mathf.LerpUnclamped(_initialValue, ToDestinationValue, time);
                CurrentValueNormalized = MMMaths.Remap(CurrentValue, _initialValue, ToDestinationValue, 0f, 1f);
                break;
            }


            if (AddToInitialValue)
            {
                CurrentValue += InitialValue;
            }

            if (ControlMode == ControlModes.OneTime)
            {
                if (_shaking && (Time.time - _shakeStartTimestamp > OneTimeDuration))
                {
                    _shaking = false;
                    if (RevertToInitialValueAfterEnd)
                    {
                        CurrentValue = InitialValue;
                        TargetAttribute.SetValue(CurrentValue);
                    }
                    else
                    {
                        CurrentValue  = OneTimeCurve.Evaluate(1f);
                        CurrentValue  = MMMaths.Remap(CurrentValue, 0f, 1f, OneTimeRemapMin, OneTimeRemapMax);
                        CurrentValue *= OneTimeAmplitude;
                        TargetAttribute.SetValue(CurrentValue);
                    }
                    if (DisableAfterOneTime)
                    {
                        this.enabled = false;
                    }
                    if (DisableGameObjectAfterOneTime)
                    {
                        this.gameObject.SetActive(false);
                    }
                    return;
                }
            }

            if (ControlMode == ControlModes.ToDestination)
            {
                if (_shaking && (Time.time - _shakeStartTimestamp > ToDestinationDuration))
                {
                    _shaking = false;
                    if (RevertToInitialValueAfterEnd)
                    {
                        CurrentValue = InitialValue;
                    }
                    else
                    {
                        CurrentValue = ToDestinationValue;
                    }
                    TargetAttribute.SetValue(CurrentValue);

                    if (DisableAfterOneTime)
                    {
                        this.enabled = false;
                    }
                    if (DisableGameObjectAfterOneTime)
                    {
                        this.gameObject.SetActive(false);
                    }
                    return;
                }
            }

            TargetAttribute.SetValue(CurrentValue);
        }