Added basic character controller via importing Starter Assets from Unity.

This commit is contained in:
2025-11-08 11:59:44 +09:00
parent 3e92e5684a
commit e7e0733258
347 changed files with 46833 additions and 8 deletions

View File

@@ -0,0 +1,129 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
public class TouchscreenInput : MonoBehaviour
{
[Header("Settings")]
[Tooltip("Move joystick magnitude is in [-1;1] range, this multiply it before sending it to move event")]
public float MoveMagnitudeMultiplier = 1.0f;
[Tooltip("Look joystick magnitude is in [-1;1] range, this multiply it before sending it to move event")]
public float LookMagnitudeMultiplier = 1.0f;
public bool InvertLookY;
[Header("Events")]
public UnityEvent<Vector2> MoveEvent;
public UnityEvent<Vector2> LookEvent;
public UnityEvent<bool> JumpEvent;
public UnityEvent<bool> SprintEvent;
private UIDocument m_Document;
private VirtualJoystick m_MoveJoystick;
private VirtualJoystick m_LookJoystick;
private void Awake()
{
m_Document = GetComponent<UIDocument>();
var safeArea = Screen.safeArea;
var root = m_Document.rootVisualElement;
root.style.position = Position.Absolute;
root.style.left = safeArea.xMin;
root.style.right = Screen.width - safeArea.xMax;
root.style.top = Screen.height - safeArea.yMax;
root.style.bottom = safeArea.yMin;
}
private void Start()
{
var joystickMove = m_Document.rootVisualElement.Q<VisualElement>("JoystickMove");
var joystickLook = m_Document.rootVisualElement.Q<VisualElement>("JoystickLook");
m_MoveJoystick = new VirtualJoystick(joystickMove);
m_MoveJoystick.JoystickEvent.AddListener(mov =>
{
MoveEvent.Invoke(mov * MoveMagnitudeMultiplier);
});;
m_LookJoystick = new VirtualJoystick(joystickLook);
m_LookJoystick.JoystickEvent.AddListener(mov =>
{
if (InvertLookY)
mov.y *= -1;
LookEvent.Invoke(mov * LookMagnitudeMultiplier);
});
var jumpButton = m_Document.rootVisualElement.Q<VisualElement>("ButtonJump");
jumpButton.RegisterCallback<PointerEnterEvent>(evt => { JumpEvent.Invoke(true); });
jumpButton.RegisterCallback<PointerLeaveEvent>(evt => { JumpEvent.Invoke(false); });
var sprintButton = m_Document.rootVisualElement.Q<VisualElement>("ButtonSprint");
sprintButton.RegisterCallback<PointerEnterEvent>(evt => { SprintEvent.Invoke(true); });
sprintButton.RegisterCallback<PointerLeaveEvent>(evt => { SprintEvent.Invoke(false); });
}
}
public class VirtualJoystick
{
public VisualElement BaseElement;
public VisualElement Thumbstick;
public UnityEvent<Vector2> JoystickEvent = new();
public VirtualJoystick(VisualElement root)
{
BaseElement = root;
Thumbstick = root.Q<VisualElement>("JoystickHandle");
BaseElement.RegisterCallback<PointerDownEvent>(HandlePress);
BaseElement.RegisterCallback<PointerMoveEvent>(HandleDrag);
BaseElement.RegisterCallback<PointerUpEvent>(HandleRelease);
}
void HandlePress(PointerDownEvent evt)
{
BaseElement.CapturePointer(evt.pointerId);
}
void HandleRelease(PointerUpEvent evt)
{
BaseElement.ReleasePointer(evt.pointerId);
Thumbstick.style.left = Length.Percent(50);
Thumbstick.style.top = Length.Percent(50);
JoystickEvent.Invoke(Vector2.zero);
}
void HandleDrag(PointerMoveEvent evt)
{
if (!BaseElement.HasPointerCapture(evt.pointerId)) return;
var width = BaseElement.contentRect.width;
var center = new Vector3(width / 2, width / 2);
var centerToPosition = evt.localPosition - center;
if (centerToPosition.magnitude > width/2)
{
centerToPosition = centerToPosition.normalized * width / 2;
}
var newPos = center + centerToPosition;
Thumbstick.style.left = newPos.x;
Thumbstick.style.top = newPos.y;
centerToPosition /= (width / 2);
//we invert y as the y of UI goes down, but pushing the joystick up is expected to give a positive y value
centerToPosition.y *= -1;
JoystickEvent.Invoke(centerToPosition);
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 6d7e4e4868ed5df4b928c22423c690a0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 267961
packageName: 'Starter Assets: Character Controllers | URP'
packageVersion: 2.0.2
assetPath: Assets/Starter Assets/Runtime/Mobile/Scripts/TouchscreenInput.cs
uploadId: 721456

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a499c73a35b488449a6f9e9db9e88120
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,41 @@
/*
The PlayerInput component has an auto-switch control scheme action that allows automatic changing of connected devices.
IE: Switching from Keyboard to Gamepad in-game.
When built to a mobile phone; in most cases, there is no concept of switching connected devices as controls are typically driven through what is on the device's hardware (Screen, Tilt, etc)
In Input System 1.0.2, if the PlayerInput component has Auto Switch enabled, it will search the mobile device for connected devices; which is very costly and results in bad performance.
This is fixed in Input System 1.1.
For the time-being; this script will disable a PlayerInput's auto switch control schemes; when project is built to mobile.
*/
using System;
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
public class MobileDisableAutoSwitchControls : MonoBehaviour
{
#if ENABLE_INPUT_SYSTEM && (UNITY_IOS || UNITY_ANDROID)
[Header("Target")]
public PlayerInput playerInput;
void Start()
{
DisableAutoSwitchControls();
}
void DisableAutoSwitchControls()
{
playerInput.neverAutoSwitchControlSchemes = true;
}
private void Update()
{
Debug.Log(playerInput.currentControlScheme);
}
#endif
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 901182334643ba1438a25accc6bd0c79
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 267961
packageName: 'Starter Assets: Character Controllers | URP'
packageVersion: 2.0.2
assetPath: Assets/Starter Assets/Runtime/Mobile/Scripts/Utilities/MobileDisableAutoSwitchControls.cs
uploadId: 721456

View File

@@ -0,0 +1,28 @@
using StarterAssets;
using UnityEngine;
public class VirtualInput : MonoBehaviour
{
[Header("Output")]
public StarterAssetsInputs StarterAssetsInputs;
public void VirtualMoveInput(Vector2 virtualMoveDirection)
{
StarterAssetsInputs.MoveInput(virtualMoveDirection);
}
public void VirtualLookInput(Vector2 virtualLookDirection)
{
StarterAssetsInputs.LookInput(virtualLookDirection);
}
public void VirtualJumpInput(bool virtualJumpState)
{
StarterAssetsInputs.JumpInput(virtualJumpState);
}
public void VirtualSprintInput(bool virtualSprintState)
{
StarterAssetsInputs.SprintInput(virtualSprintState);
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: ed06e3b6fec5ecc4696004cedc31f0ac
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 267961
packageName: 'Starter Assets: Character Controllers | URP'
packageVersion: 2.0.2
assetPath: Assets/Starter Assets/Runtime/Mobile/Scripts/VirtualInput.cs
uploadId: 721456