#unity3d
#unity3d
Вопрос:
У меня есть приложение, в котором мне нужно увеличить яркость Android с помощью прокрутки вверх и вниз, но проблема в том, что я хочу постоянно увеличивать яркость, если я прокручиваю вверх и уменьшаю, если я прокручиваю вниз. Мой код
if(t.phase == TouchPhase.Ended)
{
//save ended touch 2d point
secondPressPos = new Vector2(t.position.x,t.position.y);
//create vector from the two points
currentSwipe = new Vector3(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
//normalize the 2d vector
currentSwipe.Normalize();
//swipe upwards
if(currentSwipe.y > 0 amp;amp; currentSwipe.x > -0.5f amp;amp; currentSwipe.x < 0.5f)
{
if(Brightness >= 1.0f)
{
Brightness = 1.0f;
}
else if (Brightness <= -1.0f){
Brightness = 0.0f;
}
else{
Brightness = Brightness 0.2f;
}
}
}
Этот код работает, только если я хочу увеличить или уменьшить яркость на некоторое значение, но затем мне нужно снова провести пальцем, чего я не хочу.
Ответ №1:
Я бы просто не нормализовал вектор, а использовал его как фактор, например,
// Configure some threshold (in pixels) the user has to swipe before changing the brightness
[SerializeField] private float THRESHOLD = 0f;
// Since the position change is in pixels of the display
// configure here how this movement is mapped into your brightness change
[SerializeField] private float SENSITIVITY = 0.01f;
private void Update()
{
if(Input.touchCount < 1) return;
var t = Input.GetTouch(0);
switch (t.phase)
{
case TouchPhase.Began:
firstPressPos = t.position;
break;
case TouchPhase.Moved:
secondPressPos = t.position;
var currentSwipe = secondPressPos - firstPressPos;
if(Mathf.Abs(currentSwipe.x) < 0.5f amp;amp; Mathf.Abs(currentSwipe.y) > THRESHOLD)
{
// remove the threshold amount
currentSwipe.y -= Mathf.Sign(currentSwipe.y) * THRESHOLD;
// Smooth change the brightness instead of stepwise
// depending on how far the user swiped
Brightness = currentSwipe.y * SENSITIVITY;
// Sanatize value between 0 and 1
Brightness = Mathf.Clamp(Brightness, 0, 1);
}
break;
}
}