My project in Unity has a cell entry system array from the link, how can I get it? I want to get 1 when translated forward, I want to get -1 when translated backwards. How can I do that?
https://developer.android.com/training/wearables/user-input/rotary-input
My project in Unity has a cell entry system array from the link, how can I get it? I want to get 1 when translated forward, I want to get -1 when translated backwards. How can I do that? https://developer.android.com/training/wearables/user-input/rotary-input
CodePudding user response:
I'm not sure what your "cell entry system array from the link" involves, but to get the value of a specific cell in an array in Unity, you can use the GetValue()
method of the Array
class. This method takes the index of the cell as a parameter and returns the cell's value at that index.
For example, if you have an array of integers called myArray
and you want to get the value of the cell at index 3, you can use the following code:
int value = (int)myArray.GetValue(3);
To get the direction of translation, you can use the ev.getAxisValue()
method of the MotionEvent object that is passed to the onGenericMotion()
method. This method takes the axis that you want to get the value of as a parameter and returns the value of that axis for the current motion event.
For example, if you want to get the value of the AXIS_SCROLL
axis for the current motion event, you can use the following code:
float value = ev.getAxisValue(MotionEvent.AXIS_SCROLL);
You can then use this value to determine the translation direction and add or subtract 1 from the cell value accordingly:
myView.setOnGenericMotionListener(new View.OnGenericMotionListener() {
@Override
public boolean onGenericMotion(View v, MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_SCROLL &&
ev.isFromSource(InputDeviceCompat.SOURCE_ROTARY_ENCODER)
) {
// Get the current value of the cell at index 3
int value = (int)myArray.GetValue(3);
// Get the value of the AXIS_SCROLL axis for the current motion event
float axisValue = ev.getAxisValue(MotionEvent.AXIS_SCROLL);
// Add or subtract 1 from the cell value based on the direction of translation
if (axisValue > 0) {
value ;
} else if (axisValue < 0) {
value--;
}
// Set the new value of the cell at index 3
myArray.SetValue(value, 3);
return true;
}
return false;
}
});