新增对 InputSystem 的支持

Update BaseGraph.cs

Update XCharts.Runtime.asmdef
This commit is contained in:
边上海
2023-01-29 19:43:05 +08:00
parent e90d7d61d9
commit 7fbaf7af70
14 changed files with 444 additions and 288 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

View File

@@ -0,0 +1,17 @@
---
title: 入门教程:从 Input Manager 转到 Input System
sidebar_position: 1
slug: /inputsystem
---
# 教程:从 Input Manager 转到 Input System
## 1. 按图示修改项目配置中输入模式为 Input System
![Project Settings](img/inputsystem02.png)
## 2. 使用 Unity Package Manager 安装 Input System
![UPM](img/inputsystem01.png)
## 3. 选中场景中 EventSystem 游戏对象,更换输入模组
![Input Module](img/inputsystem03.png)

View File

@@ -70,6 +70,7 @@
- 支持万级大数据量绘制,支持采样绘制。
- 支持`TexMeshPro`
- 支持所有`5.6`以上的`Unity`版本。
- 支持 Input System [如何从 Input Manager 转 Input System](Documentation~/zh/inputsystem.md))。
## 截图

View File

@@ -631,12 +631,12 @@ namespace XCharts.Runtime
internal void UpdateStartLabelPosition(Vector3 pos)
{
m_StartLabel.SetPosition(pos);
if (m_StartLabel != null) m_StartLabel.SetPosition(pos);
}
internal void UpdateEndLabelPosition(Vector3 pos)
{
m_EndLabel.SetPosition(pos);
if (m_EndLabel != null) m_EndLabel.SetPosition(pos);
}
public void UpdateRuntimeData(float chartX, float chartY, float chartWidth, float chartHeight)

View File

@@ -1,8 +1,10 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using XUGL;
#if INPUT_SYSTEM_ENABLED
using Input = XCharts.Runtime.InputHelper;
#endif
namespace XCharts.Runtime
{

View File

@@ -3,7 +3,9 @@ using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using XUGL;
#if INPUT_SYSTEM_ENABLED
using Input = XCharts.Runtime.InputHelper;
#endif
namespace XCharts.Runtime
{
[UnityEngine.Scripting.Preserve]

View File

@@ -253,6 +253,14 @@ namespace XCharts.Runtime
else
return component;
}
public T EnsureChartComponent<T>() where T : MainComponent
{
var component = GetChartComponent<T>();
if (component == null)
return AddChartComponent<T>();
else
return component;
}
public bool TryGetChartComponent<T>(out T component, int index = 0)
where T : MainComponent

View File

@@ -11,7 +11,7 @@ namespace XCharts.Runtime
{
[AddComponentMenu("XCharts/EmptyChart", 10)]
[ExecuteInEditMode]
[RequireComponent(typeof(RectTransform))]
[RequireComponent(typeof(RectTransform),typeof(CanvasRenderer))]
[DisallowMultipleComponent]
public partial class BaseChart : BaseGraph, ISerializationCallbackReceiver
{

View File

@@ -2,6 +2,9 @@ using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
#if INPUT_SYSTEM_ENABLED
using Input = XCharts.Runtime.InputHelper;
#endif
namespace XCharts.Runtime
{
@@ -206,8 +209,9 @@ namespace XCharts.Runtime
{
if (!isPointerInChart) return;
if (canvas == null) return;
Vector2 mousePos = Input.mousePosition;
Vector2 local;
if (!ScreenPointToChartPoint(Input.mousePosition, out local))
if (!ScreenPointToChartPoint(mousePos, out local))
{
pointerPos = Vector2.zero;
}

View File

@@ -0,0 +1,111 @@
#if INPUT_SYSTEM_ENABLED
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.LowLevel;
namespace XCharts.Runtime
{
public class InputHelper
{
public static Vector2 mousePosition
{
get
{
var value = Vector2.zero;
if (null != Mouse.current)
{
value = Mouse.current.position.ReadValue();
}
else if (null != Touchscreen.current && Touchscreen.current.touches.Count > 0)
{
value = Touchscreen.current.touches[0].position.ReadValue();
}
return value;
}
}
public static int touchCount
{
get
{
var value = 0;
if (null != Touchscreen.current)
{
value = Touchscreen.current.touches.Count;
}
return value;
}
}
public static Touch GetTouch(int v)
{
UnityEngine.TouchPhase PhaseConvert(TouchState state)
{
UnityEngine.TouchPhase temp = UnityEngine.TouchPhase.Began;
switch (state.phase)
{
case UnityEngine.InputSystem.TouchPhase.Began:
temp = UnityEngine.TouchPhase.Began;
break;
case UnityEngine.InputSystem.TouchPhase.Moved:
temp = UnityEngine.TouchPhase.Moved;
break;
case UnityEngine.InputSystem.TouchPhase.Canceled:
temp = UnityEngine.TouchPhase.Canceled;
break;
case UnityEngine.InputSystem.TouchPhase.Stationary:
temp = UnityEngine.TouchPhase.Stationary;
break;
default:
case UnityEngine.InputSystem.TouchPhase.Ended:
case UnityEngine.InputSystem.TouchPhase.None:
temp = UnityEngine.TouchPhase.Ended;
break;
}
return temp;
}
var touch = Touchscreen.current.touches[v];
var value = touch.ReadValue();
//copy touchcontrol's touchstate data into touch
return new Touch
{
deltaPosition = value.delta,
fingerId = value.touchId,
position = value.position,
phase = PhaseConvert(value),
pressure = value.pressure,
radius = value.radius.magnitude,
radiusVariance = value.radius.sqrMagnitude,
type = value.isPrimaryTouch ? TouchType.Direct : TouchType.Indirect,
tapCount = value.tapCount,
deltaTime = Time.realtimeSinceStartup - (float)value.startTime,
rawPosition = value.startPosition,
};
}
public static bool GetKeyDown(KeyCode keyCode)
{
var value = false;
if (null != Keyboard.current)
{
var key = Keyboard.current.spaceKey;
switch (keyCode)
{
case KeyCode.Space:
key = Keyboard.current.spaceKey;
break;
case KeyCode.L:
key = Keyboard.current.lKey;
break;
default:
Debug.LogError($"{nameof(InputHelper)}: not support {keyCode} yet , please add it yourself if needed");
break;
}
value = key.wasPressedThisFrame;
}
return value;
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5069defa9fe8c7a43843e1189e2d606c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,13 +1,13 @@
{
"name": "XCharts.Runtime",
"references": [
],
"optionalUnityReferences": [],
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": []
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}