Skip to content

Instantly share code, notes, and snippets.

@taesiri
Created November 12, 2013 14:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taesiri/7431660 to your computer and use it in GitHub Desktop.
Save taesiri/7431660 to your computer and use it in GitHub Desktop.
Sample Unity3D Plugin for Sending SMS in Android. (JAVA plugin)
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.BrainDump.PluginTester"
android:versionCode="2"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.SMS_RECEIVED" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application android:name="com.BrainDump.PluginTester.Brain" android:icon="@drawable/app_icon" android:label="@string/app_name">
<activity android:name=".BrainDumpActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.BrainDump.PluginTester;
import android.app.Application;
public class Brain extends Application {
}
package com.BrainDump.PluginTester;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;
import com.unity3d.player.UnityPlayerActivity;
public class BrainDumpActivity extends UnityPlayerActivity{
@Override
protected void onCreate(Bundle icicle){
super.onCreate(icicle);
}
@Override
protected void onResume(){
super.onResume();
}
@Override
protected void onStop(){
super.onStop();
}
@Override
protected void onPause(){
super.onPause();
}
public void showMessage(final String message) {
this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(BrainDumpActivity.this, message, Toast.LENGTH_SHORT).show();
}
});
}
public void sendSMSMessage(final String phoneNumber, final String messageBody){
this.runOnUiThread(new Runnable() {
public void run(){
try
{
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, messageBody, null, null);
showMessage("Message Sent! Destination :" + phoneNumber);
}
catch(Exception exp){
showMessage("Error has been Occurred!\n" + exp.getMessage());
showMessage("Cannot send SMS to " + phoneNumber);
Log.d("Unity", exp.getStackTrace().toString());
}
}
});
}
public void tip(){
Log.d("Unity", "Tip Begin Call!");
showMessage("my text");
Log.d("Unity", "Tip End Call!");
}
}
using UnityEngine;
namespace Assets.Scripts.BrainDump
{
public class PlugInCaller : MonoBehaviour
{
private AndroidJavaClass _activityClass;
private AndroidJavaObject _activityObject;
private string _messageBody = "Sent from Unity with LOVE";
private string _phoneNumber = "000000000000";
private void OnGUI()
{
var mobileStyle = new GUIStyle {fontSize = 20, normal = {textColor = Color.white}};
if (GUILayout.Button("Simple Toast", GUILayout.Width(500), GUILayout.Height(50)))
{
_activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
_activityObject = _activityClass.GetStatic<AndroidJavaObject>("currentActivity");
_activityObject.Call("showMessage", "A direct Message from C# with love!");
}
GUILayout.BeginHorizontal();
GUILayout.Label("Phone Number: ", mobileStyle);
_phoneNumber = GUILayout.TextField(_phoneNumber, mobileStyle, GUILayout.Height(50));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Message: ", mobileStyle);
_messageBody = GUILayout.TextArea(_messageBody, mobileStyle, GUILayout.Height(150));
GUILayout.EndHorizontal();
if (GUILayout.Button("Send SMS", GUILayout.Width(500), GUILayout.Height(50)))
{
_activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
_activityObject = _activityClass.GetStatic<AndroidJavaObject>("currentActivity");
_activityObject.Call("sendSMSMessage", _phoneNumber, _messageBody);
}
}
}
}
@reegankens
Copy link

why my app force close when I use AndroidManifest.xml

@taesiri
Copy link
Author

taesiri commented Apr 11, 2014

Sorry, i just saw your command. you should include appropriate AndroidManifest.xml in your unity project. (creating eclipse project, adding classes ...) or simply change bundle identifier in your unity project

@samnangcattor
Copy link

Sorry taesiri, i already did as you post, but i could not see the result of it.

@pooyanmir
Copy link

سلام آقای تأثیری
از نام شما حدس زدم که احتمالا ایرانی باشید
محمد رضا عزیز! من در حال ساخت یک مسنجر در یونیتی هستم و از این طریق پست شما رو دیدم. مشکلی دارم که خواستم بدونم امکانش هست که شما به من کمک کنید؟
مشکل من آن است که ارتفاع کیبورد در یونیتی در اندروید قابل دریافت نیست و همیشه عدد صفر را نشان میدهد
فیلد اینپوت تکست هم قابل ادیت نیست! خیلی سرچ کردم و یک ماه است که درگیر این مسئله هستم.
بهترین پاسخی که دیدم لینک زیر است ::
http://iswwwup.com/t/7639b145fe65/height-of-virtual-android-keyboard-for-untiy3d.html
ولی نتوانستم با آن هم موفق شوم
خوشحال میشوم صدای شما را بشنوم
شماره من:
09112119203

@Vagonn
Copy link

Vagonn commented Apr 17, 2016

Hello. It's not worked for me. Unity 5.3.4

With manifest file app force closed. I delete manifest file and build. But when I press SendSMS, nothing happens

@rmdwirizki
Copy link

rmdwirizki commented Aug 27, 2016

I create another gist based on this code but without creating a java plugin, just in case somebody need it https://gist.github.com/rmdwirizki/87f9e68c7ef6ef809a777eb25f12c3b2

@BahmanAries
Copy link

@rmdwirizki: Your code works like a charm and it's much easier for me to use. Thank you.

@SajadAhmadiNiat
Copy link

ببخشید بنده از همین راه رفتم . ولی با ارور رفرنس مواجه میشدم . در خطهایی که مواردی مثل
_activityObject.Call("sendSMSMessage", _phoneNumber, _messageBody);
وجود داشت
به این ها گیر میداد . چیکار میشه کرد اقای تاثیری؟

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment