using UnityEngine; using UnityEngine.EventSystems;...

बनाया गया: 3 जून 2025

का उपयोग करके उत्तर दिया गया O4 Mini High द्वारा Chat01

प्रश्न

using UnityEngine;
using UnityEngine.EventSystems;

public class camaraControl : MonoBehaviour
{
public float keySpeed;
public float dragSpeed;
public float inertiaDrag;
public float zoomSpeed;
public float zoomSpeedTouch;

text
public Vector2 zoomLimits = new Vector2(); private Vector3 inertiaVelocity; private Vector3 dragOrigin; private bool isDragging = false; void Update() { if (ap1Control.isPaused) return;

#if UNITY_STANDALONE || UNITY_EDITOR
HandleKeyboard();
HandleScreenEdge();
ApplyInertia();
HandleDrag();
HandleZoom();
#elif UNITY_ANDROID || UNITY_IOS
if (BuildingGridPlacer.IsPlacingBuilding)
{
HandleScreenEdge();
}
else
{
ApplyInertia();
HandleDrag();
HandleZoom();
}
#endif
}

text
void HandleKeyboard() { float horizontal = Input.GetAxisRaw("Horizontal"); float vertical = Input.GetAxisRaw("Vertical"); Vector3 direction = new Vector3(horizontal, vertical, 0).normalized; transform.position += direction * keySpeed * Time.deltaTime; } void HandleDrag() { if (EventSystem.current != null && EventSystem.current.IsPointerOverGameObject()) return; if (Input.GetMouseButtonDown(0)) { dragOrigin = Input.mousePosition; isDragging = true; inertiaVelocity = Vector3.zero; return; } if (Input.GetMouseButton(0)) { Vector3 difference = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin); Vector3 move = new Vector3(-difference.x * dragSpeed, -difference.y * dragSpeed, 0); transform.Translate(move, Space.World); dragOrigin = Input.mousePosition; inertiaVelocity = move / Time.deltaTime; } if (Input.GetMouseButtonUp(0)) { isDragging = false; } } void ApplyInertia() { if (!isDragging && inertiaVelocity.magnitude > 0.01f) { transform.Translate(inertiaVelocity * Time.deltaTime, Space.World); inertiaVelocity = Vector3.Lerp(inertiaVelocity, Vector3.zero, inertiaDrag * Time.deltaTime); } } void HandleScreenEdge() { float edgeThickness = 10f; Vector3 move = Vector3.zero; if (Input.mousePosition.x < edgeThickness) { move.x = -1; } else if (Input.mousePosition.x > Screen.width - edgeThickness) { move.x = 1; } if (Input.mousePosition.y < edgeThickness) { move.y = -1; } else if (Input.mousePosition.y > Screen.height - edgeThickness) { move.y = 1; } Vector3 moveDirection = new Vector3(move.x, move.y, 0).normalized; transform.position += moveDirection * dragSpeed * Time.deltaTime; } void HandleZoom()

{
float scroll = Input.GetAxis("Mouse ScrollWheel");
if (scroll != 0f)
{
Camera.main.orthographicSize -= scroll * zoomSpeed;
}

text
if (Input.GetKey(KeyCode.Q) || Input.GetKey(KeyCode.KeypadPlus)) { Camera.main.orthographicSize -= zoomSpeed * Time.deltaTime; } if (Input.GetKey(KeyCode.E) || Input.GetKey(KeyCode.KeypadMinus)) { Camera.main.orthographicSize += zoomSpeed * Time.deltaTime; } if (Input.touchCount == 2) { Touch touchZero = Input.GetTouch(0); Touch touchOne = Input.GetTouch(1); Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition; Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition; float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude; float touchDeltaMag = (touchZero.position - touchOne.position).magnitude; float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag; Camera.main.orthographicSize += deltaMagnitudeDiff * zoomSpeedTouch; } Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize, zoomLimits.x, zoomLimits.y);

}

}


como hago que el edgethickness del HandleScreenEdge() sea mas ancho si la plataforma es android?

विचार

Adapting behavior for Android

The user wants to adjust the edge thickness in the HandleScreenEdge() method based on whether the platform is Android. This can be done in Unity using platform checking like Application.platform == RuntimePlatform.Android. I can suggest modifying the method to evaluate the platform and set edgeThickness accordingly. One way could be using this approach:
`float edgeThickness = (Application.platform == RuntimePlatform.Android) ? some value : default

Thought for 4 seconds

उत्तर

Para ajustar dinámicamente el grosor del borde (​edgeThickness​) en ​HandleScreenEdge()​ según la plataforma (por ejemplo, que sea más ancho en Android), basta con comprobar en tiempo de ejecución (o mediante directivas de compilación) si estamos en Android y asignar un valor distinto. Por ejemplo:

csharp
void HandleScreenEdge() { // Opción A: usando directivas de compilación #if UNITY_ANDROID || UNITY_IOS float edgeThickness = 30f; // más ancho en dispositivos móviles #else float edgeThickness = 10f; // valor por defecto en PC/Editor #endif Vector3 move = Vector3.zero; if (Input.mousePosition.x < edgeThickness) { move.x = -1; } else if (Input.mousePosition.x > Screen.width - edgeThickness) { move.x = 1; } if (Input.mousePosition.y < edgeThickness) { move.y = -1; } else if (Input.mousePosition.y > Screen.height - edgeThickness) { move.y = 1; } Vector3 moveDirection = new Vector3(move.x, move.y, 0).normalized; transform.position += moveDirection * dragSpeed * Time.deltaTime; }

Si en lugar de usar ​#if UNITY_ANDROID​ prefieres detectar la plataforma en tiempo de ejecución (por ejemplo, para tener un solo binario), puedes hacer algo así:

csharp
void HandleScreenEdge() { // Opción B: comprobación en tiempo de ejecución float edgeThickness; #if UNITY_ANDROID || UNITY_IOS // Application.isMobilePlatform también funciona, pero explícito es más claro: edgeThickness = 30f; #else edgeThickness = 10f; #endif // (El resto del método se mantiene igual) Vector3 move = Vector3.zero; if (Input.mousePosition.x < edgeThickness) { move.x = -1; } else if (Input.mousePosition.x > Screen.width - edgeThickness) { move.x = 1; } if (Input.mousePosition.y < edgeThickness) { move.y = -1; } else if (Input.mousePosition.y > Screen.height - edgeThickness) { move.y = 1; } Vector3 moveDirection = new Vector3(move.x, move.y, 0).normalized; transform.position += moveDirection * dragSpeed * Time.deltaTime; }

O bien, si quieres comprobar en tiempo de ejecución sin directivas (útil si compilas un mismo .apk para varias plataformas), puedes usar:

csharp
void HandleScreenEdge() { float edgeThickness = Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer ? 30f : 10f; Vector3 move = Vector3.zero; if (Input.mousePosition.x < edgeThickness) { move.x = -1; } else if (Input.mousePosition.x > Screen.width - edgeThickness) { move.x = 1; } if (Input.mousePosition.y < edgeThickness) { move.y = -1; } else if (Input.mousePosition.y > Screen.height - edgeThickness) { move.y = 1; } Vector3 moveDirection = new Vector3(move.x, move.y, 0).normalized; transform.position += moveDirection * dragSpeed * Time.deltaTime; }

Con cualquiera de estas tres aproximaciones, el resultado será que, al ejecutar en Android (o iOS), ​edgeThickness​ tome un valor mayor (30 px en el ejemplo), y en PC/Editor siga siendo 10 px. Ajusta los valores (30f, 10f) a tus necesidades.

इस प्रश्नोत्तर को साझा करें