I am using vuforia library in my unity project where I need to change the DigitalEyewearBehaviour properties on a users button click action. So far the orientation of the screen changes and when checked through the debug log the eyewear type and the mode both are seem to be set perfectly. But the device only changes the screen orientation to landscape and blacks out the camera.
public void ChangeToHeadGearMode(){
//static setting changes
Screen.orientation = ScreenOrientation.Landscape;
//Getting Player Settings
string ViewerType = PlayerPrefs.GetString("Viewer Type","Generic Cardboard (Vuforia)");
string SterioFrameWork = PlayerPrefs.GetString ("Sterio Framework", "0");
mDebug.Log ("Getting VT " + ViewerType);
mDebug.Log ("Getting SF " + SterioFrameWork);
//setting set
mDebug.Log (mHeadGearParameters.GetEyewearType().ToString());
mHeadGearParameters.SetEyewearType(Vuforia.DigitalEyewearAbstractBehaviour.EyewearType.VideoSeeThrough);
mDebug.Log (mHeadGearParameters.GetEyewearType().ToString());
if(SterioFrameWork=="0")
{
mHeadGearParameters.SetStereoCameraConfiguration(Vuforia.DigitalEyewearAbstractBehaviour.StereoFramework.Vuforia);
mDebug.Log ("ST=0");
}
else if(SterioFrameWork=="1")
{
mHeadGearParameters.SetStereoCameraConfiguration(Vuforia.DigitalEyewearAbstractBehaviour.StereoFramework.Cardboard);
mDebug.Log ("ST=1");
}
else if(SterioFrameWork=="2")
{
mHeadGearParameters.SetStereoCameraConfiguration(Vuforia.DigitalEyewearAbstractBehaviour.StereoFramework.GearVR);
mDebug.Log ("ST=2");
}
mHeadGearParameters.SetViewerActive (true,true);
}
The vuforia developer portal says a method as SetViewerActive When I apply that method only the camera blacks out. If I does not use that the camera does not black out but the view does not change to cardboard view. I have tried all the possible options in SetViewerActive method but does not work. I went through the vuforia developer portal a number of times but did not find the error.
What am I doing wrong or where am I missing an important point?
I found the answer to my own question and I am posting it to anyone who might come across the same problem. As of now there are no tutorials or real examples of how this actually works. So I had to go with trial and error and finally I found the solution.
The thing you need to do is just deinit the camera instance and then do the setting changes and then SetViewerActive(true,true) so that the camera will be reinitialised.
The final solution is as follows.
//static setting changes
Screen.orientation = ScreenOrientation.Landscape;
if (CameraDevice.Instance.Stop () && CameraDevice.Instance.Deinit ())
{
//Getting Player Settings
string ViewerType = PlayerPrefs.GetString("Viewer Type","Generic Cardboard (Vuforia)");
string SterioFrameWork = PlayerPrefs.GetString ("Sterio Framework", "0");
mDebug.Log ("Getting VT " + ViewerType);
mDebug.Log ("Getting SF " + SterioFrameWork);
//setting set
mDebug.Log (mHeadGearParameters.GetEyewearType().ToString());
mHeadGearParameters.SetEyewearType(DigitalEyewearAbstractBehaviour.EyewearType.VideoSeeThrough);
mDebug.Log (mHeadGearParameters.GetEyewearType().ToString());
if(SterioFrameWork=="0")
{
mHeadGearParameters.SetStereoCameraConfiguration(DigitalEyewearAbstractBehaviour.StereoFramework.Vuforia);
mDebug.Log ("ST=0");
}
else if(SterioFrameWork=="1")
{
mHeadGearParameters.SetStereoCameraConfiguration(DigitalEyewearAbstractBehaviour.StereoFramework.Cardboard);
mDebug.Log ("ST=1");
}
else if(SterioFrameWork=="2")
{
mHeadGearParameters.SetStereoCameraConfiguration(DigitalEyewearAbstractBehaviour.StereoFramework.GearVR);
mDebug.Log ("ST=2");
}
mHeadGearParameters.SetViewerActive (true,true);
}
Remember to active again the normal view: use DigitalEyewearARController.Instance.SetViewerActive(false, true); to deactivate it and reset the camera.
Related
I'm newbie with Unity3D.
here is my source, very simple
public void persp_ortho_Btn_Clicked()
{
if (!Camera_Controller.shared_instance.isOrthoCamera)
{
Camera_Controller.shared_instance.isOrthoCamera = true;
Camera.main.orthographic = true;
ui_camera.orthographic = true;
}
else
{
Camera_Controller.shared_instance.isOrthoCamera = false;
Camera.main.orthographic = false;
ui_camera.orthographic = false;
}
}
When i run with this code in Editor (Game simulate window)
this works well like below :
But, after build and run .exe,
main camera doesn't change to ortho camera, also like below :
it seems the "orthographic" property of main camera is change true to false, false to true in built runtime as well either. But maybe the view matrix is not changed.
Why this happen and how can i fix this ??
Your code should work unless something is forcing the camera to stay in the same perspective. I am not able to say what might be the cause with the details provided.
Just test out if the camera will switch without the if statement. Like this
public void persp_ortho_Btn_Clicked()
{
Camera.main.orthographic = !Camera.main.orthographic;
}
If so, then there is some issue with the condition check.
Alternatively, you can try using Unity cinemachine to get a smooth transition.
Working on a Unity hybrid VR (cardboard) /2D app. The cardboard side of it works fine. I am having trouble with the 2D/VR switching.
When I am in 2D mode, reticle does not move, although screen taps register. So the app seems unaware of the gyro.
I feel like I am missing something fundamental here. I have a GvrEventSystem prefab that has both an EventSystem and GvrPointerInputModule components.
What obvious thing am I over-looking?
ETA:
I have been asked to add relevant code. Here is the code for 2D-VR switching on-the-fly. This code executes w/out error, and the app switches between VR and 2D mode every 3 seconds:
readonly string NONE_STRING = "";
readonly string CARDBOARD_STRING = "cardboard";
void Start()
{
Invoke("GoPhone", 3.0f);
}
void GoPhone()
{
SetVREnabled(false);
Invoke("GoVR", 3.0f);
}
void GoVR()
{
SetVREnabled(true);
Invoke("GoPhone", 3.0f);
}
void SetVREnabled(bool isEnabled)
{
if (isEnabled)
{
StartCoroutine(LoadDevice(CARDBOARD_STRING));
}
else
{
StartCoroutine(LoadDevice(NONE_STRING));
}
}
IEnumerator LoadDevice(string newDevice)
{
if (String.Compare(XRSettings.loadedDeviceName, newDevice, true) != 0)
{
XRSettings.LoadDeviceByName(newDevice);
yield return null;
if (!XRSettings.loadedDeviceName.Equals(NONE_STRING))
XRSettings.enabled = true;
}
}
Although I feel like my problem is a configuration problem, and not a code problem. In the editor, which does not support VR mode, the app behaves in 2D mode as expected.
Also ETA:
JIC
User error! I did not follow the "Magic Window" instructions as detailed at https://developers.google.com/vr/develop/unity/guides/magic-window... let my folly be a warning to future generations!
I am trying to transform object into VR app using google cardboard sdk in Unity3d. I write a script and comparing input buttons on Update method. So my code is below.
void Update () {
if (Input.GetButtonDown("Fire1"))
{
onObjectDown();
}
if (Input.GetButtonDown("Fire2"))
{
onObjectExit();
}
if (Input.GetButtonDown("Fire3"))
{
onObjectExit();
}
if (dragging)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 rayPoint = ray.GetPoint(distance);
transform.position = rayPoint;
}
}
onObjectDown() & onObjectExit() method are as follows
public void onObjectDown()
{
Debug.Log(name + " Game Object Down!");
distance = Vector3.Distance(transform.position, Camera.main.transform.position);
dragging = true;
}
public void onObjectExit()
{
dragging = false;
Debug.Log(name + " Game Object Exit!");
GetComponent<Renderer>().material.color = originalColor;
}
This coding is perfectly working on play mode on desktop. But on emulator only onObjectDown is executing. This means only "Fire1" button is pressed. Is anyone know how to get input from button "Fire2" & "Fire3" via bluetooth controller ?
For reference, my input setting in project is as below
Button 1 on Bluetooth controller you can initializing with Input.GetMouseButton(0) or Fire 1 because it simulate display tap. But the other controller button simulate (in my case) back button on Android device, so u can initializing with Input.GetKeyDown(KeyCode.Escape).
enter image description here
Since that the Unity Plugin mentioned above display only the Character into the Game Scene when I cliked the button DONE, I am trying to add code into UmazingCC.cs file to display both Character and the Name added in the tab to show both of them in the Game Scene.
The original UmazingCC part code is the following
enter code hereif (GUILayout.Button("Done", bottomBarButtonStyle)){
// Save the character, fade out the camera and load up the game scene
GameObject avatarGO;
if (selectedGender == "Man") {
avatarGO = maleAvatarSpawn;
} else {
avatarGO = femaleAvatarSpawn;
}
var avatar = avatarGO.GetComponent<UMAAvatarBase>();
if( avatar != null )
{
string finalPath = savePath + "/" + characterName + ".txt";
if (finalPath.Length != 0)
{
PersistentNameHolder.characterName = characterName;
var asset = ScriptableObject.CreateInstance<UMATextRecipe>();
asset.Save(avatar.umaData.umaRecipe, avatar.context);
System.IO.File.WriteAllText(finalPath, asset.recipeString);
ScriptableObject.Destroy(asset);
// If the camera has a fader, make it fade out and load the game scene, otherwise just load the scene ourselves
if (orbitCamera.GetComponent<CameraFader>() != null) {
orbitCamera.GetComponent<CameraFader>().fadeOutAndLoadScene(gameSceneName);
} else {
Application.LoadLevel(gameSceneName);
}
The Name metod code is the following
enter code here void FinishWindowContents (int windowID) {
GUILayout.BeginHorizontal(horizontalLayoutStyle);
GUILayout.BeginVertical(verticalLayoutStyle);
GUILayout.Label ("Nombre", bottomBarLabelStyle);
characterName = GUILayout.TextField(characterName);
GUILayout.EndVertical();
GUILayout.BeginVertical(verticalLayoutStyle);
if (characterName.Length == 0) {
GUI.enabled = false;
}
For it, Could anybody say me what code lines should I add in this place to show both Character and its name in the Game Scene?
Thanks in advance and sorry for my little english.
Ps. I added a screen image to see better the problem.
I am making a scene game with a Character Avatar and its name (The interface has a text field to add in it its name) and then when I clicked the DONE button, I want to run a new scene to display both Character Avatar and the name added into text field, for it, Could anybody help me to make the code to do it? Thanks in advance Alejandro Castan Ps. Sorry for my little English and hope you can understand me
void FinishWindowContents (int windowID) {
GUILayout.BeginHorizontal(horizontalLayoutStyle);
GUILayout.BeginVertical(verticalLayoutStyle);
GUILayout.Label ("Name", bottomBarLabelStyle);
characterName = GUILayout.TextField(characterName);
GUILayout.EndVertical();
GUILayout.BeginVertical(verticalLayoutStyle);
if (characterName.Length == 0) {
GUI.enabled = false;
}
if (GUILayout.Button("Done", bottomBarButtonStyle)) {
// Save the character, fade out the camera and load up the game scene
GameObject avatarGO;
if (selectedGender == "Man") {
avatarGO = maleAvatarSpawn;
} else {
avatarGO = femaleAvatarSpawn;
}
var avatar = avatarGO.GetComponent<UMAAvatarBase>();
if( avatar != null )
{
string finalPath = savePath + "/" + characterName + ".txt";
if (finalPath.Length != 0)
{
PersistentNameHolder.characterName = characterName;
var asset = ScriptableObject.CreateInstance<UMATextRecipe>();
asset.Save(avatar.umaData.umaRecipe, avatar.context);
System.IO.File.WriteAllText(finalPath, asset.recipeString);
ScriptableObject.Destroy(asset);
// If the camera has a fader, make it fade out and load the game scene, otherwise just load the scene ourselves
if (orbitCamera.GetComponent<CameraFader>() != null) {
orbitCamera.GetComponent<CameraFader>().fadeOutAndLoadScene(gameSceneName);
} else {
Application.LoadLevel(gameSceneName);
}
}
}
}
if (characterName.Length == 0) {
GUI.enabled = true;
}
GUILayout.EndVertical();
GUILayout.EndVertical();
}
Note-I added some code(this code is more complex that I asked above) you can see it. This code is from Unity plugin and what I would need to add only the text added in the text field into the new scene when I do click in the Done button.
PlayerPrefs is a way to save data between scenes.
It doesn't need some extra code and is great for what you are doing because it allows you to write/read in any moment of your game.
Hope you'll find useful.