Integrate Unity3d view into Android activity - android

I'm currently working on a small AR app for Android and am facing the problem of integrating Unity3d into an activity. The requirements indicate that I need to be able to present some Android UI - e.g. menus and an action bar - and a camera view that will display a model created in Unity3d when the target is detected.
I found a link that helped me a lot: Unity3d forums. One of the users there asked the same question I have now but never got any proper answer -that's why I'm posting here.
Problem:
I got a small Unity3d project that is essentially a white cube and am trying to display it in one of my Android activities. The model looks fine when activity doesn't have setContentView() in its onCreate() method but then I can't specify my layout in an XML file.
When I do add the setContentView() method, I can see the cube but it's very small and there doesn't seem to be any way of actually making it change its size.
The XML file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/unityView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
1st version of the activity implementation:
public class HomeActivity extends UnityPlayerActivity {
UnityPlayer unityPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
And the resulting screenshot:
2nd version of the activity implementation:
public class HomeActivity extends UnityPlayerActivity {
UnityPlayer unityPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
unityPlayer = new UnityPlayer(this);
int glesMode = unityPlayer.getSettings().getInt("gles_mode", 1);
unityPlayer.init(glesMode, false);
FrameLayout layout = (FrameLayout) findViewById(R.id.unityView);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layout.addView(unityPlayer.getView(), 0, lp);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
unityPlayer.windowFocusChanged(hasFocus);
}
}
And the resulting screenshot:
Could anyone explain to me why that is and how to fix it?

While I still don't know why it works like that, I've found a way of fixing it.
Instead of simply using setContentView() in onCreate(), extend onResume() and in that method recursively look through all the available views to find the parent view of the UnityPlayer object. Once that's found, layouts and other views can be inflated and added to that parent view.
Here's the link with a code example - I've used this to make my app work: https://developer.vuforia.com/resources/dev-guide/extending-unity-android-activity-and-adding-custom-views-eclipse
Edit: Here's a code snippet showing my solution.
#Override
public void onResume() {
super.onResume();
if (unityPlayer == null) {
View rootView = findViewById(android.R.id.content);
unityPlayer = findUnityPlayerView(rootView);
if (unityPlayer != null) {
ViewGroup unityPlayerParentView = (ViewGroup)(unityPlayer.getParent());
View mainHomeView = getLayoutInflater().inflate(R.layout.activity_main, null);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
unityPlayerParentView.addView(mainHomeView, layoutParams);
}
}
}
and
private UnityPlayer findUnityPlayerView(View view) {
if (view instanceof UnityPlayer) {
return (UnityPlayer) view;
}
if (view instanceof ViewGroup) {
ViewGroup childrenViews = (ViewGroup) view;
for (int i = 0; i < childrenViews.getChildCount(); i++) {
UnityPlayer foundView = findUnityPlayerView(childrenViews.getChildAt(i));
if (foundView != null) {
return foundView;
}
}
}
return null;
}

Related

Using personal SurfaceView on LinearLayout

I'm currently developing an Android App, using a personal SurfaceView and double buffering. However I'm facing a little problem with my code.
In one hand I have an xml view, based on LinearLayout hierarchy. When I instantiate my activity, I set my contentView on this xml. The problem is then that my double buffering don't works anymore. Thread is running but nothing is displayed.
In the other hand, I set my contentView with a new personal SurfaveView element and display works fine. But of course, I cannot access anymore to the other elements of my LinearLayout.
Actually, I would like to set my contentView on my xml view AND keep my display working.
I hope I was clear enough, thank you for your answers!
Here is my activity:
public class MainController extends Activity {
#Override
protected void onCreate(final Bundle p_savedInstanceState) {
super.onCreate(p_savedInstanceState);
setContentView(R.layout.main_controller_activity);
this.drawableView = (MCustomDrawableView) findViewById(R.id.drawingBox);
...
}
...
}
My surface view:
public class MCustomDrawableView extends SurfaceView {
public MCustomDrawableView(Context p_context, AttributeSet p_attributes) {
super(p_context, p_attributes);
this.getHolder().addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(final SurfaceHolder p_holder) {
try {
// Instantiating a new thread
thread = new MBufferingThread(getInstance());
thread.start();
} catch (Exception exception) {
exception.printStackTrace();
}
}
#Override
public void surfaceChanged(final SurfaceHolder p_holder, int p_format, int p_width, int p_height) {...}
#Override
public void surfaceDestroyed(final SurfaceHolder p_holder) {...}
});
// Setup drawing options
setupDrawing();
}
...
}
And my xml view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#color/app_background"
tools:context=".MainController">
<com.iskn.calligraphy.models.draw.MCustomDrawableView
android:id="#+id/drawingBox"
style="#style/DrawingBox" />
<SeekBar
android:id="#+id/brushSize"
style="#style/SizeSeekBar" />
<SeekBar
android:id="#+id/brushOpacity"
style="#style/OpacitySeekBar" />
</LinearLayout>
EDIT:
After further researches and analyses, it appears clearly that:
setContentView(R.layout.main_controller_activity): in this case I get all the elements from my activity, but the MCustomDrawableView display nothing.
setContentView(new MCustomDrawableView(getApplicationContext())): on that case, MCustomDrawableView is working well (it displays what I want), but I don't have the others View from my main_controller_activity
In both cases:
my thread is running and works well.
my drawing function is called as well, with the holder.lockCanvas() and holder.unlockCanvasAndPost(bufferCanvas) methods.
Well, I found a functionnal solution :
I think the problem was coming from the MCustomDrawableView initialization . I created a new instance of that class from the onCreate method , and not from the xml file . Then, I set it to the contentView:
protected void onCreate(Bundle p_savedInstanceState) {
super.onCreate(p_savedInstanceState);
setContentView(R.layout.main_controller_activity);
// Instantiate drawable object & set style properties
this.drawableView = new MCustomDrawableView(getApplicationContext());
FrameLayout.LayoutParams style = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
// Add the drawable view to the main_activity
addContentView(this.drawableView, style);
}
But this way raises a new problem. The added view is on the top, which means that all the others View are hided. I solved this with bringToBack(this.drawableView) below method:
private void bringToBack(View p_view) {
// Get parent from the current view
ViewGroup viewGroup = ((ViewGroup) p_view.getParent());
int childrenCount = viewGroup.indexOfChild(p_view);
for(int cpt = 0; cpt < childrenCount; cpt++) {
// Move the child to the top
viewGroup.bringChildToFront(viewGroup.getChildAt(cpt));
}
}
It brings back the provided view to the background position. I also had to set the LinearLayout's alpha to 0.
I'm still open to other solutions!

GLSurfaceView transparent background

I'm using min3D library in my project to visualize a 3D model. This library is based in openGL.
For this, I defined a renderer which manages the GLSurfaceView. At this moment, I see the 3D model in my app, but the background of the surface view is black, and my goal is to make it transparent, this way I would only see the 3D model without the black background.
The min3D library examples, as other SO questions and info I've readed about this, tell that the way to achieve this is by doing this:
_glSurfaceView.setEGLConfigChooser(8,8,8,8, 16, 0);
_glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
and this:
scene.backgroundColor().setAll(0x00000000);
But I don't get to make the background transparent.
First of all, this is my layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/valuesContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
...
</LinearLayout>
<FrameLayout
android:id="#+id/model3d_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/valuesContainer"/>
</RelativeLayout>
The 3D model fragment is setted inside the FrameLayout this way:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_row3d, container, false);
Fragment modelFragment = new Obj3DView();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.model3d_container, modelFragment).commit();
return view;
}
And finally, this this the fragment which shows the 3D model and where I'm trying to modify things to make the surface trasnparent:
public class Obj3DView extends RendererFragment {
private Object3dContainer rowObject3D;
#Override
protected void glSurfaceViewConfig() {
// !important
_glSurfaceView.setEGLConfigChooser(8,8,8,8, 16, 0);
_glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
}
/** Called when the activity is first created. */
#Override
public void initScene() {
scene.backgroundColor().setAll(0x00000000);
scene.lights().add(new Light());
scene.lights().add(new Light());
Light myLight = new Light();
myLight.position.setZ(150);
scene.lights().add(myLight);
IParser myParser = Parser.createParser(Parser.Type.OBJ, getResources(), "com.masermic.rowingsoft:raw/row_obj",true);
myParser.parse();
rowObject3D = myParser.getParsedObject();
rowObject3D.position().x = rowObject3D.position().y = rowObject3D.position().z = 0;
rowObject3D.scale().x = rowObject3D.scale().y = rowObject3D.scale().z = 0.28f;
// Depending on the model you will need to change the scale faceObject3D.scale().x = faceObject3D.scale().y = faceObject3D.scale().z = 0.009f;
scene.addChild(rowObject3D);
}
#Override
public void updateScene() {
rowObject3D.rotation().x += 0.5;
rowObject3D.rotation().z += 1;
rowObject3D.rotation().y += 0.1;
}
}
Note: This fragment extends from RendererFragment(extending fragment) which belongs to the min3D library, and this is the most relevant code of this class:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_initSceneHander = new Handler();
_updateSceneHander = new Handler();
//
// These 4 lines are important.
//
Shared.context(getActivity());
scene = new Scene(this);
Renderer r = new Renderer(scene);
Shared.renderer(r);
_glSurfaceView = new GLSurfaceView(getActivity());
glSurfaceViewConfig();
_glSurfaceView.setRenderer(r);
_glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
}
I answer my own question. After searching a lot, I finally got a solution that works. Just have to include this call inside glSurfaceViewConfig():
_glSurfaceView.setZOrderOnTop(true);

Android View width and height have not changed after rotation

I have an activity whose layout I need to change after a rotation and part of the layout is a graph that is drawn using the width and height of the view that it will be placed into. The first time my code runs, the graph is drawn correctly, however after the rotation the width and height of the container view are not correct, in fact they appear to be the view as if it was not rotated.
Here is what I have so far,
In my manifest for the activity I am working:
android:configChanges="keyboardHidden|orientation|screenSize"
In my activity I have these following methods:
onCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
patient_id = extras.getInt("patient_id");
patient_name = extras.getString("patient_name");
historyDurationType = 12;
constructLayout();
}
constructLayout
public void constructLayout(){
if(landScape){
setContentView(R.layout.activity_bg_history_static_land);
//Set buttons
btnTwelve = (Button)findViewById(R.id.btnTwelveHoursLand);
btnTwentyFour = (Button)findViewById(R.id.btnTwentyFourHoursLand);
btnSeven= (Button)findViewById(R.id.btnSevenDaysLand);
btnTwelve.setOnClickListener(this);
btnTwentyFour.setOnClickListener(this);
btnSeven.setOnClickListener(this);
btnTwelve.setBackgroundColor(getResources().getColor(R.color.light_blue_regular));
btnTwentyFour.setBackgroundResource(android.R.drawable.btn_default);
btnSeven.setBackgroundResource(android.R.drawable.btn_default);
}else{
setContentView(R.layout.activity_bg_history_static);
//Set buttons
btnTwelve = (Button)findViewById(R.id.btnTwelveHours);
btnTwentyFour = (Button)findViewById(R.id.btnTwentyFourHours);
btnSeven= (Button)findViewById(R.id.btnSevenDays);
btnTwelve.setOnClickListener(this);
btnTwentyFour.setOnClickListener(this);
btnSeven.setOnClickListener(this);
btnTwelve.setBackgroundColor(getResources().getColor(R.color.light_blue_regular));
btnTwentyFour.setBackgroundResource(android.R.drawable.btn_default);
btnSeven.setBackgroundResource(android.R.drawable.btn_default);
btnComment = (Button)findViewById(R.id.btnCommentGraph);
btnComment.setOnClickListener(this);
populateOtherContent(officialReadings12);
TextView tvStats = (TextView)findViewById(R.id.txtStatistics);
Typeface chunkFiveFont = Typeface.createFromAsset(getAssets(), "fonts/chunkfivettfversion.ttf");
tvStats.setTypeface(chunkFiveFont);
TextView tvReading = (TextView)findViewById(R.id.txtReadingTitle);
tvReading.setTypeface(chunkFiveFont);
comment = null;
}
if(needData){
getLatestReadings();
}
populateGraph();
}
populateGraph
public void populateGraph(){
if(landScape){
graph_container = (LinearLayout)findViewById(R.id.graph_land_content_layout);
}else{
graph_container = (LinearLayout)findViewById(R.id.graph_content_layout);
}
//Create graphlayout
mainGraph_Layout = new RelativeLayout(this);
RelativeLayout.LayoutParams glParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
mainGraph_Layout.setId(909);
mainGraph_Layout.setLayoutParams(glParams);
graph_container.addView(mainGraph_Layout);
graph_container.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if(needsGraph){
layoutGraph();
needsGraph = false;
}
}
});
}
layoutGraph
public void layoutGraph(){
viewWidth = mainGraph_Layout.getWidth();
viewHeight = mainGraph_Layout.getHeight();
//MORE STUFF IS HERE BUT NOT IMPORTANT
}
onConfigurationChanged
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
ActionBar actionBar = getActionBar();
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
//Config is landscape here
actionBar.hide();
needData = false;
landScape = true;
needsGraph = true;
constructLayout();
}else{
//Config is portrait here
actionBar.show();
needData = false;
landScape = false;
needsGraph = true;
constructLayout();
}
}
After rotation, it is at the layoutGraph() viewWidth and viewHeight objects where I have the problem. I had assumed by that point (having used the global layout listener) that the values would be correct. My understanding was that the listener would only have been triggered once "graph_container" was completed (and landscape or portrait) and so when calling layoutGraph() the width and height of "mainGraph_layout" (a child a graph_container, widths and heights set to MATCH_PARENT) would be good to go. It appears that the width and height I am getting are as if the phone is still portrait, and worth noting it appears that the removal of the action bar has also been taken into account.
Sorry for the long question but I thought it best to show all the code. If anything else needs to be shown then please let me know.
Thanks in advance,
Josh
There is a much better way to do this.
Use resource folders
Put your default layout files in res/layout, and the ones for landscape in res/layout-land. In other words, move res/layout/activity_bg_history_static_land.xml to res/layout-land/activity_bg_history_static.xml.
In onCreate, call
setContentView(R.layout.activity_bg_history_static);
The system will pick the file from res/layout-land when you are in landscape orientation, res/layout otherwise.
If you have views that are only present in one layout but not the other e.g. the comment button, wrap the code inside a null check like this:
btnComment = (Button) findViewById(R.id.btnCommentGraph);
if (btnComment != null) {
btnComment.setOnClickListener(this);
}
For populateGraph(), make sure both res/layout/activity_bg_history_static.xml and res/layout-land/activity_bg_history_static.xml has android:id="#+id/R.id.graph_content. Then you can do findViewById(R.id.graph_content) and get the LinearLayout you need.
Save data across rotation
In your activity, override onSaveInstanceState(), and save the data from getLatestReadings() into the bundle.
Then, in onCreate:
if (savedInstanceState == null) {
getLatestReadings();
} else {
// Restore latest readings from savedInstanceState
}
With that, you can let the system handle the rotation i.e. remove this from your manifest:
android:configChanges="keyboardHidden|orientation|screenSize"
Since the system is handling the rotation, you don't need to have a view tree observer any more. And you don't have to override onConfigurationChanged.

Android get layout height and width in a fragment

I am working on a fragment and I want to get the dimension of a layout contained in the xml fragment layout.
When I try the code
RelativeLayout myLayout = view.findViewById(R.id.myLayout);
myLayout.getHeight();
it returns 0.
I need these dimensions to put inside myLayout other objects.
I try to use:
myLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener(){
#Override
public void onGlobalLayout() {
mHeight = myLayout.getHeight();
mWidth= myLayout.getWidth();
System.out.println("width: "+mWidth+" height: "+mHeight);
}
});
but this code is invoke a lot of time and I don't know exactly when it is execute.
I need these dimensions into public void onActivityCreated () method. Is it possible?
There's a cleaner solution to this, just use the View.post() method on your fragment's root view, and you can call getMeasuredHeight()/getMeasuredWidth() and get the actual values.
E.g.
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.myfragment, container, false);
root.post(new Runnable() {
#Override
public void run() {
// for instance
int height = root.getMeasuredHeight();
}
});
return root;
}
Very neat and tidy and no messy mucking about with the ViewTreeObserver.
I've only tested this with android-23 devices, but the API has had this method since level 1.
Anyway, WFM.
The addOnGlobalLayoutListener will be called whenever small change of the view happened. So you need to remove this listener from the view.
Simple usage:
public static void removeOnGlobalLayoutListener(View v, ViewTreeObserver.OnGlobalLayoutListener listener){
if (Build.VERSION.SDK_INT < 16) {
v.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
} else {
v.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
}
}
I would suggest you to check :
if (myLayout.getHeight()>0 && myLayout.getWidth()>0) {
// Do some code...
removeOnGlobalLayoutListener(..)
}
In onActivityCreated, the view hasn't been measured yet.
So no, you can't get the measurements from there. onGlobalLayout is a good place to do it. Layouts happen directly after measurement.
You can remove your listener afterwards, if you only want to do it once. Note that layouts can change size though (soft keyboards appearing, for example)

Adding View to Relative Layout behind an existing view causes screen flicker

I'm trying to insert a View behind another view that is taking up the full screen and then later removing the view in the front to reveal the only remaining view. Functionally, everything is working as expected but the problem is that when I call View.addView() to add the second view, specifying to add it at index 0 so it is behind the first view, the screen flickers. It's almost as if the view is actually getting added in front of the first view for a fraction of a second and then it is hidden again as it is moved behind it.
Here's what I'm doing:
When the Activity is created I add an ImageView to a RelativeLayout and make the RelativeLayout instance the Activity's content view:
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
m_layout = new RelativeLayout(this);
m_layout.setBackgroundColor(Color.BLACK);
m_splashImage = new ImageView(this);
m_splashImage.setImageResource(R.drawable.splash);
m_splashImage.setScaleType(ScaleType.FIT_XY);
m_layout.addView(m_splashImage,
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
setContentView(m_layout);
}
When the Activity is started, I created and add the GLSurfaceView to the RelativeLayout at index 0, so it is behind the ImageView:
protected void onStart() {
super.onStart();
m_layout.addView(new MyGLSurfaceView(), 0,
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
}
Later, after all of the loading is done and the GLSurfaceView is ready to continuously render,
the splash ImageView is removed and cleaned up.
public void hideSplashScreen() {
if (m_splashImage != null) {
m_layout.removeView(m_splashImage);
m_splashImage = null;
}
}
Is there a better way to do this that doesn't require creating the GLSurfaceView before the onStart() is called?
Have you tried using view.setVisibility(View.GONE) on of the view that you adding behind? Of course before you are adding it.

Categories

Resources