Last active
June 26, 2021 17:07
-
-
Save agrawalsuneet/0620f07d2bb8dfc7be92ba8a4ec99034 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
public class AcceptIncomingTextInUnity : MonoBehaviour | |
{ | |
public Button acceptIncomingTextBtn; | |
void Start() | |
{ | |
acceptIncomingTextBtn.onClick.AddListener(onAcceptImageClicked); | |
} | |
public void onAcceptImageClicked() | |
{ | |
#if UNITY_ANDROID | |
acceptIncomingImage(); | |
#else | |
Debug.Log("No Toast setup for this platform."); | |
#endif | |
} | |
void acceptIncomingImage() | |
{ | |
//create a class reference of unity player activity | |
AndroidJavaClass unityActivity = | |
new AndroidJavaClass ("com.unity3d.player.UnityPlayer"); | |
//get the context of current activity | |
AndroidJavaObject context = unityActivity.GetStatic<AndroidJavaObject> ("currentActivity"); | |
//get the current intent object | |
AndroidJavaObject intent = context.Call<AndroidJavaObject>("getIntent"); | |
//below line is for debug purpose | |
Debug.Log("UnityClientDebugging GetExtra type: " + intent.Call<string>("getType")); | |
//it will print | |
//UnityClientDebugging GetExtra type: text/plain | |
//get the extra text from intent | |
string incomingText = intent.Call<string>("getStringExtra", "android.intent.extra.TEXT"); | |
//set it as button text | |
acceptIncomingTextBtn.GetComponentInChildren<Text>().text = incomingText; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment