plantilla base para movimiento básico
This commit is contained in:
Robii Aragon
2026-02-05 05:07:55 -08:00
parent 195b696771
commit 779f2c8b20
14443 changed files with 23840465 additions and 452 deletions

View File

@@ -0,0 +1,238 @@
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections;
using UnityEngine.UI;
using System;
[System.Serializable]
//System to use touch buttons to check if the button is being pressing, holding or released
public class touchButtonListener : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
[Header ("Main Settings")]
[Space]
public bool changeColorOnPress;
public Color releaseColor;
public Color pressColor;
public float colorChangeSpeed;
public bool useCurrentColorForRelease;
// public inputManager input;
[Space]
[Header ("Debug")]
[Space]
public bool pressedDown = false;
public bool pressedUp = false;
public bool pressed;
public bool buttonColorVisible = true;
public int currentFingerId;
[Space]
[Header ("Components")]
[Space]
public RawImage buttonIcon;
Color currentColorForRelease;
Coroutine colorTransition;
Touch currentTouch;
int currentTouchCount;
bool touchPlatform;
void Start ()
{
if (changeColorOnPress) {
if (useCurrentColorForRelease && buttonIcon != null) {
currentColorForRelease = buttonIcon.color;
}
}
touchPlatform = touchJoystick.checkTouchPlatform ();
}
//if you press the button
public void OnPointerDown (PointerEventData eventData)
{
pressedDown = true;
pressed = true;
if (changeColorOnPress) {
changeColor (true);
}
StartCoroutine (disableDown ());
// input.increaseCurrentNumberOfTouchButtonsPressed ();
// print (eventData.pointerId);
//checkTouchID ();
}
public void checkTouchID ()
{
currentTouchCount = Input.touchCount;
if (!touchPlatform) {
currentTouchCount++;
}
for (int i = 0; i < currentTouchCount; i++) {
if (!touchPlatform) {
currentTouch = touchJoystick.convertMouseIntoFinger ();
} else {
currentTouch = Input.GetTouch (i);
}
if (currentTouch.phase == TouchPhase.Began) {
currentFingerId = currentTouch.fingerId;
}
}
}
public int getCurrentFingerId ()
{
return currentFingerId;
}
//if you release the button
public void OnPointerUp (PointerEventData eventData)
{
pressedUp = true;
pressed = false;
if (changeColorOnPress) {
changeColor (false);
}
StartCoroutine (disableUp ());
// input.decreaseCurrentNumberOfTouchButtonsPressed ();
}
//disable the booleans parameters after press them
IEnumerator disableDown ()
{
WaitForSeconds delay = new WaitForSeconds (0.001f);
yield return delay;
pressedDown = false;
}
IEnumerator disableUp ()
{
WaitForSeconds delay = new WaitForSeconds (0.001f);
yield return delay;
pressedUp = false;
}
//if the button is disabled, reset the button
void OnDisable ()
{
resetPressedState ();
}
void resetPressedState ()
{
pressedDown = false;
pressedUp = false;
pressed = false;
}
void changeColor (bool state)
{
if (!buttonColorVisible) {
return;
}
if (colorTransition != null) {
StopCoroutine (colorTransition);
}
colorTransition = StartCoroutine (changeColorCoroutine (state));
}
IEnumerator changeColorCoroutine (bool state)
{
if (buttonIcon != null) {
Color targetColor = Color.white;
if (state) {
targetColor = pressColor;
} else {
if (useCurrentColorForRelease) {
targetColor = currentColorForRelease;
} else {
targetColor = releaseColor;
}
}
while (buttonIcon.color != targetColor) {
buttonIcon.color = Color.Lerp (buttonIcon.color, targetColor, Time.deltaTime * colorChangeSpeed);
yield return null;
}
}
yield return null;
}
public void setButtonColorVisibleState (bool state)
{
if (buttonIcon != null) {
buttonColorVisible = state;
Color targetColor = buttonIcon.color;
if (buttonColorVisible) {
targetColor.a = releaseColor.a;
} else {
targetColor.a = 0;
}
buttonIcon.color = targetColor;
}
}
public void resetButtonState ()
{
if (pressed || pressedDown || pressedUp) {
resetPressedState ();
if (buttonColorVisible) {
Color targetColor = buttonIcon.color;
targetColor.a = releaseColor.a;
buttonIcon.color = targetColor;
}
}
}
public void setButtonIconComponent ()
{
buttonIcon = GetComponent<RawImage> ();
// input = FindObjectOfType <inputManager> ();
updateComponent ();
}
void updateComponent ()
{
GKC_Utils.updateComponent (this);
}
}

View File

@@ -0,0 +1,17 @@
fileFormatVersion: 2
guid: d6b4a91fd2086bf42808ae72e80539f4
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 40995
packageName: Game Kit Controller - Shooter Melee Adventure Creator 3D + 2.5D
packageVersion: 3.77g
assetPath: Assets/Game Kit Controller/Scripts/Mobile/touchButtonListener.cs
uploadId: 814740

View File

@@ -0,0 +1,450 @@
using UnityEngine;
using System.Reflection;
using UnityEngine.UI;
public class touchJoystick : MonoBehaviour
{
[Header ("Main Settings")]
[Space]
public int fingerId = 666;
public float dragDistance = 1;
public bool snapsToFinger = true;
public bool hideOnRelease = false;
public bool touchPad;
public float touchPadHorizontalExtraValue = 1;
public float touchPadVerticalExtraValue = 1;
public bool showJoystick;
[Space]
[Header ("Other Settings")]
[Space]
public bool clampJoystickToScreenSide;
public bool joystickOnRightScreenSide;
[Space]
public bool checkIfPressInsideTouchZone;
public RectTransform touchZoneRectTransform;
[Space]
[Header ("Colors Settings")]
[Space]
public bool useChaneColor;
public float changeColorSpeed;
public Color regularBaseColor;
public Color regularStickColor;
public Color pressedBaseColor;
public Color pressedStickColor;
[Space]
[Header ("Debug")]
[Space]
public bool showDebugPrint;
public Vector2 currentAxisValue;
public Vector2 currentTouchPosition;
public bool touching;
public bool hoveringJoystick;
public bool touchingPreviously;
public bool adjustinColors;
public bool buttonColorVisible = true;
public bool joystickClampedToScreenSideActive;
[Space]
[Header ("Components")]
[Space]
public GameObject stickBase;
public GameObject stick;
public RectTransform baseRectTransform;
public RectTransform stickRectTransform;
public RawImage baseRenderer;
public RawImage stickRenderer;
bool touchPlatform;
Color currentBaseColor;
Color currentStickColor;
Vector3 previousPosition;
Vector2 originalStickPosition;
Vector2 originalBasePosition;
Vector3 globalTouchPosition;
Vector3 differenceVector;
Vector3 difference;
int touchCount;
Touch currentPressedTouch;
Touch currentTouch;
int currentFingerId;
int currentTouchIndex;
bool firstTouchActive;
float screenWidth;
void Start ()
{
touchPlatform = checkTouchPlatform ();
if (!showJoystick) {
setSticksState (false, false);
}
originalStickPosition = stickRectTransform.anchoredPosition;
originalBasePosition = baseRectTransform.anchoredPosition;
if (clampJoystickToScreenSide) {
screenWidth = GKC_Utils.getScreenResolution ().x / 2;
if (screenWidth <= 0) {
screenWidth = Screen.width / 2;
}
}
}
void Update ()
{
setJoystickColors ();
if (currentFingerId > -1) {
moveJoystick ();
}
}
public void setHoverState (bool state)
{
hoveringJoystick = state;
if (hoveringJoystick && !touching) {
touchCount = Input.touchCount;
if (!touchPlatform) {
touchCount++;
}
for (currentTouchIndex = 0; currentTouchIndex < touchCount; currentTouchIndex++) {
if (!touchPlatform) {
currentTouch = convertMouseIntoFinger ();
} else {
currentTouch = Input.GetTouch (currentTouchIndex);
}
//if the touch action has started, check if the finger is inside the touch zone rect, visible in the editor
if (currentTouch.phase == TouchPhase.Began) {
currentTouchPosition = currentTouch.position;
if (clampJoystickToScreenSide) {
joystickClampedToScreenSideActive = false;
bool isScreenRightSide = currentTouchPosition.x > screenWidth;
if (joystickOnRightScreenSide) {
if (!isScreenRightSide) {
joystickClampedToScreenSideActive = true;
}
} else {
if (isScreenRightSide) {
joystickClampedToScreenSideActive = true;
}
}
if (showDebugPrint) {
print ("joystick Clamped " + joystickClampedToScreenSideActive +
" isScreenRightSide " + isScreenRightSide + " " + currentTouchPosition.x +
" " + screenWidth);
}
if (joystickClampedToScreenSideActive) {
return;
}
}
if (checkIfPressInsideTouchZone) {
if (!RectTransformUtility.RectangleContainsScreenPoint (touchZoneRectTransform, currentTouchPosition)) {
if (showDebugPrint) {
print ("outside touch press " + fingerId);
}
return;
}
}
currentFingerId = currentTouch.fingerId;
fingerTouching (true);
if (snapsToFinger) {
stickRectTransform.position = currentTouchPosition;
baseRectTransform.position = stickRectTransform.position;
}
if (touchPad) {
previousPosition = currentTouchPosition;
}
}
}
}
}
// set the value of touching to activate of deactivate the icons of the joystick
void fingerTouching (bool state)
{
touching = state;
if (showJoystick) {
if (hideOnRelease) {
setSticksState (state, state);
} else if ((!stickBase.gameObject.activeSelf || !stick.gameObject.activeSelf)) {
setSticksState (true, true);
}
} else {
setSticksState (false, false);
}
}
void setSticksState (bool stickBaseState, bool stickState)
{
if (stickBase.activeSelf != stickBaseState) {
stickBase.SetActive (stickBaseState);
}
if (stick.activeSelf != stickState) {
stick.SetActive (stickState);
}
}
//if the joystick is released, the icons back to their default positions
void resetJoystickPosition ()
{
stickRectTransform.anchoredPosition = originalStickPosition;
baseRectTransform.anchoredPosition = originalBasePosition;
currentAxisValue = Vector2.zero;
fingerTouching (false);
}
//check if the joystick is being used and set the icons position according to the finger or mouse movement
void moveJoystick ()
{
if (touching) {
touchCount = Input.touchCount;
if (!touchPlatform) {
touchCount++;
}
currentPressedTouch.fingerId = fingerId;
for (currentTouchIndex = 0; currentTouchIndex < touchCount; currentTouchIndex++) {
if (!touchPlatform) {
currentTouch = convertMouseIntoFinger ();
} else {
currentTouch = Input.GetTouch (currentTouchIndex);
}
if (currentTouch.fingerId == currentFingerId) {
currentPressedTouch = currentTouch;
}
}
if (currentPressedTouch.fingerId == fingerId || currentPressedTouch.phase == TouchPhase.Ended) {
resetJoystickPosition ();
currentFingerId = -1;
return;
}
globalTouchPosition = currentPressedTouch.position;
differenceVector = globalTouchPosition - stickBase.transform.position;
if (differenceVector.sqrMagnitude > dragDistance * dragDistance) {
differenceVector.Normalize ();
differenceVector *= dragDistance;
stickRectTransform.position = baseRectTransform.position + differenceVector;
} else {
stickRectTransform.position = globalTouchPosition;
}
if (!touchPad) {
currentAxisValue = differenceVector / dragDistance;
} else {
difference = globalTouchPosition - previousPosition;
if (differenceVector.sqrMagnitude > dragDistance * dragDistance) {
difference.Normalize ();
}
currentAxisValue = (difference / dragDistance);
currentAxisValue.x *= touchPadHorizontalExtraValue;
currentAxisValue.y *= touchPadVerticalExtraValue;
previousPosition = globalTouchPosition;
}
}
}
//get the vertical and horizontal axis values
public Vector2 GetAxis ()
{
return currentAxisValue;
}
public Vector2 getRawAxis ()
{
Vector2 axisValues = Vector2.zero;
if (currentAxisValue.x > 0) {
axisValues.x = 1;
} else if (currentAxisValue.x < 0) {
axisValues.x = -1;
} else {
axisValues.x = 0;
}
if (currentAxisValue.y > 0) {
axisValues.y = 1;
} else if (currentAxisValue.y < 0) {
axisValues.y = -1;
} else {
axisValues.y = 0;
}
return axisValues;
}
void setJoystickColors ()
{
if (useChaneColor) {
if (buttonColorVisible) {
if (touching != touchingPreviously || !firstTouchActive) {
touchingPreviously = touching;
adjustinColors = true;
firstTouchActive = true;
}
if (adjustinColors) {
if (touching) {
currentBaseColor = pressedBaseColor;
currentStickColor = pressedStickColor;
} else {
currentBaseColor = regularBaseColor;
currentStickColor = regularStickColor;
}
baseRenderer.color = Color.Lerp (baseRenderer.color, currentBaseColor, Time.deltaTime * changeColorSpeed);
stickRenderer.color = Color.Lerp (stickRenderer.color, currentStickColor, Time.deltaTime * changeColorSpeed);
if (baseRenderer.color == currentBaseColor && stickRenderer.color == currentStickColor) {
adjustinColors = false;
}
}
}
}
}
//it simulates touch control in the game with the mouse position, using left button as tap finger with press, hold and release actions
public static Touch convertMouseIntoFinger ()
{
object mouseFinger = new Touch ();
FieldInfo phase = mouseFinger.GetType ().GetField ("m_Phase", BindingFlags.NonPublic | BindingFlags.Instance);
FieldInfo fingerId = mouseFinger.GetType ().GetField ("m_FingerId", BindingFlags.NonPublic | BindingFlags.Instance);
FieldInfo position = mouseFinger.GetType ().GetField ("m_Position", BindingFlags.NonPublic | BindingFlags.Instance);
if (Input.GetMouseButtonDown (0)) {
phase.SetValue (mouseFinger, TouchPhase.Began);
} else if (Input.GetMouseButtonUp (0)) {
phase.SetValue (mouseFinger, TouchPhase.Ended);
} else {
phase.SetValue (mouseFinger, TouchPhase.Moved);
}
fingerId.SetValue (mouseFinger, 11);
position.SetValue (mouseFinger, new Vector2 (Input.mousePosition.x, Input.mousePosition.y));
return (Touch)mouseFinger;
}
//check the if the current platform is a touch device
public static bool checkTouchPlatform ()
{
if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) {
return true;
}
return false;
}
public void setTouchPadState (bool state)
{
touchPad = state;
}
public void setShowJoystickState (bool state)
{
showJoystick = state;
setSticksState (showJoystick, showJoystick);
}
public void setSnapsToFingerState (bool state)
{
snapsToFinger = state;
}
public void setHideOnReleaseState (bool state)
{
hideOnRelease = state;
}
public void setJoystickColorVisibleState (bool state)
{
buttonColorVisible = state;
Color targetColorBase = baseRenderer.color;
Color targetColorStick = stickRenderer.color;
if (buttonColorVisible) {
targetColorBase.a = regularBaseColor.a;
targetColorStick.a = regularStickColor.a;
} else {
targetColorBase.a = 0;
targetColorStick.a = 0;
}
baseRenderer.color = targetColorBase;
stickRenderer.color = targetColorStick;
}
public bool touchJoystickIsPressed ()
{
return touching;
}
}

View File

@@ -0,0 +1,17 @@
fileFormatVersion: 2
guid: e2b5b075a555314458891b24fef9a4c3
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 40995
packageName: Game Kit Controller - Shooter Melee Adventure Creator 3D + 2.5D
packageVersion: 3.77g
assetPath: Assets/Game Kit Controller/Scripts/Mobile/touchJoystick.cs
uploadId: 814740