Add android view in Unity Activity
Hi, I want to add TextView in the activity which is sent by Unity . I made an android plugin to communicate with unity.
public void getActivity(final Activity unityActivity) {
instance = this;
activity = unityActivity;
try {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
LayoutInflater inflater = activity.getLayoutInflater();
Resources resources = activity.getResources();
String pkgName = activity.getPackageName();
View view = inflater.inflate(R.layout.android_layout,null);
ViewGroup.LayoutParams param = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
// add the view hierarchy in to the unity activity
activity.addContentView(view, param);
TextView textView = (TextView) activity.findViewById(resources.getIdentifier(String.valueOf(R.id.button_id), "id", pkgName));
textView.setText("My Text");
}
});
}
catch (ActivityNotFoundException e)
{
}
}
And calling in Unity like:
AndroidJavaObject mClass;
AndroidJavaObject Innstance;
AndroidJavaObject activity;
private void InitInstance()
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
print(activity.GetType().ToString());
mClass = new AndroidJavaObject("com.udacity.horatio.widgetexample.TutorialPointWidget.UnityPlayerPlugin");
Innstance = mClass.GetStatic<AndroidJavaObject>("instance");
}
public void TestWithNativeUnityPlayer()
{
Innstance.Call("getActivity", activity);
}
Calling TestWithNativeUnityPlayer() on button press
I have the following error
zaist.jpg
(106.1 kB)
Comment
Your answer
Follow this Question
Related Questions
Unity UI elements flaky texture glitch on Android Builds 0 Answers
How to make a trigger area change a canvas' text? 1 Answer
Damage Text pop-up when enemy is hit 1 Answer
UI Canvas Anchor points are stuck in the bottom left corner and disabled when in overlay mode. 1 Answer
Get single component from canvas, instead of all components 1 Answer