2026-04-27 BGM 및 이스터에그

This commit is contained in:
2026-04-27 17:47:44 +09:00
parent 88a71e6292
commit 18d3077cc4
840 changed files with 53720 additions and 4 deletions

View File

@@ -0,0 +1,263 @@
using UnityEngine;
using UnityEngine.Events;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem; //if you are getting error here, please make sure the input system package is installed.
#else
using System.Collections.Generic;
#endif
namespace TinyGiantStudio.Text
{
[DisallowMultipleComponent]
public class ButtonInputProcessor : MonoBehaviour
{
/// <summary>
/// How long you have to press a key down for it to register as a second key press
/// </summary>
[Tooltip("How long you have to press a key down for it to register as a second key press")]
public float tickRate = 0.5f;
public float tickRateSideWays = 0.5f;
//TODO: convert to c# events
public UnityEvent upAxisEvent;
public UnityEvent downAxisEvent;
public UnityEvent leftAxisEvent;
public UnityEvent rightAxisEvent;
public UnityEvent submitEvent;
private float lastPressedUp;
private float lastPressedDown;
private float lastPressedLeft;
private float lastPressedRight;
private float lastPressedSubmit;
private Vector2 axisInput;
#if ENABLE_INPUT_SYSTEM
#else
private float axisSensitivity = 0.1f;
private string verticalAxisString = "Vertical";
private string horizontalAxisString = "Horizontal";
private string submitString = "Submit";
[HideInInspector]
public List<StandardInput> inputs = new List<StandardInput>();
[System.Serializable]
public class StandardInput
{
public KeyCode key;
public UnityEvent unityEvent;
[HideInInspector]
public float lastPressed;
}
#endif
private void Update()
{
ProcessAxisInput();
}
#if ENABLE_INPUT_SYSTEM
void ProcessAxisInput()
{
if (axisInput.y > 0)
AttemptUp();
else if (axisInput.y < 0)
AttemptDown();
else
{
lastPressedDown = 0;
lastPressedUp = 0;
}
if (axisInput.x > 0)
AttemptRight();
else if (axisInput.x < 0)
AttemptLeft();
else
{
lastPressedLeft = 0;
lastPressedRight = 0;
}
}
#else
private void ProcessAxisInput()
{
//If you are having error here, please check if you changed the string for Horizontal axis and change the value of horizontalAxisString accordingly
if (Input.GetAxis(horizontalAxisString) < -axisSensitivity)
AttemptLeft();
else
lastPressedLeft = 0;
//If you are having error here, please check if you changed the string for Horizontal axis and change the value of horizontalAxisString accordingly
if (Input.GetAxis(horizontalAxisString) > axisSensitivity)
AttemptRight();
else
lastPressedRight = 0;
//If you are having error here, please check if you changed the string for Vertical axis and change the value of verticalAxisString accordingly
if (Input.GetAxis(verticalAxisString) > axisSensitivity)
AttemptUp();
else
lastPressedUp = 0;
//If you are having error here, please check if you changed the string for Vertical axis and change the value of verticalAxisString accordingly
if (Input.GetAxis(verticalAxisString) < -axisSensitivity)
AttemptDown();
else
lastPressedDown = 0;
if (Input.GetButton(submitString))
{
if (tickRate > 0)
{
if (lastPressedSubmit + tickRate < Time.time)
{
lastPressedSubmit = Time.time;
submitEvent.Invoke();
}
}
else
{
submitEvent.Invoke();
}
}
else
lastPressedSubmit = 0;
}
#endif
#region Take new input
#if ENABLE_INPUT_SYSTEM
public void OnNavigate(InputAction.CallbackContext context)
{
axisInput = context.ReadValue<Vector2>();
if (axisInput.x == 0)
{
lastPressedLeft = 0;
lastPressedRight = 0;
}
else if (axisInput.y == 0)
{
lastPressedUp = 0;
lastPressedDown = 0;
}
}
/// <summary>
/// Used by Player Input script, made by Unity
/// </summary>
/// <param name="value"></param>
void OnNavigate(InputValue value)
{
axisInput = value.Get<Vector2>();
}
public void OnSubmit(InputAction.CallbackContext context)
{
if (!gameObject.activeInHierarchy)
return;
submitEvent.Invoke();
}
/// <summary>
/// Used by Player Input script, made by Unity
/// </summary>
/// <param name="value"></param>
void OnSubmit(InputValue value)
{
if (!gameObject.activeInHierarchy)
return;
if (!value.isPressed) //what does this do?
return;
submitEvent.Invoke();
}
#endif
#endregion Take new input
#region Process Input
private void AttemptUp()
{
if (tickRate > 0)
{
if (lastPressedUp + tickRate < Time.time)
{
lastPressedUp = Time.time;
if (upAxisEvent != null)
upAxisEvent.Invoke();
//else
// Debug.Log(upAxisEvent);
}
}
else
{
upAxisEvent.Invoke();
}
}
private void AttemptDown()
{
if (tickRate > 0)
{
if (lastPressedDown + tickRate < Time.time)
{
lastPressedDown = Time.time;
downAxisEvent.Invoke();
}
}
else
{
downAxisEvent.Invoke();
}
}
private void AttemptLeft()
{
if (tickRate > 0)
{
if (lastPressedLeft + tickRateSideWays < Time.time)
{
lastPressedLeft = Time.time;
leftAxisEvent.Invoke();
}
}
else
{
leftAxisEvent.Invoke();
}
}
private void AttemptRight()
{
if (tickRate > 0)
{
if (lastPressedRight + tickRateSideWays < Time.time)
{
lastPressedRight = Time.time;
rightAxisEvent.Invoke();
}
}
else
{
rightAxisEvent.Invoke();
}
}
#endregion Process Input
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 9a2c76f58cbcee94ba836cc95788869a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 08799c2d4ac203d4abfea66f06ee0b33, type: 3}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 247241
packageName: Modular 3D Text - In-Game 3D UI System
packageVersion: 4.9.2
assetPath: Assets/Plugins/Tiny Giant Studio/Modular 3D Text/Scripts/Input/ButtonInputProcessor.cs
uploadId: 877966

View File

@@ -0,0 +1,274 @@
using UnityEngine;
using UnityEngine.Events;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Events;
#endif
namespace TinyGiantStudio.Text
{
[AddComponentMenu("Tiny Giant Studio/Modular 3D Text/Input System/Global Button Input System", order: 20050)]
[HelpURL("https://ferdowsur.gitbook.io/modular-3d-text/input/button-key/button-input-system")]
public class ButtonInputSystemGlobal : MonoBehaviour
{
#region Variable declaration
public static ButtonInputSystemGlobal Instance;
[SerializeField] private ButtonInputProcessorStyle _buttonInputProcessorStyle = ButtonInputProcessorStyle.IndividualPlayerInputComponents;
public ButtonInputProcessorStyle MyButtonInputProcessorStyle
{
get { return _buttonInputProcessorStyle; }
set
{
if (_buttonInputProcessorStyle != value)
{
#if UNITY_EDITOR
if (debugLogs)
Debug.Log(value + " being set");
#endif
_buttonInputProcessorStyle = value;
UpdateButtonInputProcessorScript();
}
}
}
#if ENABLE_INPUT_SYSTEM
public InputActionAsset inputActionAsset;
#endif
/// <summary>
/// This is assigned when MText_InputSystemController is enabled/disabled and when a list with autofocus is instantiated on void start()
/// </summary>
public ButtonInputSystemLocal selectedInputSystem;
public bool debugLogs = false;
#endregion Variable declaration
void Awake()
{
if (Instance)
Debug.LogWarning("Multiple MText_UI_ButtonInputProcessor script found on scene.");
Instance = this;
UpdateButtonInputProcessorScript();
#if ENABLE_INPUT_SYSTEM
if (MyButtonInputProcessorStyle != ButtonInputProcessorStyle.CommonInputController)
return;
if (inputActionAsset == null)
{
ImportantLog("No input action asset selected on ");
return;
}
ButtonInputProcessor inputProcessor = gameObject.GetComponent<ButtonInputProcessor>();
var map = inputActionAsset.FindActionMap("UI");
map.Enable();
map.FindAction("Navigate").performed += inputProcessor.OnNavigate;
map.FindAction("Submit").performed += inputProcessor.OnSubmit;
#endif
}
public void Select(ButtonInputSystemLocal buttonInputSystem)
{
selectedInputSystem = buttonInputSystem;
}
public void DeSelect(ButtonInputSystemLocal buttonInputSystem)
{
if (selectedInputSystem == buttonInputSystem)
selectedInputSystem = null;
}
/// <summary>
/// Adds/removes and adds all required listeners to button input processor script.
///
/// Called in 3 cases.
/// In Awake()
/// MyButtonInputProcessorStyle modification
/// By editor script that doesn't do setup instantly and calls it later because of add component delay
/// </summary>
/// <param name="alsoDoSetup"></param>
public void UpdateButtonInputProcessorScript(bool alsoDoSetup = true)
{
if ((MyButtonInputProcessorStyle == ButtonInputProcessorStyle.CommonInputController))
{
if (!gameObject.GetComponent<ButtonInputProcessor>())
gameObject.AddComponent<ButtonInputProcessor>();
if (alsoDoSetup)
SetupInputProcessor();
}
else
{
if (gameObject.GetComponent<ButtonInputProcessor>())
{
if (Application.isPlaying)
Destroy(gameObject.GetComponent<ButtonInputProcessor>());
else
DestroyImmediate(gameObject.GetComponent<ButtonInputProcessor>());
}
}
}
/// <summary>
/// Attaches required listeners to button input processor
/// </summary>
public void SetupInputProcessor()
{
ButtonInputProcessor buttonInputProcessor = gameObject.GetComponent<ButtonInputProcessor>();
if (buttonInputProcessor == null)
return;
buttonInputProcessor.upAxisEvent ??= new UnityEvent();
buttonInputProcessor.downAxisEvent ??= new UnityEvent();
buttonInputProcessor.leftAxisEvent ??= new UnityEvent();
buttonInputProcessor.rightAxisEvent ??= new UnityEvent();
buttonInputProcessor.submitEvent ??= new UnityEvent();
#if UNITY_EDITOR
if (!Application.isPlaying)
{
if (!CheckIfContains(buttonInputProcessor.upAxisEvent, this, "UpAxisEvent"))
UnityEventTools.AddPersistentListener(buttonInputProcessor.upAxisEvent, UpAxisEvent);
if (!CheckIfContains(buttonInputProcessor.downAxisEvent, this, "DownAxisEvent"))
UnityEventTools.AddPersistentListener(buttonInputProcessor.downAxisEvent, DownAxisEvent);
if (!CheckIfContains(buttonInputProcessor.leftAxisEvent, this, "LeftAxisEvent"))
UnityEventTools.AddPersistentListener(buttonInputProcessor.leftAxisEvent, LeftAxisEvent);
if (!CheckIfContains(buttonInputProcessor.rightAxisEvent, this, "RightAxisEvent"))
UnityEventTools.AddPersistentListener(buttonInputProcessor.rightAxisEvent, RightAxisEvent);
if (!CheckIfContains(buttonInputProcessor.submitEvent, this, "SubmitEvent"))
UnityEventTools.AddPersistentListener(buttonInputProcessor.submitEvent, SubmitEvent);
EditorUtility.SetDirty(buttonInputProcessor);
}
else
{
if (!CheckIfContains(buttonInputProcessor.upAxisEvent, this, "UpAxisEvent"))
buttonInputProcessor.upAxisEvent.AddListener(UpAxisEvent);
if (!CheckIfContains(buttonInputProcessor.downAxisEvent, this, "DownAxisEvent"))
buttonInputProcessor.downAxisEvent.AddListener(DownAxisEvent);
if (!CheckIfContains(buttonInputProcessor.leftAxisEvent, this, "LeftAxisEvent"))
buttonInputProcessor.leftAxisEvent.AddListener(LeftAxisEvent);
if (!CheckIfContains(buttonInputProcessor.rightAxisEvent, this, "RightAxisEvent"))
buttonInputProcessor.rightAxisEvent.AddListener(RightAxisEvent);
if (!CheckIfContains(buttonInputProcessor.submitEvent, this, "SubmitEvent"))
buttonInputProcessor.submitEvent.AddListener(SubmitEvent);
}
#else
if (!CheckIfContains(buttonInputProcessor.upAxisEvent, this, "UpAxisEvent"))
buttonInputProcessor.upAxisEvent.AddListener(UpAxisEvent);
if (!CheckIfContains(buttonInputProcessor.downAxisEvent, this, "DownAxisEvent"))
buttonInputProcessor.downAxisEvent.AddListener(DownAxisEvent);
if (!CheckIfContains(buttonInputProcessor.leftAxisEvent, this, "LeftAxisEvent"))
buttonInputProcessor.leftAxisEvent.AddListener(LeftAxisEvent);
if (!CheckIfContains(buttonInputProcessor.rightAxisEvent, this, "RightAxisEvent"))
buttonInputProcessor.rightAxisEvent.AddListener(RightAxisEvent);
if (!CheckIfContains(buttonInputProcessor.submitEvent, this, "SubmitEvent"))
buttonInputProcessor.submitEvent.AddListener(SubmitEvent);
#endif
}
bool CheckIfContains(UnityEvent myEvent, object target, string targetMethodName)
{
myEvent ??= new UnityEvent(); //double check because I am paranoid
for (int i = 0; i < myEvent.GetPersistentEventCount(); i++)
{
if (myEvent.GetPersistentTarget(i) == (object)target)
{
if (myEvent.GetPersistentMethodName(i) == targetMethodName)
{
if (debugLogs)
Debug.Log("Already contains " + targetMethodName);
return true;
}
}
}
return false;
}
public void UpAxisEvent()
{
if (IsThereAppropriateTarget())
selectedInputSystem.upAxisEvent.Invoke();
}
public void DownAxisEvent()
{
if (IsThereAppropriateTarget())
selectedInputSystem.downAxisEvent.Invoke();
}
public void LeftAxisEvent()
{
if (IsThereAppropriateTarget())
selectedInputSystem.leftAxisEvent.Invoke();
}
public void RightAxisEvent()
{
if (IsThereAppropriateTarget())
selectedInputSystem.rightAxisEvent.Invoke();
}
public void SubmitEvent()
{
if (IsThereAppropriateTarget())
selectedInputSystem.submitEvent.Invoke();
}
bool IsThereAppropriateTarget()
{
if (selectedInputSystem == null)
return false;
if (!selectedInputSystem.gameObject.activeInHierarchy)
return false; //if the list is disabled in hierarchy, not an appropriate target
return true;
}
void ImportantLog(string msg)
{
#if UNITY_EDITOR
Debug.Log(msg, gameObject);
#else
if(debugLogs)
Debug.Log(msg, gameObject);
#endif
}
public enum ButtonInputProcessorStyle
{
IndividualPlayerInputComponents,
CommonInputController,
Custom
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 31cbbd8dc7af5874b8cbd53743f8f7ee
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 08799c2d4ac203d4abfea66f06ee0b33, type: 3}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 247241
packageName: Modular 3D Text - In-Game 3D UI System
packageVersion: 4.9.2
assetPath: Assets/Plugins/Tiny Giant Studio/Modular 3D Text/Scripts/Input/ButtonInputSystemGlobal.cs
uploadId: 877966

View File

@@ -0,0 +1,234 @@
using UnityEngine;
using UnityEngine.Events;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
#if UNITY_EDITOR
using UnityEditor.Events;
#endif
namespace TinyGiantStudio.Text
{
[DisallowMultipleComponent]
[AddComponentMenu("Tiny Giant Studio/Modular 3D Text/Input System/Local Button Input System", order: 20051)]
[HelpURL("https://ferdowsur.gitbook.io/modular-3d-text/input/button-key/button-input-system")]
public class ButtonInputSystemLocal : MonoBehaviour
{
public UnityEvent upAxisEvent;
public UnityEvent downAxisEvent;
public UnityEvent leftAxisEvent;
public UnityEvent rightAxisEvent;
public UnityEvent submitEvent;
private ButtonInputSystemGlobal buttonInputSystemGlobal;
private bool useCommonControls;
//note to self: is called after on enable
private void Start()
{
if (buttonInputSystemGlobal == null) //if this GameObject pre-exists in scene, it may call OnEnable before the global instance has been created.
{
GetCommonControlSettings();
SelectThis();
}
UpdateButtonInputProcessorScript();
}
private void OnEnable()
{
GetCommonControlSettings();
SelectThis();
}
private void OnDisable()
{
if (buttonInputSystemGlobal == null)
return;
buttonInputSystemGlobal.DeSelect(this);
}
private void GetCommonControlSettings()
{
if (buttonInputSystemGlobal == null)
buttonInputSystemGlobal = ButtonInputSystemGlobal.Instance;
if (buttonInputSystemGlobal == null)
return;
if (buttonInputSystemGlobal.MyButtonInputProcessorStyle == ButtonInputSystemGlobal.ButtonInputProcessorStyle.CommonInputController)
useCommonControls = true;
else
useCommonControls = false;
}
private void SelectThis()
{
if (buttonInputSystemGlobal == null)
return;
buttonInputSystemGlobal.Select(this);
}
private void UpdateButtonInputProcessorScript()
{
if (useCommonControls)
{
#if ENABLE_INPUT_SYSTEM
if (GetComponent<PlayerInput>())
{
if (Application.isPlaying)
Destroy(gameObject.GetComponent<PlayerInput>());
else
DestroyImmediate(gameObject.GetComponent<PlayerInput>());
}
#endif
if (gameObject.GetComponent<ButtonInputProcessor>())
{
if (Application.isPlaying)
Destroy(gameObject.GetComponent<ButtonInputProcessor>());
else
DestroyImmediate(gameObject.GetComponent<ButtonInputProcessor>());
}
}
else
{
if (!gameObject.GetComponent<ButtonInputProcessor>())
{
gameObject.AddComponent<ButtonInputProcessor>();
SetupInputProcessor();
}
}
}
/// <summary>
/// Attaches required listeners to button input processor
/// </summary>
public void SetupInputProcessor()
{
ButtonInputProcessor buttonInputProcessor = gameObject.GetComponent<ButtonInputProcessor>();
if (buttonInputProcessor == null)
return;
buttonInputProcessor.upAxisEvent ??= new UnityEvent();
buttonInputProcessor.downAxisEvent ??= new UnityEvent();
buttonInputProcessor.leftAxisEvent ??= new UnityEvent();
buttonInputProcessor.rightAxisEvent ??= new UnityEvent();
buttonInputProcessor.submitEvent ??= new UnityEvent();
#if UNITY_EDITOR
if (!Application.isPlaying)
{
if (!CheckIfContains(buttonInputProcessor.upAxisEvent, this, "UpAxisEvent"))
UnityEventTools.AddPersistentListener(buttonInputProcessor.upAxisEvent, UpAxisEvent);
if (!CheckIfContains(buttonInputProcessor.downAxisEvent, this, "DownAxisEvent"))
UnityEventTools.AddPersistentListener(buttonInputProcessor.downAxisEvent, DownAxisEvent);
if (!CheckIfContains(buttonInputProcessor.leftAxisEvent, this, "LeftAxisEvent"))
UnityEventTools.AddPersistentListener(buttonInputProcessor.leftAxisEvent, LeftAxisEvent);
if (!CheckIfContains(buttonInputProcessor.rightAxisEvent, this, "RightAxisEvent"))
UnityEventTools.AddPersistentListener(buttonInputProcessor.rightAxisEvent, RightAxisEvent);
if (!CheckIfContains(buttonInputProcessor.submitEvent, this, "SubmitEvent"))
UnityEventTools.AddPersistentListener(buttonInputProcessor.submitEvent, SubmitEvent);
UnityEditor.EditorUtility.SetDirty(buttonInputProcessor);
}
else
{
if (!CheckIfContains(buttonInputProcessor.upAxisEvent, this, "UpAxisEvent"))
buttonInputProcessor.upAxisEvent.AddListener(UpAxisEvent);
if (!CheckIfContains(buttonInputProcessor.downAxisEvent, this, "DownAxisEvent"))
buttonInputProcessor.downAxisEvent.AddListener(DownAxisEvent);
if (!CheckIfContains(buttonInputProcessor.leftAxisEvent, this, "LeftAxisEvent"))
buttonInputProcessor.leftAxisEvent.AddListener(LeftAxisEvent);
if (!CheckIfContains(buttonInputProcessor.rightAxisEvent, this, "RightAxisEvent"))
buttonInputProcessor.rightAxisEvent.AddListener(RightAxisEvent);
if (!CheckIfContains(buttonInputProcessor.submitEvent, this, "SubmitEvent"))
buttonInputProcessor.submitEvent.AddListener(SubmitEvent);
}
#else
if (!CheckIfContains(buttonInputProcessor.upAxisEvent, this, "UpAxisEvent"))
buttonInputProcessor.upAxisEvent.AddListener(UpAxisEvent);
if (!CheckIfContains(buttonInputProcessor.downAxisEvent, this, "DownAxisEvent"))
buttonInputProcessor.downAxisEvent.AddListener(DownAxisEvent);
if (!CheckIfContains(buttonInputProcessor.leftAxisEvent, this, "LeftAxisEvent"))
buttonInputProcessor.leftAxisEvent.AddListener(LeftAxisEvent);
if (!CheckIfContains(buttonInputProcessor.rightAxisEvent, this, "RightAxisEvent"))
buttonInputProcessor.rightAxisEvent.AddListener(RightAxisEvent);
if (!CheckIfContains(buttonInputProcessor.submitEvent, this, "SubmitEvent"))
buttonInputProcessor.submitEvent.AddListener(SubmitEvent);
#endif
}
private bool CheckIfContains(UnityEvent myEvent, object target, string targetMethodName)
{
myEvent ??= new UnityEvent(); //double check because I am paranoid
for (int i = 0; i < myEvent.GetPersistentEventCount(); i++)
{
if (myEvent.GetPersistentTarget(i) == (object)target)
{
if (myEvent.GetPersistentMethodName(i) == targetMethodName)
{
return true;
}
}
}
return false;
}
public void UpAxisEvent()
{
if (IsThereAppropriateTarget())
upAxisEvent.Invoke();
}
public void DownAxisEvent()
{
if (IsThereAppropriateTarget())
downAxisEvent.Invoke();
}
public void LeftAxisEvent()
{
if (IsThereAppropriateTarget())
leftAxisEvent.Invoke();
}
public void RightAxisEvent()
{
if (IsThereAppropriateTarget())
rightAxisEvent.Invoke();
}
public void SubmitEvent()
{
if (IsThereAppropriateTarget())
submitEvent.Invoke();
}
private bool IsThereAppropriateTarget()
{
return (gameObject.activeInHierarchy);
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: bd7d51d0bd890f247a44eaa6f9e72876
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 08799c2d4ac203d4abfea66f06ee0b33, type: 3}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 247241
packageName: Modular 3D Text - In-Game 3D UI System
packageVersion: 4.9.2
assetPath: Assets/Plugins/Tiny Giant Studio/Modular 3D Text/Scripts/Input/ButtonInputSystemLocal.cs
uploadId: 877966

View File

@@ -0,0 +1,201 @@
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.EnhancedTouch;
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
#endif
namespace TinyGiantStudio.Text
{
/// <summary>
/// Handles input for raycast selector
/// </summary>
[AddComponentMenu("Tiny Giant Studio/Modular 3D Text/Input System/Raycast Input Processor", order: 20052)]
[HelpURL("https://ferdowsur.gitbook.io/modular-3d-text/input/mouse-touch/raycast-input-processor")]
[DisallowMultipleComponent]
[RequireComponent(typeof(RaycastSelector))]
public class RaycastInputProcessor : MonoBehaviour
{
#region Raycast settings
[Tooltip("If not assigned, it will automatically get Camera.main on Start")]
public Camera myCamera;
#endregion Raycast settings
public Transform pointerOnUI;
public Transform currentTarget;
bool dragging = false;
RaycastSelector raycastSelector;
#region Unity Things
void Awake()
{
raycastSelector = GetComponent<RaycastSelector>();
#if ENABLE_INPUT_SYSTEM
EnhancedTouchSupport.Enable();
#endif
}
void Start()
{
//If no camera assigned, get Camera.main
if (!myCamera)
{
myCamera = Camera.main;
if (!myCamera)
Debug.Log("No camera selected for 3D UI Raycaster");
}
}
void Update()
{
if (!myCamera)
return;
//If Already dragging stuff, do dragging stuff
if (dragging)
{
Dragging();
DetectDragEnd();
}
else
{
SelectPress();
}
}
/// <summary>
/// Select or press
/// </summary>
void SelectPress()
{
//Check if mouse is on something
pointerOnUI = RaycastCheck();
//If mouse not on the old UI, unselect old one
if (pointerOnUI != currentTarget)
raycastSelector.UnselectTarget(currentTarget);
//If mouse on a UI
if (pointerOnUI)
{
//If it's a new target, select that
if (pointerOnUI != currentTarget)
raycastSelector.SelectTarget(pointerOnUI);
//If the UI is clicked
if (PressedButton())
{
raycastSelector.PressTarget(pointerOnUI);
dragging = true;
}
}
currentTarget = pointerOnUI;
}
#endregion Unity things
void Dragging()
{
Vector3 screenPoint = myCamera.WorldToScreenPoint(currentTarget.position);
#if ENABLE_INPUT_SYSTEM
//Get the mouse position on screen
Vector3 cursorScreenPoint = new Vector3(Pointer.current.position.ReadValue().x, Pointer.current.position.ReadValue().y, screenPoint.z);
#else
//Get the mouse position on screen
Vector3 cursorScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
#endif
//Convert cursor position to world position
Vector3 cursorPosition = myCamera.ScreenToWorldPoint(cursorScreenPoint);
raycastSelector.Dragging(currentTarget, cursorPosition);
}
bool PressedButton()
{
#if ENABLE_INPUT_SYSTEM
if (MouseClicked() || Tapped())
return true;
return false;
#else
return Input.GetMouseButtonDown(0);
#endif
}
#if ENABLE_INPUT_SYSTEM
bool MouseClicked()
{
if (Mouse.current != null)
return Mouse.current.leftButton.wasPressedThisFrame;
return false;
}
bool Tapped()
{
if (Touch.activeTouches.Count > 0)
return Touch.activeTouches[0].ended;
return false;
}
#endif
Transform RaycastCheck()
{
#if ENABLE_INPUT_SYSTEM
Ray ray = myCamera.ScreenPointToRay(Pointer.current.position.ReadValue());
#else
Ray ray = myCamera.ScreenPointToRay(Input.mousePosition);
#endif
return raycastSelector.RaycastCheck(ray);
}
void DetectDragEnd()
{
if (MouseButtonReleased() && dragging)
{
dragging = false;
raycastSelector.DragEnded(currentTarget, RaycastCheck());
}
if (!Input.touchSupported)
return;
if (Input.touchCount > 0)
{
#if ENABLE_INPUT_SYSTEM
if (Input.touches[0].phase == UnityEngine.TouchPhase.Ended)
#else
if (Input.touches[0].phase == TouchPhase.Ended)
#endif
{
dragging = false;
raycastSelector.DragEnded(currentTarget, RaycastCheck());
}
}
else
{
dragging = false;
raycastSelector.DragEnded(currentTarget, RaycastCheck());
}
}
bool MouseButtonReleased()
{
#if ENABLE_INPUT_SYSTEM
if (Mouse.current != null)
return Mouse.current.leftButton.wasReleasedThisFrame;
return false;
#else
return Input.GetMouseButtonUp(0);
#endif
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 7a96c571f3b24244fb35c210e152f41f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 08799c2d4ac203d4abfea66f06ee0b33, type: 3}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 247241
packageName: Modular 3D Text - In-Game 3D UI System
packageVersion: 4.9.2
assetPath: Assets/Plugins/Tiny Giant Studio/Modular 3D Text/Scripts/Input/RaycastInputProcessor.cs
uploadId: 877966

View File

@@ -0,0 +1,271 @@
using UnityEngine;
namespace TinyGiantStudio.Text
{
/// <summary>
/// This component is used to cast a ray from camera to interact with 3D UI Elements. Handles the logic part only. Not the input part.
/// </summary>
[DisallowMultipleComponent]
[AddComponentMenu("Tiny Giant Studio/Modular 3D Text/Input System/Raycast Selector", order: 20052)]
[HelpURL("https://ferdowsur.gitbook.io/modular-3d-text/input/mouse-touch/raycast-selector")]
public class RaycastSelector : MonoBehaviour
{
#region Variable Declaration--------------------------
#region Raycast settings
[SerializeField] LayerMask UILayer = ~0;
[SerializeField] float maxRayDistance = 5000;
#endregion Raycast settings
#region Behavior Settings
[Tooltip("True = How normal UI works. It toggles if clicking a inputfield enables it " +
"and clicking somewhere else disables it")]
public bool onlyOneTargetFocusedAtOnce = true;
[Tooltip("Unhovering mouse from a Btn will unselect it")]
public bool unselectBtnOnUnhover = true;
#endregion Behavior Settings
Transform clickedTarget = null;
#endregion Variable Declaration--------------------------
/// <summary>
/// Recieves ray
/// </summary>
/// <param name="ray"></param>
/// <returns>What was hit</returns>
public Transform RaycastCheck(Ray ray)
{
if (Physics.Raycast(ray, out RaycastHit hit, maxRayDistance, UILayer))
return hit.transform;
return null;
}
public void PressTarget(Transform hit)
{
if (onlyOneTargetFocusedAtOnce)
UnFocusPreviouslySelectedItems(hit);
PressInputString(hit);
PressButton(hit);
PressSlider(hit);
}
void PressInputString(Transform hit)
{
InputField inputString = hit.gameObject.GetComponent<InputField>();
if (!InteractWithInputString(inputString))
return;
inputString.Select();
clickedTarget = hit;
}
void PressSlider(Transform hit)
{
SliderHandle sliderHandle = hit.gameObject.GetComponent<SliderHandle>();
if (!InteractWithSlider(sliderHandle))
return;
hit.gameObject.GetComponent<SliderHandle>().slider.ClickedVisual();
}
void PressButton(Transform hit)
{
Button button = hit.gameObject.GetComponent<Button>();
if (!InteractWithButton(button))
return;
button.PressButtonVisualUpdate();
}
void UnFocusPreviouslySelectedItems(Transform hit)
{
if (hit != clickedTarget)
{
if (clickedTarget)
{
if (clickedTarget.gameObject.GetComponent<InputField>())
{
if (clickedTarget.gameObject.GetComponent<InputField>().interactable)
{
clickedTarget.gameObject.GetComponent<InputField>().Focus(false);
}
}
}
}
}
/// <summary>
/// Selects the 3D UI passed as parameter
/// </summary>
/// <param name="target"></param>
public void SelectTarget(Transform target)
{
SelectButton(target);
SelectSlider(target);
}
void SelectSlider(Transform hit)
{
SliderHandle sliderHandle = hit.gameObject.GetComponent<SliderHandle>();
if (!InteractWithSlider(sliderHandle))
return;
sliderHandle.slider.SelectedVisual();
}
void SelectButton(Transform hit)
{
Button button = hit.gameObject.GetComponent<Button>();
if (!InteractWithButton(button))
return;
button.SelectButton();
}
#region Unselect
/// <summary>
///
/// </summary>
/// <param name="target"></param>
public void UnselectTarget(Transform target)
{
if (!target)
return;
UnselectButton(target);
UnselectSlider(target);
}
void UnselectSlider(Transform hit)
{
SliderHandle sliderHandle = hit.gameObject.GetComponent<SliderHandle>();
if (!InteractWithSlider(sliderHandle))
return;
sliderHandle.slider.UnSelectedVisual();
}
void UnselectButton(Transform hit)
{
Button button = hit.gameObject.GetComponent<Button>();
if (!InteractWithButton(button))
return;
if (unselectBtnOnUnhover)
{
List list = StaticMethods.GetParentList(button.transform);
if (!list)
button.UnselectButton();
else
list.UnselectEverythingDontChangeSelectedItemValue();
}
else
{
//button.UnselectButton();
}
}
#endregion Unselect
#region Drag
#region Dragging
public void Dragging(Transform hit, Vector3 cursorPosition)
{
DragSlider(hit, cursorPosition);
DragButton(hit);
}
void DragSlider(Transform hit, Vector3 cursorPosition)
{
SliderHandle sliderHandle = hit.gameObject.GetComponent<SliderHandle>();
if (!InteractWithSlider(sliderHandle))
return;
//cursorPosition in slider handle's local space
Vector3 localPosition = hit.parent.InverseTransformPoint(cursorPosition); //used to be hit.inverseTransformPoint
//Remove Y Z position from handle
localPosition = new Vector3(localPosition.x, 0, 0);
float size = sliderHandle.slider.backgroundSize;
localPosition.x = Mathf.Clamp(localPosition.x, -size / 2, size / 2);
hit.localPosition = localPosition;
sliderHandle.slider.GetCurrentValueFromHandle();
sliderHandle.slider.ValueChanged();
}
void DragButton(Transform hit)
{
Button button = hit.gameObject.GetComponent<Button>();
if (!InteractWithButton(button))
return;
button.ButtonBeingPressed();
}
#endregion Dragging
#region Drag End
public void DragEnded(Transform hit, Transform currentTarget)
{
DragEndOnSlider(hit);
DragEndOnButton(hit, currentTarget);
}
void DragEndOnSlider(Transform hit)
{
SliderHandle sliderHandle = hit.gameObject.GetComponent<SliderHandle>();
if (!InteractWithSlider(sliderHandle))
return;
sliderHandle.slider.ValueChangeEnded();
}
void DragEndOnButton(Transform hit, Transform currentTarget)
{
Button button = hit.gameObject.GetComponent<Button>();
if (!InteractWithButton(button))
return;
if (currentTarget != hit) button.isSelected = false;
button.PressCompleted();
}
#endregion Drag End
#endregion Drag
#region CanItIteractWithIt
bool InteractWithButton(Button button)
{
if (!button)
return false;
if (button.interactable && button.interactableByMouse)
return true;
return false;
}
bool InteractWithSlider(SliderHandle sliderHandle)
{
if (!sliderHandle)
return false;
if (sliderHandle.slider && sliderHandle.slider.interactable)
return true;
return false;
}
bool InteractWithInputString(InputField inputString)
{
if (!inputString)
return false;
if (inputString.interactable)
return true;
return false;
}
#endregion CanItIteractWithIt
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 03e22dd27ac765142b843df639f9c119
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 08799c2d4ac203d4abfea66f06ee0b33, type: 3}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 247241
packageName: Modular 3D Text - In-Game 3D UI System
packageVersion: 4.9.2
assetPath: Assets/Plugins/Tiny Giant Studio/Modular 3D Text/Scripts/Input/RaycastSelector.cs
uploadId: 877966