1. Home
  2. Docs
  3. UniREST Solution 3.5
  4. UniREST Client
  5. UniREST Client Tools
  6. UnityToText, TextToUnity

UnityToText, TextToUnity

The UniREST Client class contains two methods to easily convert common Unity data-type objects into strings suitable to be sent to your Database. The following Unity data-types are fully supported:

  • Vector2, Vector3, Vector4, Quaternion
  • Transform (position, localPosition, localScale, rotation, localRotation, eulerAngles, localEulerAngles)
  • Color, Color32
  • Rect (x, y, width, height, center, max, min, position, size, xMax, xMin, yMax, yMin)

UnityToText() method

This method converts a Unity object into a single string, suitable to be saved in your Database table (into a TEXT type column).

UniRESTClient.UnityToText(data);
ParameterData-typeDescription
dataobject (of Unity data-type)The Unity object to convert into a string. It must be one of the supported data-type.
Returned valueDescription
stringThis method returns a single string containing all the parameters of the Unity object, separated by a pipe char. If the given object is not valid, an empty string is returned.

TextToUnity() method

This method converts a string (previously created by the UnityToText() method) into the given Unity data-type.

UniRESTClient.TextToUnity<TYPE>(data);
ParameterData-typeDescription
TYPEUnity data-typeOne of the supported Unity data-type. The provided string will be converted to this type.
datastringThe string to convert into a Unity object. It must be a valid string created by the UnityToText() method.
Returned valueDescription
stringThis method returns a Unity object of the given type. If the given string is not valid, a null object is returned.

Example

In the following C# Script example, there are two functions. The SavePosition() function gets the Unity gameobject position, converts it into a string and sends it to the Database for saving. The LoadPosition() function reads the position from the Database, converts it into a Unity Vector3 object and sets the GameObject new transform position.

public class Player : MonoBehaviour {
    
    void SavePosition() {

        var myPos = UniRESTClient.UnityToText(transform.position);
        var w = UniRESTClient.Write(API.player_position, new DB.Player { position = myPos });

    }

    void LoadPosition() {

        var r = UniRESTClient.ReadOne<DB.Player>(API.player_position, new DB.Player { id = UniRESTClient.UserID });
        var myPos = UniRESTClient.TextToUnity<Vector3>(r.position);
        transform.position = myPos;

    }

}