Neel Gajjar - Unity Developer
By: Neel Gajjar - Unity Developer

Send SMS From Your Game - Unity3D

In the game, you can use "prefer the game" feature or invite the friend(s) who is not using the game. Then you can send friends the SMS with game information.

For achieving this, you have to attach the SendSMSClass class to your active game object & use 

- sendSingleSMS() - for sending SMS to single recipient &

- sendMultipleSMS() - for sending SMS to multiple recipients.

This functions will open the default native SMS app with pre-filled recipient mobile number(s) & body massage, which we passed from the method.


Code:

- using System.Collections;

- using System.Collections.Generic;

- using UnityEngine;


public class SendSMSClass : MonoBehaviour {


   //recipient's mobile number

    string sender_mobile_numner = "8866509166";

    //recipient's mobile number array

    string[] sender_mobile_number_array = new string[]{"8866509166","9898071856"};

    string sms_body = "Your sms body";


   //for sending SMS to single recipient

    public void sendSingleSMS(){    

       //Open the native SMS default app

        Application.OpenURL(string.Format("sms:"+sender_mobile_numner+"?body="+sms_body));

    }


   //for sending SMS to multiple recipients

    public void sendMultipleSMS(){

        sender_mobile_numner = "";

        foreach(string mobile_number_str in sender_mobile_number_array)

            sender_mobile_numner += mobile_number_str + ";";

            

       //Open the native SMS default app

        Application.OpenURL(string.Format("sms:"+sender_mobile_numner+"?body="+sms_body));

    }


   void OnGUI() {

        if (GUI.Button (new Rect (10, 10, 250, 50), "Send SMS to single recipient"))

            sendSingleSMS ();

        else if (GUI.Button (new Rect (10, 70, 250, 50), "Send SMS to multiple recipient"))

            sendMultipleSMS ();

    }

}


This code will be useful for Android & iOS game.

Also, see the attachments for APK & Project sample.


Leave us a comment