Ejemplo n.º 1
0
        public SmoothSaveDataSource(NamedVariable fsmVariable, string key, bool isGlobal, Fsm fsm)
        {
            /*
             * Variables have three categories of identification
             *      Type	ID
             *      Keyed	Variable Type + Array + Key
             *      Global	Variable Type + Array + Variable Name + Global
             *      Local	Variable Type + Array + Variable Name + Local + Scene + Owner + FSM
             */

            // Check common data
            if (fsmVariable == null)
            {
                throw new ArgumentNullException("fsmVariable", "Cannot create data source for null FSM variable");
            }

            // Work around RawValue bug for array variables, case 1364
            fsmVariable.ToString();

            // Assign common properties
            _fsmVariable = fsmVariable;
            Key          = StringEmptyToNull(key);
            if (_fsmVariable.VariableType == VariableType.Array)
            {
                IsArray = true;
                var realType = FsmUtility.GetVariableRealType(((FsmArray)_fsmVariable).ElementType);
                if (realType == typeof(bool))
                {
                    FsmVariableType = VariableType.Bool;
                }
                else if (realType == typeof(Color))
                {
                    FsmVariableType = VariableType.Color;
                }
                else if (realType == typeof(Enum))
                {
                    FsmVariableType = VariableType.Enum;
                }
                else if (realType == typeof(float))
                {
                    FsmVariableType = VariableType.Float;
                }
                else if (realType == typeof(int))
                {
                    FsmVariableType = VariableType.Int;
                }
                else if (realType == typeof(Quaternion))
                {
                    FsmVariableType = VariableType.Quaternion;
                }
                else if (realType == typeof(Rect))
                {
                    FsmVariableType = VariableType.Rect;
                }
                else if (realType == typeof(string))
                {
                    FsmVariableType = VariableType.String;
                }
                else if (realType == typeof(Vector2))
                {
                    FsmVariableType = VariableType.Vector2;
                }
                else if (realType == typeof(Vector3))
                {
                    FsmVariableType = VariableType.Vector3;
                }
                else
                {
                    throw new NotSupportedException("Invalid FSM variable type: " + realType);
                }
            }
            else
            {
                FsmVariableType = fsmVariable.VariableType;
            }

            // If keyed, serialize and exit
            if (Key != null)
            {
                Serialize();
                return;
            }

            // Assign global and local properties
            IsGlobal        = isGlobal;
            FsmVariableName = fsmVariable.Name;

            // Check global and local data
            if (string.IsNullOrEmpty(FsmVariableName))
            {
                throw new ArgumentNullException("fsmVariable.Name", "Cannot create global or FSM data source with null or empty FSM variable name");
            }

            // If global, serialize and exit
            if (IsGlobal)
            {
                Serialize();
                return;
            }

            // Check local data
            if (fsm == null)
            {
                throw new ArgumentNullException("fsm", "Cannot create FSM data source with null or empty FSM");
            }

            // Assign local properties
#if UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2
            SceneName = StringEmptyToNull(Application.loadedLevelName);
#else
            SceneName = StringEmptyToNull(SceneManager.GetActiveScene().name);
#endif
            OwnerName = StringEmptyToNull(fsm.OwnerName);
            FsmName   = StringEmptyToNull(fsm.Name);

            // Check local data
            if (SceneName == null)
            {
                throw new NotSupportedException("Cannot create FSM data source without loaded scene");
            }
            if (OwnerName == null)
            {
                throw new ArgumentNullException("fsm.OwnerName", "Cannot create FSM data source with null or empty owner name");
            }
            if (FsmName == null)
            {
                throw new ArgumentNullException("fsm.Name", "Cannot create FSM data source with null or empty FSM name");
            }

            // Serialize and exit
            Serialize();
        }