Exemple #1
0
 /// <summary>
 /// Unity callback called when the behaviour becomes disabled or inactive.
 /// Disables the MonoBehaviour.
 /// </summary>
 void OnDisable()
 {
     if (m_MonoBehaviour != null)
     {
         m_MonoBehaviour.enabled = false;
     }
     else
     {
         Print.LogError("No MonoBehaviour in " + stateName + " (MonoState)", this);
     }
 }
        /// <summary>
        /// Cache member info.
        /// </summary>
        protected bool UpdateMemberInfo()
        {
            ClearCached();
            var type = targetObject.ObjectType;

            m_PropertyInfo = type.GetProperty(propertyName);
            if (m_PropertyInfo == null)
            {
                m_FieldInfo  = type.GetField(propertyName);
                m_MemberInfo = m_FieldInfo;
            }
            else
            {
                m_MemberInfo = m_PropertyInfo;
            }

            if (m_MemberInfo != null)
            {
                // Cache values
                m_CachedTargetType   = type;
                m_CachedPropertyName = propertyName;
                // Get property type
                if (m_FieldInfo != null)
                {
                    m_CachedPropertyType = m_FieldInfo.FieldType;
                    m_CanWrite           = true;
                    m_CanRead            = true;
                }
                else if (m_PropertyInfo != null)
                {
                    m_CachedPropertyType = m_PropertyInfo.PropertyType;
                    m_CanWrite           = m_PropertyInfo.CanWrite;
                    m_CanRead            = m_PropertyInfo.CanRead;
                }
                // Not supported
                else
                {
                    Print.LogError("MemberInfo not supported for: " + propertyName);
                    ClearCached();
                    return(false);
                }
                return(true);
            }
            else
            {
                ClearCached();
            }
            return(false);
        }
        /// <summary>
        /// Unity callback called when the object becomes enabled and active.
        /// Enables the start state, if the start state is null then tries to enable the first one.
        /// </summary>
        public virtual void OnEnable()
        {
            #if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                return;
            }
            #endif

            // Add this parent to the blackboard to receive system events
            if (isRoot)
            {
                blackboard.AddRootParent(this);
            }

            // Enable the concurrent state
            if (m_ConcurrentState != null && !m_ConcurrentState.enabled)
            {
                ((MonoBehaviour)m_ConcurrentState).enabled = true;
            }

            // There is not an enabled state?
            if (m_EnabledState == null || !m_EnabledState.enabled || m_EnabledState.parent != this)
            {
                // Enable the start state
                if (m_StartState != null && m_StartState.parent == this && !m_StartState.enabled)
                {
                    m_StartState.enabled = true;
                }
                // Try to get the first state
                else
                {
                    var states     = this.states;
                    var firstState = states.Count > 0 ? states[0] : null;
                    if (firstState != null && !(firstState is InternalAnyState))
                    {
                        m_StartState         = firstState;
                        m_StartState.enabled = true;
                        Print.LogWarning("Start State not set in \'" + stateName + "\', getting the first one \'" + m_StartState.name + "\' (" + m_StartState.GetType().Name + ").", this);
                    }
                    else
                    {
                        Print.LogError("No state in \'" + stateName + "\' (" + GetType().Name + ").", this);
                    }
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// Sets the fsm.anyState; if it's null.
        /// </summary>
        void Awake()
        {
            var fsm = this.fsm;

            if (fsm != null)
            {
                if (fsm.anyState == null)
                {
                    fsm.anyState = this;
                }
                else if (fsm.anyState != this)
                {
                    Print.LogError("More than one AnyState in FSM.", this.gameObject);
                }
            }
            else
            {
                Print.LogError("AnyState without FSM.", this.gameObject);
            }
        }