コード例 #1
0
        public static System.Object ConstructObjectFromString(System.Type type, string value)
        {
            // If the type is a string then our process is simple
            if (type == typeof(string))
            {
                return(value);
            }
            // if the type is a vector 2, 3 or 4 then handle it
            if (type == typeof(Vector2))
            {
                // convert the string to a vector 2
                Vector2 vectorValue = Vector2.zero;
                if (CommandHelpers.Vector2FromTokens(new string[] { value }, 0, ref vectorValue))
                {
                    return(vectorValue);
                }

                return(null);
            }
            if (type == typeof(Vector3))
            {
                // convert the string to a vector 3
                Vector3 vectorValue = Vector3.zero;
                if (CommandHelpers.Vector3FromTokens(new string[] { value }, 0, ref vectorValue))
                {
                    return(vectorValue);
                }

                return(null);
            }
            if (type == typeof(Vector4))
            {
                // convert the string to a vector 4
                Vector4 vectorValue = Vector4.zero;
                if (CommandHelpers.Vector4FromTokens(new string[] { value }, 0, ref vectorValue))
                {
                    return(vectorValue);
                }

                return(null);
            }
            if (type == typeof(Quaternion))
            {
                // convert the string to a quaternion
                Vector4 vectorValue = Vector4.zero;
                if (CommandHelpers.Vector4FromTokens(new string[] { value }, 0, ref vectorValue))
                {
                    return(new Quaternion(vectorValue.x, vectorValue.y, vectorValue.z, vectorValue.w));
                }

                return(null);
            }
            if (type == typeof(Color))
            {
                // is the value being provided as a web style string?
                if (value.Contains('#'))
                {
                    Color colour = Color.magenta;
                    if (ColorUtility.TryParseHtmlString(value, out colour))
                    {
                        return(colour);
                    }
                }                 // otherwise assume it is a RGBA set
                else
                {
                    // convert the string to a color
                    Vector4 vectorValue = Vector4.zero;
                    if (CommandHelpers.Vector4FromTokens(new string[] { value }, 0, ref vectorValue))
                    {
                        return(new Color(vectorValue.x, vectorValue.y, vectorValue.z, vectorValue.w));
                    }
                }

                return(null);
            }

            // does the type have a parse method that takes a string?
            MethodInfo parseMethod = type.GetMethod("Parse", BindingFlags.Public | BindingFlags.Static, null,
                                                    CallingConventions.Any, new System.Type[] { typeof(string) }, null);

            if (parseMethod != null)
            {
                try
                {
                    return(parseMethod.Invoke(null, new object[] { value }));
                }
                catch (System.Exception ex)
                {
                    Debug.LogError("Failed to parse type: " + ex.Message);
                    return(null);
                }
            }

            // does the type have a constructor that takes a string?
            ConstructorInfo constructor = type.GetConstructor(new System.Type[] { typeof(string) });

            if (constructor != null)
            {
                try
                {
                    return(constructor.Invoke(new object[] { value }));
                }
                catch (System.Exception ex)
                {
                    Debug.LogError("Failed to construct type: " + ex.Message);
                    return(null);
                }
            }

            return(null);
        }