Beispiel #1
0
        public static string Execute(string[] tokens)
        {
            // incorrect number of tokens found
            if (tokens.Length < 2)
            {
                return("[Error] You must provide a valid path to the game object to modify.");
            }

            // attempt to find the object
            GameObject foundObject = CommandHelpers.GetGameObject(tokens[0]);

            // object not found?
            if (foundObject == null)
            {
                return("[Error] Could not find the specified game object. Check the path. Paths must be case sensitive.");
            }

            // check if the coordinates are correct
            Vector3 coordinates = Vector3.zero;

            if (!CommandHelpers.Vector3FromTokens(tokens, 1, ref coordinates))
            {
                return("[Error] The incorrect number of coordinates were given. Coordinates should be x,y,z");
            }

            // update the scale
            foundObject.transform.localScale = coordinates;

            return(foundObject.name + " is now scaled to " + foundObject.transform.localScale + " (local)");
        }
Beispiel #2
0
        public static string Execute(string[] tokens)
        {
            // incorrect number of tokens found
            if (tokens.Length < 3)
            {
                return("[Error] You must provide a valid path to the game object to modify.");
            }

            // attempt to find the object
            GameObject foundObject = CommandHelpers.GetGameObject(tokens[1]);

            // object not found?
            if (foundObject == null)
            {
                return("[Error] Could not find the specified game object. Check the path. Paths must be case sensitive.");
            }

            // is the location being set as local or world?
            bool isLocal = false;

            if (tokens[0].ToLower() == "local")
            {
                isLocal = true;
            }
            else if (tokens[0].ToLower() == "world")
            {
                isLocal = false;
            }
            else
            {
                return("[Error] No indication was provided for if the coordinates are in local or world space.");
            }

            // check if the coordinates are correct
            Vector3 coordinates = Vector3.zero;

            if (!CommandHelpers.Vector3FromTokens(tokens, 2, ref coordinates))
            {
                return("[Error] The incorrect number of coordinates were given. Coordinates should be x,y,z");
            }

            // update the location
            if (isLocal)
            {
                foundObject.transform.localEulerAngles = coordinates;
            }
            else
            {
                foundObject.transform.eulerAngles = coordinates;
            }

            return(foundObject.name + " is now rotated to " + foundObject.transform.eulerAngles + " (world) " + foundObject.transform.localEulerAngles + " (local)");
        }
Beispiel #3
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);
        }