0
\$\begingroup\$

I'm sure there has to be a simple answer to this, but frankly I cannot find anything about it anywhere on the internet, so I'm asking here. How do I add a pointer like this hand to the side of the currently selected UI button in my game?

I tried using Unity's built in UI system to do this, using the "sprite swap" feature that changes sprites based on whether or not the button is currently selected. I made an image for my "Play" button, and then an image for when the play button was selected that included a pointer as part of the image, but the image would either resize when the button was selected, or the image was moved to the right to accommodate Unity's centering mechanic on the UI. Perhaps this wasn't so wrong, and I'm just not using the UI system correctly.

Does a feature like this have to be hard coded into the UI design?

\$\endgroup\$
2
\$\begingroup\$

I would write a small script that takes the currently selected button and has a fuction that gets called from a event that changes the position of the hand to the position of the selected button + an offset so that it is to the side.

\$\endgroup\$
1
\$\begingroup\$

It is absolutely doable with the built in UI. Find a way yo store the hands all possible positions in a List or Array. I.e:
-you can just subtract the distance from your UI buttons positions to the hand positions, and then store the new positions.
Then find a way to change your sprites position based on user input i.e:
-declare an index variable, and increment, decrement, and clamp it according to user input and your design, i.e:

In your start or wherever you need to declare them:

GameObject handPrefab=//YourHandPrefab added in editor, etc  
Vector3[] handPositions=//hand positions  

In your update:

if(yourUpButtonPressed){  

  handPositionIndex++;  
  handPositionIndex=handPositionIndex%handPositions.Length; //this jumps back to 0 if Length reached
  handPosition.transform.position=handPositions[handPositionIndex]; 

}

Obviously, you gotta do all these for your directions, you are going to use.

Or if you going an event driven way:
Declare your event in start or where you want it and add a lambda expression that matches the signature.

public event YourUIDelegate OnUIMenuNavigate;  


OnUIMenuNavigate+=(int x)=>{    

 handPositionIndex+=x;  
 handPositionIndex=handPositionIndex%handPositions.Length; //this jumps back to 0 if Length reached
 handPosition.transform.position=handPositions[handPositionIndex];  

};

Call your OnUIMenuNavigate where you deal with the logic of navigation in the UI.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.