Пример #1
0
            public SerializedDeclaration(Declaration decl)
            {
                this.name        = decl.Name;
                this.type        = decl.ReturnType;
                this.description = decl.Description;

                sourceYarnAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(decl.SourceFileName);

                switch (this.type)
                {
                case Type.Number:
                    this.defaultValueNumber = (float)decl.DefaultValue;
                    break;

                case Type.String:
                    this.defaultValueString = (string)decl.DefaultValue;
                    break;

                case Type.Bool:
                    this.defaultValueBool = (bool)decl.DefaultValue;
                    break;

                default:
                    throw new System.InvalidOperationException($"Invalid declaration type {decl.ReturnType}");
                }
            }
        /// <summary>
        /// Used internally by serialization functions to wrap around the
        /// SetValue() methods.
        /// </summary>
        void SetVariable(string name, Yarn.Type type, string value)
        {
            switch (type)
            {
            case Type.Bool:
                bool newBool;
                if (bool.TryParse(value, out newBool))
                {
                    SetValue(name, newBool);
                }
                else
                {
                    throw new System.InvalidCastException($"Couldn't initialize default variable {name} with value {value} as Bool");
                }
                break;

            case Type.Number:
                float newNumber;
                if (float.TryParse(value, out newNumber))
                {     // TODO: this won't work for different cultures (e.g. French write "0.53" as "0,53")
                    SetValue(name, newNumber);
                }
                else
                {
                    throw new System.InvalidCastException($"Couldn't initialize default variable {name} with value {value} as Number (Float)");
                }
                break;

            case Type.String:
            case Type.Undefined:
            default:
                SetValue(name, value);     // no special type conversion required
                break;
            }
        }