Log in or register to post comments

QCARUnityPlayer in Subview

March 28, 2012 - 2:37am #1

Hi..

I'm trying to display the QCARUnityPlayer in one part of the display.. basically it should do the same es suggested in here with the normal unityplayer (works fine)

http://forum.unity3d.com/threads/98315-Using-Unity-Android-In-a-Sub-View

any ideas?

i would like to have the unity qcar at the top and a surfaceview at the bottom.

i tried smthg like:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

 surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
... etc.
}

the qcar player is always in front and i can't see the surfaceview

thx a lot in advande..

QCARUnityPlayer in Subview

November 24, 2017 - 2:29pm #21

do you know the steps please i need a help 

QCARUnityPlayer in Subview

August 27, 2015 - 2:07pm #20

Could you kindly please help me with integrating Unity and Vuforia into Android Studio.. Basically all i want to achieve right now is to have a menu system designed in Android Studio and from that menu options the user goes into the AR scene . I am totally new to this. Any help would be highly appreciated as most of the content is done on eclipse and not on android studio.

QCARUnityPlayer in Subview

December 23, 2014 - 7:36pm #19

Hi,

Sorry, right now I'm a little busy with other things.  I don't have the time right now to strip down a sample.  I will consider it later, but likely that's still a few months from now.

putting QCAR in a fragment is the same as putting Unity into a fragment, in fact, you'll be doing both, both the unity stuff and then the QCAR stuff.

You should be able to find everything you need by searching these forums and also searching the unity forums (unity inside android fragment), 

and also by looking at the code that is generated by Unity when it saves the project as a google project (it automatically creates an activity project with Unity being set up and called for various events/intents.)  and when you save your Vuforia Unity project, you get the basics of Vuforia in an activity project as well.  the trick is then to do the same thing but with a fragment setup, so the code that was in the activity is now instead doing the same thing in the fragment.

the bigger issue is the finicky-ness of Vuforia, or appearance of finicky-ness. After much pain dealing with Vuforia,  I now recommend that one should update to the latest vuforia, and unity vuforia, and follow their directions to the letter, but then DO NOT MIX the unity vuforia samples.  I found that was the real problem I was having getting my code to work. Once I just stripped everything down and use one and only one sample as a base, the image sample, everything worked as claimed by the Vuforia support people.  (mixing the unity samples, i.e. importing more than one into your project as an asset, caused some files to be overwritten, and undefined behaviors).

 

best,

CG

QCARUnityPlayer in Subview

December 5, 2014 - 4:21am #18

Hi,

Can you put your complete working code in github?

Currently I have integrated JPCT-ae with Vuforia but since my model size is large it usually takes a lot of time for loading which is annoying. I left using Unity 3D since I was not able to use Unity 3D in a sub-view. AR is only a part of my App, which is only used to show a 3D model with simple transformation capabilities.

Thanks in advance

With Regards,
MSL

 

QCARUnityPlayer in Subview

August 17, 2014 - 3:41pm #17

Well, you know what? Doesn't really matter at this point if I can't get multiple side-by-side fragments, since I can do a complex fragment layout instead and see if that will work just as well.  And a complex layout so far is working, at least everything looks like it's running (haven't hooked up any buttons yet, I mean, so it does nothing).

Still can't figure out how to force it into landscape mode (but maybe that's the emulator! I hope so!)

And can't figure out why the camera (webcam on my desktop--> emulator) is upside down when it outputs via the emulator!

(Haven't tried it on my real device yet, so who knows, maybe it's just an emulator weirdness!)

If anyone has any ideas, lemme know...

Thanks!

 

p.s. If I figure out the multiple fragments at one time issue, I will post a solution then.

QCARUnityPlayer in Subview

August 17, 2014 - 2:25pm #16

Well, worked as you had it.  There is still something I'm not getting, that is obvious, about how the qcar/unity setup works.  If you've seen this before, any advice would be appreciated.

The instant I did this, the whole thing stops working now:  (why did I do this, well, I need to have at least two fragments running and sharing the screen..one with the AR unity/vuforia feed and the other for other user information, inputs/buttons, etc.)

i.e. changed my main layout from simple a framelayout (or an empty linearlayout, which also works) to a linear layout that includes the fragment)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.LittleDogsMedia.TryAgain.MainActivity"
    tools:ignore="MergeRootFrame" >
 
 
    <fragment android:name="com.LittleDogsMedia.TryAgain.MainActivity$PlaceholderFragment"
              android:id="@+id/unity_container_fragment"
              android:layout_weight="4"
              android:layout_width="0dp"
              android:layout_height="match_parent" />
 
</LinearLayout>
 
the fragment xml looks like this:
 
    android:id="@+id/unity_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.LittleDogsMedia.TryAgain.MainActivity$PlaceholderFragment" />
 
and during the unity initialization *your code* I still look for id unity_container (the framelayout to be used in the fragment).
I also tried having the unity framelayout look for the fragment id instead of the framelayout in the fragment, but that doesn't work either.  So I think I'm right in looking for the framelayout (versus the fragment id).
 
Before I go any further, does anything hinky jump out at you, or anyone for that matter?

 

QCARUnityPlayer in Subview

August 17, 2014 - 12:16pm #15

That's great glad you got it working and thanks for expanding on the solution.

QCARUnityPlayer in Subview

August 17, 2014 - 11:49am #14

yes, success. Thank you grennis!

For those also trying this, follow the thread instructions but ADD the following information to the process:

Make sure, in the Manifest, that the activity that will be starting the app is NOT sending native events to dalvik, i.e.

<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
      
In this method by grennis, as best I can tell, you are not overriding native activity, just starting up the regular way of doing unity+qcar.
(I had it still set to true for forwarding and while it still came up, I lost the ability to interact...i.e. I couldn't hit the buttons that Vuforia brought up on it's About screen).
 
Note: I was able use the ActionBar (so v7 and v4 compatibility).  I'm still working out the theme issue (got a gray ActionBar) and I still have to work out multiple fragment issues AND possibly trying to get the overlaying of buttons directly on the unity container (which may not be a good thing since it seems to require Native activity rather than shared...using the KB article...but we'll see...).
 
HERE's a short summary of what I did:
-------------------------------------------------------
Step 1: Export the Google Project from Unity (i.e. export your Vuforia Unity project as a Google project).  My version of Unity Free 4.5x lets me export a google project (latest as of this writing).
Step 2: Create a new Eclipse project...Android project...from existing source, and use the Google project as that source. (I'm using  Eclipse Juno, I think...the last Eclipse approved for Android before Google forces the switch to their new IDE)..
Step 3: To  make it really easy, create a second android project in your package where you get Android/Eclipse to do the work for you..i.e. create an Activity with Fragment project.  Then, copy the files you need over to your Unity based project (created in step 2)....file likely include layouts, some values\*.xml files, the menu directory (if using  menu), and right-clicking your Unity project and adding reference libraries (like a7 compatible) and copying over other libs (like a4 etc.)
You can delete this second project later...it was just there to create the basic structure to use in the project Unity created for you.
 
Step 4: Follow grennis' instructions using
1) onViewCreated,
and also remember you will need the followin as well:
2) creating member variables in fragment class to hold the UnityPlayer  and the QCARPlayerSharedActivity,
3) importing 
import com.qualcomm.QCARUnityPlayer.QCARPlayerSharedActivity;
import com.unity3d.player.UnityPlayer; 
4) having  simple main activity layout and fragment activity layout (framelayout)
5) in the manifest, in the activity to start, do not forward native messages...<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
      
 
This worked for me and by that I mean it worked in an emulator, which is even more hopeful.  And is now my starting point for getting the theme to be what I want, seeing what modifications I can make to the activity settings in the manifest without upseting Vuforia/Unity, adding in other fragments and using more sophisticated layouts,..................and most importantly, trying to get the darn thing to use landscape instead of forcing portrait each time.
 
Yes, even setting the main activity to landscape explicitly in the manifest doesn't work...the unity/vuforia part is forcing it back into portrait.  Definitely need to find out how to override that, tablets work better with landscape!  :)
 
Hope this summary helps!
Cynthia

 

QCARUnityPlayer in Subview

August 17, 2014 - 10:10am #13

Sorry for the mistake. You are not being stupid. I said put the code in "onCreateView", but it actually goes in "onViewCreated".  This method passes in the "view" parameter that I am using there.

 

 

QCARUnityPlayer in Subview

August 17, 2014 - 9:54am #12

NOTE (just tried): Given what I'm doing below (using getView() in place of view, and leaving the rootView return as is..i.e. inflate and return) I do get something now in the emulator.  I get my app with Actionbar, and the Vuforia sample (Vuforia Buttons) starts to come up...the splash screen then the About page...  However,  I cannot get any buttons to work. In other words, the About screen stays put, hitting the OK button on it, doesn't dismiss it, so I'm not sure if the camera will come up or not (yet).

Did you see this behavior? Or did you just avoid the Vuforia UI screens completely?

Am I doing the fragment correctly?  (I'm using themes so am using ActionBar and app compatible theme settings...perhaps that is causing the problems?)

-----------original reply-------------

Hi, sorry to be so stupid about this.

OK, I've modified the basic android activity with one fragment example...

....or am in the process again per your instructions (so now my activity and frame layout are simple framelayouts with the ids container and unity_container...

....and added that to my working code (basic google project from unity).

Given that, here's the basic generated fragment...with your code injected between...

note: In your call to find the unity_container  FrameLayout container = (FrameLayout)view.findViewById(R.id.unity_container);

                
the view doesn't resolve...I tried using rootView...but I'm not sure if one should even inflate the rootview or return it...but I can't figure out what 'view' should be in order to find the container in this situation. (can't use PlaceholderFragment.this, or MainActivity.this)  However, getView() should work, but stil not sure what exactly that will do in this case, and I still am not sure what I should return from onCreateView?

Second question, do you still use <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" /> in the mainfest for the main activity?  (i.e. TRUE not false?)  (and use that same long manifest that is generated by the unity/vuforia google project?)

Again, sorry to be so dumb on this...perhaps it's lack of sleep by this point.
Cynthia
    
public PlaceholderFragment() {
        }
 
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            
       
        mUnityPlayer = new UnityPlayer(getActivity());
        final int mode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
       
        this.mQCARShared = new QCARPlayerSharedActivity();
        this.mQCARShared.onCreate(getActivity(), mode, new QCARPlayerSharedActivity.IUnityInitializer() {
           @Override
           public void InitializeUnity() {
               mUnityPlayer.init(mode, false);
               FrameLayout container = (FrameLayout)view.findViewById(R.id.unity_container);
               FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
               container.addView(mUnityPlayer, 0, lp);
       
               mUnityPlayer.windowFocusChanged(true);
               mUnityPlayer.resume();
           }
        });
       
            return rootView;
        }
    }

QCARUnityPlayer in Subview

August 17, 2014 - 6:13am #11

Cynthia, the code I posted goes in onCreateView of your Fragment.

The layout is simple - the unity_container is just a FrameLayout with match_parent for height and width. This is inside the root fragment layout which is also just a FrameLayout with match_parent height and width.

I'm not sure why it isnt working for you, the code is posted is basically the same code I'm still using. If the code doesn't compile it's just simple things like you'll need to declare mUnityPlayer is UnityPlayer, etc.

 

 

 

QCARUnityPlayer in Subview

August 16, 2014 - 6:01pm #10

I, too, have been trying to put the unity/vuforia into it's own fragment within a regular android activity.  And I, too, am hitting noting but trouble.  If at all possible, could you post the entire code for the fragment and main activity?  Please?? I can't seem to find the magic mix and am getting desperate.  Your snippet  is a starting point, but you can't just cut and paste it...does it go into a regular activity/fragment set up (extending Fragment?), and is the view mentioned the fragment view or the parent of the fragment view (i.e. FrameLayout container = (FrameLayout)view.findViewByID(...) ), and what does your unity_container look like?  is that the layout for the main activity, the parent?  I've tried it multiple ways, but so far, I had hit on the right combo, like you have.  So if you're reading this, could you post the full fragment and main activity code? (suitably stripped of proprietary stuff of course).

A full sample showing how to put the unity/vuforia output into a fragment would be deeply deeply appreciated!  I need to be able to have it in a fragment (ie subview) in order to make it a truly usable application.  The demos are fine as far as they go, but they don't play well with others or match up with Android default themes...and using Vuforia via Unity is the best course in my mind for what we are doing...rebuilding everything ourselves by having to try to do it all via Vuforia Android isn't an option.

and before the moderator comes on and says you simply  have to do what the Knowledge base articles say, or other forum responses, I tried often they don't work, or don't work until you've spent way too long figuring out what is missing.  I think others have also noted this.  Perhaps they are no longer up to date?  

I'm not ruling out my own idiocy...but frankly, a really good sample of embedding a Unity/Vuforia activity as a fragment, along with a full example (aka zip file of all files) of the extending KB article would be greatly appreciated.

i.e. This KB article ( https://developer.vuforia.com/resources/dev-guide/extending-unity-android-activity-and-adding-custom-views-eclipse) is a good start, (even if it doesn't take into account you can export the Unity Vuforia project as a Google project), but it would be nice if it actually went a bit further and hooked up the button (or preferably the layout) and showed that it does work.  (mine button so far, shows up but doesn't click...and am following the KB article, and attached a new click listener at button creation during the runnable).

Samples I'd dearly wish to see: (full sample, not snippet where you miss out on seeing the members, includes, other variables that you need  to get it to actually work)

1) Using latest Unity, a sample Google Project  (output from Unity) with the vuforia/qcar sample, that then shows you how to modify that project to use an android activity with fragment layout, such that the android app is a regular android app (actionbar, etc) but a subview contains the vuforia/unity output.

2) Same as above but with up to date override example to overlay a layout (not just a programmatic button) on the vuforia/unity fragment. An updated and expanded version of the above mentioned KB article, basically.

 

Thanks so much!

Cynthia

 

 

QCARUnityPlayer in Subview

March 9, 2014 - 12:05am #9

Nice, thanks for sharing this!

 

QCARUnityPlayer in Subview

March 8, 2014 - 3:37pm #8

After much trial and error, I got things working fine with a subview - actually it is a fragment inside an activity which has it's own view. This is in the fragment onCreateView:

 

        mUnityPlayer = new UnityPlayer(getActivity());
        final int mode = mUnityPlayer.getSettings().getInt("gles_mode", 1);

        this.mQCARShared = new QCARPlayerSharedActivity();
        this.mQCARShared.onCreate(getActivity(), mode, new QCARPlayerSharedActivity.IUnityInitializer() {
            @Override
            public void InitializeUnity() {
                mUnityPlayer.init(mode, false);
                FrameLayout container = (FrameLayout)view.findViewById(R.id.unity_container);
                FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
                container.addView(mUnityPlayer, 0, lp);

                mUnityPlayer.windowFocusChanged(true);
                mUnityPlayer.resume();
            }
        });

Hope this helps you.

 

QCARUnityPlayer in Subview

May 15, 2013 - 3:45am #7

Help Please!

Hello, I would like to show a button (Android Button) in front of my AR cam, and I can do it...

If I can't do the vuforia screen smaller, how could I put a Button in front of the screen?

 

Thank you!!

QCARUnityPlayer in Subview

June 28, 2012 - 3:52pm #6

Have anybody accomplished this? I have the same scenario and I'm having the same error than dd.

Any help will be appreciated.

 

Re: QCARUnityPlayer in Subview

April 3, 2012 - 2:09pm #5

You need to extend these two classes:

com.qualcomm.QCARUnityPlayer.QCARPlayerActivity
com.qualcomm.QCARUnityPlayer.QCARPlayerNativeActivity

Don't try to work with the QCARPlayerSharedActivity directly, you'll be missing some important steps in the classes above.

- Kim

Re: QCARUnityPlayer in Subview

April 3, 2012 - 6:48am #4

i tried it like this:

public class ViewexampleActivity extends Activity{
   
	private UnityPlayer m_UnityPlayer;
	private QCARPlayerSharedActivity mQCARShared;   

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
         
        super.onCreate(savedInstanceState);
  
     // Create the UnityPlayer
     
        this.mQCARShared = new QCARPlayerSharedActivity();
        this.mQCARShared.setActivity(this);
        this.mQCARShared.onCreate(savedInstanceState);

        this.m_UnityPlayer = this.mQCARShared.getUnityPlayer();
        
        setContentView(R.layout.main);
 
        // Add the Unity view

        FrameLayout layout = (FrameLayout) FindViewById(R.id.frameLayout2);    

        LayoutParams lp = new LayoutParams (LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
          
        layout.addView(m_UnityPlayer.getView(), 0, lp); 	
    }

the qcarplayer extends the activity like the unityplayer does so i assume it could work similar as in post#1 described.
i tried it like it is done in the QCARPlayerActivity.java

now i get the error: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

my manifest only has the viewexample activity with intent main and launcher (if i add the others it won't change anything QCARPlayerActivity, QCARPlayerNativeActivity, com.unity3d.player.VideoPlayer)

what am i missing?

Re: QCARUnityPlayer in Subview

March 29, 2012 - 11:36am #3

thx.. i'll try it out.
if someone has other tipps or experience with smth like this..

everything is appreciated :)

Re: QCARUnityPlayer in Subview

March 28, 2012 - 7:51pm #2

This probably isn't possible with the default samples, which render the video background under the hood. You may want to try with the BackgroundTextureAccess sample, however. This sample renders the camera image in Unity using a standard Texture2D object.

Also note that you will need to extend the QCAR-specific activities. See this threads for some tips:

http://ar.qualcomm.at/node/2001243

Please note this isn't something we have tested. Let us know how it works!

- Kim

Log in or register to post comments