Windows Mixed Reality - AttachToController - c#

So I'm using the AttachToController script to attach a window floated on top of the controller - which works great. In the script that calls up the window, I figure out which hand pressed the controller's menu button and set the Handedness field appropriately (Left or Right). The problem I'm trying to solve is this: Let's say the user clicks on the right controller's menu button and then later on, the left menu button is clicked. The problem I'm experiencing is that even though I've altered the Handedness field, the window still appears attached to the right controller.
private void InteractionManager_InteractionSourcePressed(InteractionSourcePressedEventArgs args)
{
hand = args.state.source.handedness;
...
}
private void SetHandednessAndActivate(GameObject go)
{
AttachToController script = go.GetComponentInChildren<AttachToController>();
if (script != null)
{
script.Handedness = hand;
}
go.SetActive(true);
}
Just to be clear, if the user clicks the left controller menu button first, the window is always on the left and the same holds true for the right controller. What I want is for the window to move to whichever controller is used.

Instead of
script.Handedness = hand;
Use
script.ChangeHandedness(hand);
All the other bits are handled by the script.

Related

Back button on ui appears until you hit options and back

New to coding here, setting up a UI in unity and having a visual issue where the back button appears until I hit options and back on the start up page, any reccomendations for a fix?
Image showing back button behind the quit button
Settings of the image on the right
You can use Gameobject.setactive(true/false) and try to manage the panel by making empty game object inside UI Canvas and name it as a panel so the whole button became a child and you can easily manage it
for example like this one
public Gameobject startPanel;
public Gameobject optionPanel;
public void Startpnl()
{
//deadActive optionPanel and Activate startPanel
startPanel.setActive(true);
optionPanel.setActive(false);
}
public void optionpnl()
{
//deadActive starPanel and Activate OptionPanel
startPanel.setActive(false);
optionPanel.setActive(true);
}

Getting the menu button input on the SteamVR controller

Hi2,
Does anyone know how to get the menu button input on the steamVR controller via C# code in Unity?
Currently I am able to get the input from the the trigger, trackpad, and grip button.
private void Update()
{
if (SteamVR_Input._default.inActions.GrabGrip.GetStateDown(inputSource))
Debug.Log("grab grip"); // the side button on the controller
if (SteamVR_Input._default.inActions.GrabPinch.GetStateDown(inputSource))
Debug.Log("grab pinch"); // the back button on the controller
if (SteamVR_Input._default.inActions.Teleport.GetStateDown(inputSource))
Debug.Log("teleport"); // the big middle button on the controller
}
any help is appreciated. ^_^
Select in the menu 'Window -> SteamVR Input'. From there click on 'Open Binding UI'.
Your browser will open a screen where you can see this:
The red arrow points to where you can add an action for the menu button (sorry, mine is in German). Call it 'MenuClick' and make sure you save that in your private settings.
Then, in your code access it as
if (SteamVR_Input._default.inActions.MenuClick.GetStateDown(inputSource))
Debug.Log("menu button pressed"); // the menu button on the controller

Button `OnSelect` not functioning in Unity.UI Canvas situation

I have three buttons, when you tap one button, a panel with a text appear.
The problem is when I attached the script, nothing happens.
The "click" is registered but the the panel never appears.
My script is attached to each of the button and is something like this:
public GameObject panel; //i use to put the panel in unity
bool selected = false;
void Start () {
panel.SetActive(false);
}
void OnSelect() {
selected = !selected;
panel.SetActive(true);
}
I probably have to do something else with the panel but I can't figure it out.
Do it like this:
(1) Add the canvas to your project
(2) BIG TIP - be sure to select Scale with screen size.
That's the only one you ever use. Unity accidentally set the wrong default there, they have not fixed it yet.
(3) In your Canvas, add a BUTTON Make it say perhaps "Test"
(3) In your Canvas, add another BUTTON Make it say perhaps "Another Test"
(4) Make a script something like this...
public class MainScreen:MonoBehaviour
{
public void UserClickedTest()
{
Debug.Log("test..");
}
public void UserClickedAnotherTest()
{
Debug.Log("another test..");
}
}
(5) put ONE copy of that script on ANY object you like. You can put it on your camera, on the canvas, or anywhere else that makes sense
For now let's say you put it on your CAMERA object, for example.
(6) Click on the button "Test" .....
And do this ...
click the PLUS button under OnClick
you see the slot that says "_main" in this example. DRAG your CAMERA item from HEIRARCHY, to that slot
Using the drop down menu:
select the "UserClickedTest()" function ...
good eh?
Now for the other button, do the same but select the "UserClickedAnotherTest()" function.
You're done! Run and test!
You can't use the OnSelect system unless you use ISelectHandler and more stuff: it is difficult for beginners. I strongly recommend the OP masters the simpler technique I explain here. Enjoy!
Maybe you attached the script to the panel. If so, your scripts can not be executed as long as your GameObject is SetActive(false).
I hope I was able to help you.

C# - Can't get buttons to react to index of element in list

I have a picturebox in my system that displays a thumbnail image from a List<Bitmap> called images. There is a button on either side of the picturebox that allows the user to move to the previous or next image. What I would like to happen is that when the picturebox is on the first image of the List, then the 'previous' button is disabled, and same for the last image and the 'next' button. At any other time, the user should be able to scroll in either direction.
I have written a function called checkFirstLast() to try and implement this, and am testing it with only two elements in the images List. The function is shown below, and is called every time the image on the picturebox is changed:
//Bitmap passed in represents image currently displayed on picbox
public void checkFirstLast(Bitmap displayImage)
{
if (displayImage == images.First())
{
//cant go back
//set prev button as inactive
btnPrevImage.Enabled = false;
btnPrevImage.BackColor = INACTIVECOLOUR;
//can go forward
//set next button as active
btnNextImage.Enabled = true;
btnNextImage.BackColor = ACTIVECOLOUR;
}
else if (displayImage == images.Last())
{
//can go back
//set prev button as active
btnPrevImage.Enabled = true;
btnPrevImage.BackColor = ACTIVECOLOUR;
//cant go forward
//set next button as inactive
btnNextImage.Enabled = false;
btnNextImage.BackColor = INACTIVECOLOUR;
}
else
{
//can go back
//set prev button as active
btnPrevImage.Enabled = true;
btnPrevImage.BackColor = ACTIVECOLOUR;
//can go forward
//set next button as active
btnNextImage.Enabled = true;
btnNextImage.BackColor = ACTIVECOLOUR;
}
}
When the application loads, it loads to the first element in the images, and the checkFirstLast() function is run. This means that at this point, the 'previous' button is disabled while the 'next' button is enabled. However, when the 'next' button is clicked (checkFirstLast() run again) and the picturebox shows the second (last) element in images, neither button changes enabled state as it should.
This means that the system becomes stuck on the last element in the List and can go neither backwards (button not enabled), nor forwards (no more images, although button stays enabled).
I don't really understand what could be going wrong, surely if the function operates as expected first time around then it can't just not work when called again?
Not sure where to go from here, any advice or a point in the direction of where to look would be greatly appreciated.
Thanks in Advance,
Mark

Right mouse click on WebBuild

I am trying to make my game have the ability to right mouse button click on web builds.
void MouseCheck()
{
if(Input.GetMouseButton(1))
{
//my code
}
}
But it doesn't get detected because when I right click in web build it shows some default options such as full screen.
This is a bit tricky, but not impossible.
You need to make changes on the built's HTML. This is how you would do it:
var params =
{
disableContextMenu: true,
};
This parameter will notify Unity Web Player whether it should display the ContextMenu or not. This in turn prevents the context menu from appearing, which then allows your game to check for Right-Mouse Clicks.
Here is a link to all the customization you can do to the HTML file: Unity WebPlayer's Behaviours.
It is important that you include the C# code which does the actual right click checks happen.
if(Input.GetMouseButtonDown(1))
{
// Code here.
}
This was added for future readers who might benefit from this part

Categories