示例#1
0
        public static CameraSubStep GenerateFromString(string input)
        {
            var tokens = input.Split(':');

            if (tokens.Length < 4)
            {
                throw new Exception("Cannot convert string " + input + " into a substep!");
            }

            var subStep = new CameraSubStep();

            Vector3 vectorBuffer = Vector3.zero;

            if (!VectorUtility.TryParseVector(tokens[0], ref vectorBuffer))
            {
                throw new Exception("Cannot convert string " + tokens[0] + " into a Vector3!");
            }
            subStep.Position = vectorBuffer;

            if (!VectorUtility.TryParseVector(tokens[1], ref vectorBuffer))
            {
                throw new Exception("Cannot convert string " + tokens[1] + " into a Vector3!");
            }
            subStep.Facing = vectorBuffer;

            float floatBuffer = 0f;

            if (!float.TryParse(tokens[2], out floatBuffer))
            {
                throw new Exception("Cannot convert string " + tokens[2] + " into a float!");
            }
            subStep.Duration = Mathf.Max(floatBuffer, .001f);

            if (!float.TryParse(tokens[3], out floatBuffer))
            {
                throw new Exception("Cannot convert string " + tokens[3] + " into a float!");
            }
            subStep.Delay = Mathf.Max(floatBuffer, .001f);

            return(subStep);
        }
示例#2
0
        public static bool StringToValueOfType(this string text, Type t, ref object result, bool errorMessages = false)
        {
            if (t == typeof(string))
            {
                result = text;
                return(true);
            }

            if (t == typeof(bool))
            {
                bool v;
                if (bool.TryParse(text, out v))
                {
                    result = v;
                    return(true);
                }
                else
                {
                    if (errorMessages)
                    {
                        Debug.Log("Error:  Cannot convert argument \"" + text + "\" to bool\n");
                    }
                    return(false);
                }
            }

            if (t == typeof(int))
            {
                int v;
                if (Int32.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out v))
                {
                    result = v;
                    return(true);
                }
                else
                {
                    if (errorMessages)
                    {
                        Debug.Log("Error:  Cannot convert argument \"" + text + "\" to integer\n");
                    }
                    return(false);
                }
            }

            if (t == typeof(uint))
            {
                uint v;
                if (UInt32.TryParse(text, out v))
                {
                    result = v;
                    return(true);
                }
                else
                {
                    if (errorMessages)
                    {
                        Debug.Log("Error:  Cannot convert argument \"" + text + "\" to unsigned integer\n");
                    }
                    return(false);
                }
            }

            if (t == typeof(long))
            {
                long v;
                if (long.TryParse(text, out v))
                {
                    result = v;
                    return(true);
                }
                else
                {
                    if (errorMessages)
                    {
                        Debug.Log("Error:  Cannot convert argument \"" + text + "\" to long integer\n");
                    }
                    return(false);
                }
            }

            if (t == typeof(float))
            {
                float v;
                if (float.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out v))
                {
                    result = v;
                    return(true);
                }
                else
                {
                    if (errorMessages)
                    {
                        Debug.Log("Error:  Cannot convert argument \"" + text + "\" to float\n");
                    }
                    return(false);
                }
            }

            if (t == typeof(Color))
            {
                var colorValue = Color.magenta;
                if (ColorUtility.TryParse(text, ref colorValue))
                {
                    result = colorValue;
                    return(true);
                }
                else
                {
                    if (errorMessages)
                    {
                        Debug.Log("Error:  Cannot convert argument \"" + text + "\" to color\n");
                    }
                    return(false);
                }
            }

            if (t == typeof(Vector3))
            {
                var vector3Value = new Vector3();
                if (VectorUtility.TryParseVector(text, ref vector3Value))
                {
                    result = vector3Value;
                    return(true);
                }
                else
                {
                    if (errorMessages)
                    {
                        Debug.Log("Error:  Cannot convert argument \"" + text + "\" to vector3\n");
                    }
                    return(false);
                }
            }

            if (t == typeof(Quaternion))
            {
                var quaternionValue = new Quaternion();
                if (QuaternionUtility.TryParseQuaternion(text, ref quaternionValue))
                {
                    result = quaternionValue;
                    return(true);
                }
                else
                {
                    if (errorMessages)
                    {
                        Debug.Log("Error:  Cannot convert argument \"" + text + "\" to quaternion\n");
                    }
                    return(false);
                }
            }

            if (t.IsEnum)
            {
                // No TryParse until .NET 4...so we have to handle exceptions
                object value = null;
                var    valid = true;
                int    temp;
                if (Int32.TryParse(text, out temp))    // Check to see if it's just a number...then fail it.  (Enum.Parse allows it!)
                {
                    valid = false;
                }
                else
                {
                    try
                    {
                        value = Enum.Parse(t, text, true);
                    }
                    catch (Exception)
                    {
                        valid = false;
                    }
                }

                if (valid)
                {
                    result = value;
                    return(true);
                }
                else
                {
                    if (errorMessages)
                    {
                        Debug.Log("Error:  Cannot convert argument \"" + text + "\" to the required enum.  Valid enum values are:\n");
                        foreach (var val in Enum.GetValues(t))
                        {
                            Debug.Log("   " + val);
                        }
                    }
                    return(false);
                }
            }

            Debug.LogError("Error:  Argument type not yet supported.  Assigning \"\"");

            result = "";
            return(true);
        }