diff --git a/Documentation~/zh/img/inputsystem01.png b/Documentation~/zh/img/inputsystem01.png new file mode 100644 index 00000000..c2716e63 Binary files /dev/null and b/Documentation~/zh/img/inputsystem01.png differ diff --git a/Documentation~/zh/img/inputsystem02.png b/Documentation~/zh/img/inputsystem02.png new file mode 100644 index 00000000..5178a460 Binary files /dev/null and b/Documentation~/zh/img/inputsystem02.png differ diff --git a/Documentation~/zh/img/inputsystem03.png b/Documentation~/zh/img/inputsystem03.png new file mode 100644 index 00000000..95ad50a3 Binary files /dev/null and b/Documentation~/zh/img/inputsystem03.png differ diff --git a/Documentation~/zh/inputsystem.md b/Documentation~/zh/inputsystem.md new file mode 100644 index 00000000..ce401c66 --- /dev/null +++ b/Documentation~/zh/inputsystem.md @@ -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) + + diff --git a/Editor/Windows/XChartsEditor.cs b/Editor/Windows/XChartsEditor.cs index d72e84ff..99ed4adc 100644 --- a/Editor/Windows/XChartsEditor.cs +++ b/Editor/Windows/XChartsEditor.cs @@ -1,8 +1,14 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; using UnityEditor; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; using XCharts.Runtime; +using ADB = UnityEditor.AssetDatabase; + namespace XCharts.Editor { @@ -22,7 +28,7 @@ namespace XCharts.Editor canvas = canvasObject.AddComponent(); canvas.renderMode = RenderMode.ScreenSpaceCamera; var mainCamera = GameObject.FindGameObjectWithTag("MainCamera"); - canvas.worldCamera = mainCamera == null? null : mainCamera.GetComponent(); + canvas.worldCamera = mainCamera == null ? null : mainCamera.GetComponent(); canvasObject.AddComponent(); canvasObject.AddComponent(); if (GameObject.Find("EventSystem") == null) @@ -188,23 +194,155 @@ namespace XCharts.Editor XCThemeMgr.ReloadThemeList(); } + #region Text mesh pro support +#if UNITY_2017_1_OR_NEWER + const string SYMBOL_TMP = "dUI_TextMeshPro"; + const string ASMDEF_TMP = "Unity.TextMeshPro"; + [MenuItem("XCharts/TextMeshPro Enable")] public static void EnableTextMeshPro() { - if (!XChartsMgr.IsExistTMPAssembly()) + if (!IsSpecifyAssemblyExist(ASMDEF_TMP)) { Debug.LogError("TextMeshPro is not in the project, please import TextMeshPro package first."); return; } - XChartsMgr.EnableTextMeshPro(); - XChartsMgr.ModifyTMPRefence(); + DefineSymbolsUtil.AddGlobalDefine(SYMBOL_TMP); + XChartsMgr.RemoveAllChartObject(); + InsertSpecifyReferenceIntoAssembly(Platform.Editor, ASMDEF_TMP); + InsertSpecifyReferenceIntoAssembly(Platform.Runtime, ASMDEF_TMP); } [MenuItem("XCharts/TextMeshPro Disable")] public static void DisableTextMeshPro() { - XChartsMgr.ModifyTMPRefence(true); - XChartsMgr.DisableTextMeshPro(); + RemoveSpecifyReferenceFromAssembly(Platform.Editor, ASMDEF_TMP); + RemoveSpecifyReferenceFromAssembly(Platform.Runtime, ASMDEF_TMP); + DefineSymbolsUtil.RemoveGlobalDefine(SYMBOL_TMP); + XChartsMgr.RemoveAllChartObject(); } +#endif + #endregion + + #region InputSystem Support +#if UNITY_2019_1_OR_NEWER + //As InputSystem is released in 2019.1+ ,when unity version is 2019.1+ , enable InputSystem Support + const string SYMBOL_I_S = "INPUT_SYSTEM_ENABLED"; + const string ASMDEF_I_S = "Unity.InputSystem"; + [MenuItem("XCharts/InputSystem Enable")] + public static void EnableInputSystem() + { + if (!IsSpecifyAssemblyExist(ASMDEF_I_S)) + { + Debug.LogError("InputSystem is not in the project, please import InputSystem package first."); + return; + } + // insert input system package into editor and runtime assembly + InsertSpecifyReferenceIntoAssembly(Platform.Editor, ASMDEF_I_S); + InsertSpecifyReferenceIntoAssembly(Platform.Runtime, ASMDEF_I_S); + // add scripting define symbols + DefineSymbolsUtil.AddGlobalDefine(SYMBOL_I_S); + } + [MenuItem("XCharts/InputSystem Disable")] + public static void DisableInputSystem() + { + // remove input system package into editor and runtime assembly + RemoveSpecifyReferenceFromAssembly(Platform.Editor, ASMDEF_I_S); + RemoveSpecifyReferenceFromAssembly(Platform.Runtime, ASMDEF_I_S); + // remove scripting define symbols + DefineSymbolsUtil.RemoveGlobalDefine(SYMBOL_I_S); + } +#endif + #endregion + + #region Assistant members +#if UNITY_2017_1_OR_NEWER + // as text mesh pro is released in 2017.1, so we may use these function and types in 2017.1 or later + private static void InsertSpecifyReferenceIntoAssembly(Platform platform, string reference) + { + var file = GetPackageAssemblyDefinitionPath(platform); + var content = File.ReadAllText(file); + var data = new AssemblyDefinitionData(); + EditorJsonUtility.FromJsonOverwrite(content, data); + if (!data.references.Contains(reference)) + { + data.references.Add(reference); + var json = EditorJsonUtility.ToJson(data, true); + File.WriteAllText(file, json); + AssetDatabase.SaveAssets(); + AssetDatabase.Refresh(); + } + } + + private static void RemoveSpecifyReferenceFromAssembly(Platform platform, string reference) + { + var file = GetPackageAssemblyDefinitionPath(platform); + var content = File.ReadAllText(file); + var data = new AssemblyDefinitionData(); + EditorJsonUtility.FromJsonOverwrite(content, data); + if (data.references.Contains(reference)) + { + data.references.Remove(reference); + var json = EditorJsonUtility.ToJson(data, true); + File.WriteAllText(file, json); + } + } + + public enum Platform { Editor, Runtime } + public static string GetPackageAssemblyDefinitionPath(Platform platform) + { + var p = platform == Platform.Editor ? "Editor" : "Runtime"; + var f = "XCharts." + p + ".asmdef"; + var sub = Path.Combine(p, f); + string packagePath = Path.GetFullPath("Packages/com.monitor1394.xcharts"); + if (!Directory.Exists(packagePath)) + { + packagePath = ADB.FindAssets("t:Script") + .Where(v => Path.GetFileNameWithoutExtension(ADB.GUIDToAssetPath(v)) == "XChartsMgr") + .Select(id => ADB.GUIDToAssetPath(id)) + .FirstOrDefault(); + packagePath = Path.GetDirectoryName(packagePath); + packagePath = packagePath.Substring(0, packagePath.LastIndexOf("Runtime")); + } + return Path.Combine(packagePath, sub); + } + + public static bool IsSpecifyAssemblyExist(string name) + { +#if UNITY_2018_1_OR_NEWER + foreach (var assembly in UnityEditor.Compilation.CompilationPipeline.GetAssemblies(UnityEditor.Compilation.AssembliesType.Player)) + { + if (assembly.name.Equals(name)) return true; + } +#elif UNITY_2017_3_OR_NEWER + foreach (var assembly in UnityEditor.Compilation.CompilationPipeline.GetAssemblies()) + { + if (assembly.name.Equals(name)) return true; + } +#endif + return false; + } + + [Serializable] + class AssemblyDefinitionData + { +#pragma warning disable 649 + public string name; + public List references; + public List includePlatforms; + public List excludePlatforms; + public bool allowUnsafeCode; + public bool overrideReferences; + public List precompiledReferences; + public bool autoReferenced; + public List defineConstraints; + public List versionDefines; + public bool noEngineReferences; +#pragma warning restore 649 + } +#endif + #endregion + + } } \ No newline at end of file diff --git a/Examples/Example00_CheatSheet.cs b/Examples/Example00_CheatSheet.cs index 99bce3df..4492c94b 100644 --- a/Examples/Example00_CheatSheet.cs +++ b/Examples/Example00_CheatSheet.cs @@ -11,42 +11,33 @@ namespace XCharts.Example private LineChart chart; private float speed = 100f; - void Awake() - { - LoopDemo(); - } - private void OnEnable() { - LoopDemo(); - } - - void LoopDemo() - { - StopAllCoroutines(); StartCoroutine(CheatSheet()); } IEnumerator CheatSheet() { StartCoroutine(InitChart()); - StartCoroutine(ComponentTitle()); - yield return new WaitForSeconds(2); - StartCoroutine(ComponentAxis()); - yield return new WaitForSeconds(2); - StartCoroutine(ComponentGrid()); - yield return new WaitForSeconds(2); - StartCoroutine(ComponentSerie()); - yield return new WaitForSeconds(4); - StartCoroutine(ComponentLegend()); - yield return new WaitForSeconds(4); - StartCoroutine(ComponentTheme()); - yield return new WaitForSeconds(4); - StartCoroutine(ComponentDataZoom()); - yield return new WaitForSeconds(5); - StartCoroutine(ComponentVisualMap()); - yield return new WaitForSeconds(3); - LoopDemo(); + while (true) + { + StartCoroutine(ComponentTitle()); + yield return new WaitForSeconds(2); + StartCoroutine(ComponentAxis()); + yield return new WaitForSeconds(2); + StartCoroutine(ComponentGrid()); + yield return new WaitForSeconds(2); + StartCoroutine(ComponentSerie()); + yield return new WaitForSeconds(4); + StartCoroutine(ComponentLegend()); + yield return new WaitForSeconds(4); + StartCoroutine(ComponentTheme()); + yield return new WaitForSeconds(4); + StartCoroutine(ComponentDataZoom()); + yield return new WaitForSeconds(5); + StartCoroutine(ComponentVisualMap()); + yield return new WaitForSeconds(3); + } } IEnumerator InitChart() @@ -54,8 +45,8 @@ namespace XCharts.Example chart = gameObject.GetComponent(); if (chart == null) gameObject.AddComponent(); - chart.GetChartComponent().show = true; - chart.GetChartComponent<Title>().text = "术语解析-组件"; + chart.GetOrAddChartComponent<Title>().show = true; + chart.EnsureChartComponent<Title>().text = "术语解析-组件"; var grid = chart.GetOrAddChartComponent<GridCoord>(); grid.bottom = 30; @@ -81,31 +72,31 @@ namespace XCharts.Example IEnumerator ComponentTitle() { - chart.GetChartComponent<Title>().text = "术语解析 - 组件"; - chart.GetChartComponent<Title>().subText = "Title 标题:可指定主标题和子标题"; - chart.GetChartComponent<XAxis>().show = true; - chart.GetChartComponent<YAxis>().show = true; - chart.GetChartComponent<Legend>().show = false; + chart.EnsureChartComponent<Title>().text = "术语解析 - 组件"; + chart.EnsureChartComponent<Title>().subText = "Title 标题:可指定主标题和子标题"; + chart.EnsureChartComponent<XAxis>().show = true; + chart.EnsureChartComponent<YAxis>().show = true; + chart.EnsureChartComponent<Legend>().show = false; chart.series[0].show = false; chart.series[1].show = false; for (int i = 0; i < 4; i++) { - chart.GetChartComponent<Title>().show = !chart.GetChartComponent<Title>().show; + chart.EnsureChartComponent<Title>().show = !chart.EnsureChartComponent<Title>().show; chart.RefreshChart(); yield return new WaitForSeconds(0.2f); } - chart.GetChartComponent<Title>().show = true; + chart.EnsureChartComponent<Title>().show = true; chart.RefreshChart(); } IEnumerator ComponentAxis() { - chart.GetChartComponent<Title>().subText = "Axis 坐标轴:配置X和Y轴的轴线、刻度、标签等样式外观配置"; + chart.EnsureChartComponent<Title>().subText = "Axis 坐标轴:配置X和Y轴的轴线、刻度、标签等样式外观配置"; chart.series[0].show = false; chart.series[1].show = false; - var xAxis = chart.GetChartComponent<XAxis>(); - var yAxis = chart.GetChartComponent<YAxis>(); + var xAxis = chart.EnsureChartComponent<XAxis>(); + var yAxis = chart.EnsureChartComponent<YAxis>(); for (int i = 0; i < 4; i++) { xAxis.show = !xAxis.show; @@ -121,8 +112,8 @@ namespace XCharts.Example IEnumerator ComponentGrid() { - chart.GetChartComponent<Title>().subText = "Grid 网格:调整坐标系边距和颜色等"; - var grid = chart.GetChartComponent<GridCoord>(); + chart.EnsureChartComponent<Title>().subText = "Grid 网格:调整坐标系边距和颜色等"; + var grid = chart.EnsureChartComponent<GridCoord>(); for (int i = 0; i < 4; i++) { grid.backgroundColor = i % 2 == 0 ? Color.clear : Color.grey; @@ -136,7 +127,7 @@ namespace XCharts.Example IEnumerator ComponentSerie() { - chart.GetChartComponent<Title>().subText = "Serie 系列:调整坐标系边距和颜色等"; + chart.EnsureChartComponent<Title>().subText = "Serie 系列:调整坐标系边距和颜色等"; chart.series[0].show = true; chart.series[1].show = true; chart.AnimationReset(); @@ -157,10 +148,10 @@ namespace XCharts.Example IEnumerator ComponentLegend() { - chart.GetChartComponent<Title>().subText = "Legend 图例:展示不同系列的名字和颜色,可控制系列显示等"; - var legend = chart.GetChartComponent<Legend>(); + chart.EnsureChartComponent<Title>().subText = "Legend 图例:展示不同系列的名字和颜色,可控制系列显示等"; + var legend = chart.EnsureChartComponent<Legend>(); legend.show = true; - var grid = chart.GetChartComponent<GridCoord>(); + var grid = chart.EnsureChartComponent<GridCoord>(); grid.top = 80; legend.location.top = 50; chart.RefreshChart(); @@ -187,23 +178,23 @@ namespace XCharts.Example IEnumerator ComponentTheme() { - chart.GetChartComponent<Title>().subText = "Theme 主题:可从全局上配置图表的颜色、字体等效果,支持默认主题切换"; + chart.EnsureChartComponent<Title>().subText = "Theme 主题:可从全局上配置图表的颜色、字体等效果,支持默认主题切换"; yield return new WaitForSeconds(1f); - chart.GetChartComponent<Title>().subText = "Theme 主题:Light主题"; + chart.EnsureChartComponent<Title>().subText = "Theme 主题:Light主题"; chart.UpdateTheme(ThemeType.Light); yield return new WaitForSeconds(1f); - chart.GetChartComponent<Title>().subText = "Theme 主题:Dark主题"; + chart.EnsureChartComponent<Title>().subText = "Theme 主题:Dark主题"; chart.UpdateTheme(ThemeType.Dark); yield return new WaitForSeconds(1f); - chart.GetChartComponent<Title>().subText = "Theme 主题:Default主题"; + chart.EnsureChartComponent<Title>().subText = "Theme 主题:Default主题"; chart.UpdateTheme(ThemeType.Default); yield return new WaitForSeconds(1f); } IEnumerator ComponentDataZoom() { - chart.GetChartComponent<Title>().subText = "DataZoom 区域缩放:可通过拖、拽、缩小、放大来观察细节数据"; - var grid = chart.GetChartComponent<GridCoord>(); + chart.EnsureChartComponent<Title>().subText = "DataZoom 区域缩放:可通过拖、拽、缩小、放大来观察细节数据"; + var grid = chart.EnsureChartComponent<GridCoord>(); grid.bottom = 70; var dataZoom = chart.GetOrAddChartComponent<DataZoom>(); @@ -265,7 +256,7 @@ namespace XCharts.Example IEnumerator ComponentVisualMap() { - chart.GetChartComponent<Title>().subText = "VisualMap 视觉映射:可从全局上配置图表的颜色、字体等效果,支持默认主题切换"; + chart.EnsureChartComponent<Title>().subText = "VisualMap 视觉映射:可从全局上配置图表的颜色、字体等效果,支持默认主题切换"; var visualMap = chart.GetOrAddChartComponent<VisualMap>(); visualMap.show = true; @@ -292,7 +283,7 @@ namespace XCharts.Example "#a50026" }; visualMap.AddColors(colors); - var grid = chart.GetChartComponent<GridCoord>(); + var grid = chart.EnsureChartComponent<GridCoord>(); grid.left = 80; grid.bottom = 100; chart.RefreshChart(); diff --git a/Examples/Example10_LineChart.cs b/Examples/Example10_LineChart.cs index 1067b991..c319751b 100644 --- a/Examples/Example10_LineChart.cs +++ b/Examples/Example10_LineChart.cs @@ -11,39 +11,30 @@ namespace XCharts.Example private Serie serie; private int m_DataNum = 8; - void Awake() - { - LoopDemo(); - } - private void OnEnable() { - LoopDemo(); - } - - void LoopDemo() - { - StopAllCoroutines(); StartCoroutine(PieDemo()); } IEnumerator PieDemo() { - StartCoroutine(AddSimpleLine()); - yield return new WaitForSeconds(2); - StartCoroutine(ChangeLineType()); - yield return new WaitForSeconds(8); - StartCoroutine(LineAreaStyleSettings()); - yield return new WaitForSeconds(5); - StartCoroutine(LineArrowSettings()); - yield return new WaitForSeconds(2); - StartCoroutine(LineSymbolSettings()); - yield return new WaitForSeconds(7); - StartCoroutine(LineLabelSettings()); - yield return new WaitForSeconds(3); - StartCoroutine(LineMutilSerie()); - yield return new WaitForSeconds(5); - LoopDemo(); + while (true) + { + StartCoroutine(AddSimpleLine()); + yield return new WaitForSeconds(2); + StartCoroutine(ChangeLineType()); + yield return new WaitForSeconds(8); + StartCoroutine(LineAreaStyleSettings()); + yield return new WaitForSeconds(5); + StartCoroutine(LineArrowSettings()); + yield return new WaitForSeconds(2); + StartCoroutine(LineSymbolSettings()); + yield return new WaitForSeconds(7); + StartCoroutine(LineLabelSettings()); + yield return new WaitForSeconds(3); + StartCoroutine(LineMutilSerie()); + yield return new WaitForSeconds(5); + } } IEnumerator AddSimpleLine() diff --git a/Examples/Example11_AddSinCurve.cs b/Examples/Example11_AddSinCurve.cs index 31491178..e54efba0 100644 --- a/Examples/Example11_AddSinCurve.cs +++ b/Examples/Example11_AddSinCurve.cs @@ -4,7 +4,6 @@ using XCharts.Runtime; namespace XCharts.Example { [DisallowMultipleComponent] - [ExecuteInEditMode] public class Example11_AddSinCurve : MonoBehaviour { private float time; @@ -18,14 +17,14 @@ namespace XCharts.Example { chart = gameObject.AddComponent<LineChart>(); } - chart.GetChartComponent<Title>().show = true; - chart.GetChartComponent<Title>().text = "Sin Curve"; + chart.EnsureChartComponent<Title>().show = true; + chart.EnsureChartComponent<Title>().text = "Sin Curve"; - chart.GetChartComponent<Tooltip>().show = true; - chart.GetChartComponent<Legend>().show = false; + chart.EnsureChartComponent<Tooltip>().show = true; + chart.EnsureChartComponent<Legend>().show = false; - var xAxis = chart.GetChartComponent<XAxis>(); - var yAxis = chart.GetChartComponent<YAxis>(); + var xAxis = chart.EnsureChartComponent<XAxis>(); + var yAxis = chart.EnsureChartComponent<YAxis>(); xAxis.show = true; yAxis.show = true; diff --git a/Examples/Example13_LineSimple.cs b/Examples/Example13_LineSimple.cs index 153ef575..0e0649f4 100644 --- a/Examples/Example13_LineSimple.cs +++ b/Examples/Example13_LineSimple.cs @@ -1,4 +1,7 @@ using UnityEngine; +#if INPUT_SYSTEM_ENABLED +using Input = XCharts.Runtime.InputHelper; +#endif using XCharts.Runtime; namespace XCharts.Example diff --git a/Examples/Example20_BarChart.cs b/Examples/Example20_BarChart.cs index 270321ac..5dcede4f 100644 --- a/Examples/Example20_BarChart.cs +++ b/Examples/Example20_BarChart.cs @@ -11,48 +11,38 @@ namespace XCharts.Example private Serie serie, serie2; private int m_DataNum = 5; - void Awake() - { - LoopDemo(); - } - private void OnEnable() { - LoopDemo(); - } - - void LoopDemo() - { - StopAllCoroutines(); StartCoroutine(PieDemo()); } IEnumerator PieDemo() { - StartCoroutine(AddSimpleBar()); - yield return new WaitForSeconds(2); - StartCoroutine(BarMutilSerie()); - yield return new WaitForSeconds(3); - StartCoroutine(ZebraBar()); - yield return new WaitForSeconds(3); - StartCoroutine(SameBarAndNotStack()); - yield return new WaitForSeconds(3); - StartCoroutine(SameBarAndStack()); - yield return new WaitForSeconds(3); - StartCoroutine(SameBarAndPercentStack()); - yield return new WaitForSeconds(10); - - LoopDemo(); + while (true) + { + StartCoroutine(AddSimpleBar()); + yield return new WaitForSeconds(2); + StartCoroutine(BarMutilSerie()); + yield return new WaitForSeconds(3); + StartCoroutine(ZebraBar()); + yield return new WaitForSeconds(3); + StartCoroutine(SameBarAndNotStack()); + yield return new WaitForSeconds(3); + StartCoroutine(SameBarAndStack()); + yield return new WaitForSeconds(3); + StartCoroutine(SameBarAndPercentStack()); + yield return new WaitForSeconds(10); + } } IEnumerator AddSimpleBar() { chart = gameObject.GetComponent<BarChart>(); if (chart == null) chart = gameObject.AddComponent<BarChart>(); - chart.GetChartComponent<Title>().text = "BarChart - 柱状图"; - chart.GetChartComponent<Title>().subText = "普通柱状图"; + chart.EnsureChartComponent<Title>().text = "BarChart - 柱状图"; + chart.EnsureChartComponent<Title>().subText = "普通柱状图"; - var yAxis = chart.GetChartComponent<YAxis>(); + var yAxis = chart.EnsureChartComponent<YAxis>(); yAxis.minMaxType = Axis.AxisMinMaxType.Default; chart.RemoveData(); @@ -68,7 +58,7 @@ namespace XCharts.Example IEnumerator BarMutilSerie() { - chart.GetChartComponent<Title>().subText = "多条柱状图"; + chart.EnsureChartComponent<Title>().subText = "多条柱状图"; float now = serie.barWidth - 0.35f; while (serie.barWidth > 0.35f) @@ -90,7 +80,7 @@ namespace XCharts.Example IEnumerator ZebraBar() { - chart.GetChartComponent<Title>().subText = "斑马柱状图"; + chart.EnsureChartComponent<Title>().subText = "斑马柱状图"; serie.barType = BarType.Zebra; serie2.barType = BarType.Zebra; serie.barZebraWidth = serie.barZebraGap = 4; @@ -101,7 +91,7 @@ namespace XCharts.Example IEnumerator SameBarAndNotStack() { - chart.GetChartComponent<Title>().subText = "非堆叠同柱"; + chart.EnsureChartComponent<Title>().subText = "非堆叠同柱"; serie.barType = serie2.barType = BarType.Normal; serie.stack = ""; serie2.stack = ""; @@ -112,7 +102,7 @@ namespace XCharts.Example IEnumerator SameBarAndStack() { - chart.GetChartComponent<Title>().subText = "堆叠同柱"; + chart.EnsureChartComponent<Title>().subText = "堆叠同柱"; serie.barType = serie2.barType = BarType.Normal; serie.stack = "samename"; serie2.stack = "samename"; @@ -132,19 +122,25 @@ namespace XCharts.Example IEnumerator SameBarAndPercentStack() { - chart.GetChartComponent<Title>().subText = "百分比堆叠同柱"; + chart.EnsureChartComponent<Title>().subText = "百分比堆叠同柱"; serie.barType = serie2.barType = BarType.Normal; serie.stack = "samename"; serie2.stack = "samename"; serie.barPercentStack = true; - - serie.AddExtraComponent<LabelStyle>(); + if (null == serie.label) + { + serie.AddExtraComponent<LabelStyle>(); + } serie.label.show = true; serie.label.position = LabelStyle.Position.Center; serie.label.textStyle.color = Color.white; serie.label.formatter = "{d:f0}%"; + if (null == serie2.label) + { + serie2.AddExtraComponent<LabelStyle>(); + } serie2.label.show = true; serie2.label.position = LabelStyle.Position.Center; serie2.label.textStyle.color = Color.white; diff --git a/Examples/Example30_PieChart.cs b/Examples/Example30_PieChart.cs index e40f063c..c7b563d3 100644 --- a/Examples/Example30_PieChart.cs +++ b/Examples/Example30_PieChart.cs @@ -13,45 +13,37 @@ namespace XCharts.Example private float m_RadiusSpeed = 100f; private float m_CenterSpeed = 1f; - void Awake() - { - LoopDemo(); - } - private void OnEnable() { - LoopDemo(); - } - - void LoopDemo() - { - StopAllCoroutines(); StartCoroutine(PieDemo()); } IEnumerator PieDemo() { - StartCoroutine(PieAdd()); - yield return new WaitForSeconds(2); - StartCoroutine(PieShowLabel()); - yield return new WaitForSeconds(4); - StartCoroutine(Doughnut()); - yield return new WaitForSeconds(3); - StartCoroutine(DoublePie()); - yield return new WaitForSeconds(2); - StartCoroutine(RosePie()); - yield return new WaitForSeconds(5); - LoopDemo(); + while (true) + { + StartCoroutine(PieAdd()); + yield return new WaitForSeconds(2); + StartCoroutine(PieShowLabel()); + yield return new WaitForSeconds(4); + StartCoroutine(Doughnut()); + yield return new WaitForSeconds(3); + StartCoroutine(DoublePie()); + yield return new WaitForSeconds(2); + StartCoroutine(RosePie()); + yield return new WaitForSeconds(5); + } } IEnumerator PieAdd() { chart = gameObject.GetComponent<PieChart>(); if (chart == null) chart = gameObject.AddComponent<PieChart>(); + yield return null; chart.GetChartComponent<Title>().text = "PieChart - 饼图"; chart.GetChartComponent<Title>().subText = "基础饼图"; - var legend = chart.GetChartComponent<Legend>(); + var legend = chart.EnsureChartComponent<Legend>(); legend.show = true; legend.location.align = Location.Align.TopLeft; legend.location.top = 60; @@ -72,7 +64,7 @@ namespace XCharts.Example chart.AddData(0, 135, "视频广告"); chart.AddData(0, 1548, "搜索引擎"); - chart.onPointerClickPie = delegate(PointerEventData e, int serieIndex, int dataIndex) + chart.onPointerClickPie = delegate (PointerEventData e, int serieIndex, int dataIndex) { }; @@ -81,7 +73,7 @@ namespace XCharts.Example IEnumerator PieShowLabel() { - chart.GetChartComponent<Title>().subText = "显示文本标签"; + chart.EnsureChartComponent<Title>().subText = "显示文本标签"; serie.AddExtraComponent<LabelStyle>(); serie.label.show = true; @@ -105,7 +97,7 @@ namespace XCharts.Example IEnumerator Doughnut() { - chart.GetChartComponent<Title>().subText = "圆环图"; + chart.EnsureChartComponent<Title>().subText = "圆环图"; serie.radius[0] = 2f; while (serie.radius[0] < serie.radius[1] * 0.7f) { @@ -129,8 +121,7 @@ namespace XCharts.Example IEnumerator DoublePie() { - chart.GetChartComponent<Title>().subText = "多图组合"; - + chart.EnsureChartComponent<Title>().subText = "多图组合"; serie1 = chart.AddSerie<Pie>("访问来源2"); chart.AddData(1, 335, "直达"); chart.AddData(1, 679, "营销广告"); @@ -146,7 +137,14 @@ namespace XCharts.Example chart.RefreshChart(); yield return null; } - + if (null == serie.label) + { + serie.AddExtraComponent<LabelStyle>(); + } + if (null == serie1.label) + { + serie1.AddExtraComponent<LabelStyle>(); + } serie1.label.show = true; serie1.label.position = LabelStyle.Position.Inside; serie1.label.textStyle.color = Color.white; @@ -158,8 +156,8 @@ namespace XCharts.Example IEnumerator RosePie() { - chart.GetChartComponent<Title>().subText = "玫瑰图"; - chart.GetChartComponent<Legend>().show = false; + chart.EnsureChartComponent<Title>().subText = "玫瑰图"; + chart.EnsureChartComponent<Legend>().show = false; serie1.ClearData(); serie.ClearData(); serie1.radius = serie.radius = new float[2] { 0, 80 }; diff --git a/Examples/Example31_PieUpdateName.cs b/Examples/Example31_PieUpdateName.cs index 85091050..8cf1e61e 100644 --- a/Examples/Example31_PieUpdateName.cs +++ b/Examples/Example31_PieUpdateName.cs @@ -1,6 +1,8 @@ using UnityEngine; using XCharts.Runtime; - +#if INPUT_SYSTEM_ENABLED +using Input = XCharts.Runtime.InputHelper; +#endif namespace XCharts.Example { [DisallowMultipleComponent] diff --git a/Examples/Example41_RadarUpdate.cs b/Examples/Example41_RadarUpdate.cs index baaa46be..7ed98895 100644 --- a/Examples/Example41_RadarUpdate.cs +++ b/Examples/Example41_RadarUpdate.cs @@ -1,6 +1,8 @@ using UnityEngine; using XCharts.Runtime; - +#if INPUT_SYSTEM_ENABLED +using Input = XCharts.Runtime.InputHelper; +#endif namespace XCharts.Example { [DisallowMultipleComponent] diff --git a/Examples/Example80_Polar.cs b/Examples/Example80_Polar.cs index fdfb0b04..a468fe09 100644 --- a/Examples/Example80_Polar.cs +++ b/Examples/Example80_Polar.cs @@ -1,6 +1,8 @@ using UnityEngine; using XCharts.Runtime; - +#if INPUT_SYSTEM_ENABLED +using Input = XCharts.Runtime.InputHelper; +#endif namespace XCharts.Example { [DisallowMultipleComponent] diff --git a/Examples/Example90_Candlestick.cs b/Examples/Example90_Candlestick.cs index 7218b6b9..736925c5 100644 --- a/Examples/Example90_Candlestick.cs +++ b/Examples/Example90_Candlestick.cs @@ -1,6 +1,8 @@ using UnityEngine; using XCharts.Runtime; - +#if INPUT_SYSTEM_ENABLED +using Input = XCharts.Runtime.InputHelper; +#endif namespace XCharts.Example { [DisallowMultipleComponent] @@ -18,7 +20,7 @@ namespace XCharts.Example { chart = gameObject.AddComponent<CandlestickChart>(); } - GenerateOHLC(dataCount); + AddData(); } void Update() @@ -29,9 +31,7 @@ namespace XCharts.Example } } - void AddData() { } - - void GenerateOHLC(int count) + void AddData() { chart.ClearData(); @@ -40,7 +40,7 @@ namespace XCharts.Example var boxVals = new float[4]; var dayRange = 12; - for (int i = 0; i < count; i++) + for (int i = 0; i < dataCount; i++) { baseValue = baseValue + Random.Range(0f, 1f) * 30 - 10; for (int j = 0; j < 4; j++) diff --git a/Examples/Example_AddChart.cs b/Examples/Example_AddChart.cs index 80359764..145eb63f 100644 --- a/Examples/Example_AddChart.cs +++ b/Examples/Example_AddChart.cs @@ -1,5 +1,8 @@ using UnityEngine; using XCharts.Runtime; +#if INPUT_SYSTEM_ENABLED +using Input = XCharts.Runtime.InputHelper; +#endif namespace XCharts.Example { diff --git a/Examples/Example_DynamicChart.cs b/Examples/Example_DynamicChart.cs index df35dfb1..2a999d9a 100644 --- a/Examples/Example_DynamicChart.cs +++ b/Examples/Example_DynamicChart.cs @@ -1,6 +1,8 @@ using UnityEngine; using XCharts.Runtime; - +#if INPUT_SYSTEM_ENABLED +using Input = XCharts.Runtime.InputHelper; +#endif namespace XCharts.Example { [DisallowMultipleComponent] diff --git a/Examples/Example_Test.cs b/Examples/Example_Test.cs index e51b0ac9..33018a71 100644 --- a/Examples/Example_Test.cs +++ b/Examples/Example_Test.cs @@ -1,7 +1,9 @@ using UnityEngine; using UnityEngine.UI; using XCharts.Runtime; - +#if INPUT_SYSTEM_ENABLED +using Input = XCharts.Runtime.InputHelper; +#endif namespace XCharts.Example { [DisallowMultipleComponent] diff --git a/Examples/Example_TestTime.cs b/Examples/Example_TestTime.cs index 59f813d2..1ed2804b 100644 --- a/Examples/Example_TestTime.cs +++ b/Examples/Example_TestTime.cs @@ -1,6 +1,8 @@ using UnityEngine; using XCharts.Runtime; - +#if INPUT_SYSTEM_ENABLED +using Input = XCharts.Runtime.InputHelper; +#endif namespace XCharts.Example { [DisallowMultipleComponent] diff --git a/Examples/XCharts.Examples.asmdef b/Examples/XCharts.Examples.asmdef index 638b2960..2428dbeb 100644 --- a/Examples/XCharts.Examples.asmdef +++ b/Examples/XCharts.Examples.asmdef @@ -10,5 +10,6 @@ "overrideReferences": false, "precompiledReferences": [], "autoReferenced": true, - "defineConstraints": [] + "defineConstraints": [], + "versionDefines": [] } \ No newline at end of file diff --git a/README.md b/README.md index dd1d3b8a..faf54529 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ - 支持万级大数据量绘制,支持采样绘制。 - 支持`TexMeshPro`。 - 支持所有`5.6`以上的`Unity`版本。 +- 支持 Input System ([如何从 Input Manager 转 Input System](Documentation~/zh/inputsystem.md))。 ## 截图 diff --git a/Runtime/Component/DataZoom/DataZoom.cs b/Runtime/Component/DataZoom/DataZoom.cs index 030a0c52..eb1407c9 100644 --- a/Runtime/Component/DataZoom/DataZoom.cs +++ b/Runtime/Component/DataZoom/DataZoom.cs @@ -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) diff --git a/Runtime/Component/DataZoom/DataZoomHandler.cs b/Runtime/Component/DataZoom/DataZoomHandler.cs index 50f9f755..54a88121 100644 --- a/Runtime/Component/DataZoom/DataZoomHandler.cs +++ b/Runtime/Component/DataZoom/DataZoomHandler.cs @@ -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 { diff --git a/Runtime/Component/VisualMap/VisualMapHandler.cs b/Runtime/Component/VisualMap/VisualMapHandler.cs index a6f672da..32089ea6 100644 --- a/Runtime/Component/VisualMap/VisualMapHandler.cs +++ b/Runtime/Component/VisualMap/VisualMapHandler.cs @@ -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] diff --git a/Runtime/Internal/BaseChart.Component.cs b/Runtime/Internal/BaseChart.Component.cs index b989f2d9..62dab6a7 100644 --- a/Runtime/Internal/BaseChart.Component.cs +++ b/Runtime/Internal/BaseChart.Component.cs @@ -253,7 +253,15 @@ 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 { diff --git a/Runtime/Internal/BaseChart.cs b/Runtime/Internal/BaseChart.cs index 10499ebb..3d90a2d3 100644 --- a/Runtime/Internal/BaseChart.cs +++ b/Runtime/Internal/BaseChart.cs @@ -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 { diff --git a/Runtime/Internal/BaseGraph.cs b/Runtime/Internal/BaseGraph.cs index a74da86a..d5481354 100644 --- a/Runtime/Internal/BaseGraph.cs +++ b/Runtime/Internal/BaseGraph.cs @@ -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 { @@ -9,302 +12,303 @@ namespace XCharts.Runtime public partial class BaseGraph : MaskableGraphic, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler, IBeginDragHandler, IPointerClickHandler, IDragHandler, IEndDragHandler, IScrollHandler + { + [SerializeField] protected bool m_EnableTextMeshPro = false; + + protected Painter m_Painter; + protected int m_SiblingIndex; + + protected float m_GraphWidth; + protected float m_GraphHeight; + protected float m_GraphX; + protected float m_GraphY; + protected Vector3 m_GraphPosition = Vector3.zero; + protected Vector2 m_GraphMinAnchor; + protected Vector2 m_GraphMaxAnchor; + protected Vector2 m_GraphPivot; + protected Vector2 m_GraphSizeDelta; + protected Vector2 m_GraphAnchoredPosition; + protected Rect m_GraphRect = new Rect(0, 0, 0, 0); + protected bool m_RefreshChart = false; + protected bool m_ForceOpenRaycastTarget; + protected bool m_IsControlledByLayout = false; + protected bool m_PainerDirty = false; + protected bool m_IsOnValidate = false; + protected Vector3 m_LastLocalPosition; + + protected Action<PointerEventData, BaseGraph> m_OnPointerClick; + protected Action<PointerEventData, BaseGraph> m_OnPointerDown; + protected Action<PointerEventData, BaseGraph> m_OnPointerUp; + protected Action<PointerEventData, BaseGraph> m_OnPointerEnter; + protected Action<PointerEventData, BaseGraph> m_OnPointerExit; + protected Action<PointerEventData, BaseGraph> m_OnBeginDrag; + protected Action<PointerEventData, BaseGraph> m_OnDrag; + protected Action<PointerEventData, BaseGraph> m_OnEndDrag; + protected Action<PointerEventData, BaseGraph> m_OnScroll; + + public virtual HideFlags chartHideFlags { get { return HideFlags.None; } } + + private ScrollRect m_ScrollRect; + + public Painter painter { get { return m_Painter; } } + + protected virtual void InitComponent() { - [SerializeField] protected bool m_EnableTextMeshPro = false; + InitPainter(); + } - protected Painter m_Painter; - protected int m_SiblingIndex; + protected override void Awake() + { + CheckTextMeshPro(); + m_SiblingIndex = 0; + m_LastLocalPosition = transform.localPosition; + UpdateSize(); + InitComponent(); + CheckIsInScrollRect(); + } - protected float m_GraphWidth; - protected float m_GraphHeight; - protected float m_GraphX; - protected float m_GraphY; - protected Vector3 m_GraphPosition = Vector3.zero; - protected Vector2 m_GraphMinAnchor; - protected Vector2 m_GraphMaxAnchor; - protected Vector2 m_GraphPivot; - protected Vector2 m_GraphSizeDelta; - protected Vector2 m_GraphAnchoredPosition; - protected Rect m_GraphRect = new Rect(0, 0, 0, 0); - protected bool m_RefreshChart = false; - protected bool m_ForceOpenRaycastTarget; - protected bool m_IsControlledByLayout = false; - protected bool m_PainerDirty = false; - protected bool m_IsOnValidate = false; - protected Vector3 m_LastLocalPosition; + protected override void Start() + { + m_RefreshChart = true; + } - protected Action<PointerEventData, BaseGraph> m_OnPointerClick; - protected Action<PointerEventData, BaseGraph> m_OnPointerDown; - protected Action<PointerEventData, BaseGraph> m_OnPointerUp; - protected Action<PointerEventData, BaseGraph> m_OnPointerEnter; - protected Action<PointerEventData, BaseGraph> m_OnPointerExit; - protected Action<PointerEventData, BaseGraph> m_OnBeginDrag; - protected Action<PointerEventData, BaseGraph> m_OnDrag; - protected Action<PointerEventData, BaseGraph> m_OnEndDrag; - protected Action<PointerEventData, BaseGraph> m_OnScroll; - - public virtual HideFlags chartHideFlags { get { return HideFlags.None; } } - - private ScrollRect m_ScrollRect; - - public Painter painter { get { return m_Painter; } } - - protected virtual void InitComponent() - { - InitPainter(); - } - - protected override void Awake() + protected virtual void Update() + { + CheckSize(); + if (m_IsOnValidate) { + m_IsOnValidate = false; CheckTextMeshPro(); - m_SiblingIndex = 0; - m_LastLocalPosition = transform.localPosition; - UpdateSize(); InitComponent(); - CheckIsInScrollRect(); + RefreshGraph(); } - - protected override void Start() + else { - m_RefreshChart = true; + CheckComponent(); } + CheckPointerPos(); + CheckRefreshChart(); + CheckRefreshPainter(); + } - protected virtual void Update() - { - CheckSize(); - if (m_IsOnValidate) - { - m_IsOnValidate = false; - CheckTextMeshPro(); - InitComponent(); - RefreshGraph(); - } - else - { - CheckComponent(); - } - CheckPointerPos(); - CheckRefreshChart(); - CheckRefreshPainter(); - } - - protected virtual void SetAllComponentDirty() - { + protected virtual void SetAllComponentDirty() + { #if UNITY_EDITOR - if (!Application.isPlaying) - { - m_IsOnValidate = true; - } -#endif - m_PainerDirty = true; - } - - protected virtual void CheckComponent() - { - if (m_PainerDirty) - { - InitPainter(); - m_PainerDirty = false; - } - } - - private void CheckTextMeshPro() - { -#if dUI_TextMeshPro - var enableTextMeshPro = true; -#else - var enableTextMeshPro = false; -#endif - if (m_EnableTextMeshPro != enableTextMeshPro) - { - m_EnableTextMeshPro = enableTextMeshPro; - RebuildChartObject(); - } - } - -#if UNITY_EDITOR - protected override void Reset() { } - - protected override void OnValidate() + if (!Application.isPlaying) { m_IsOnValidate = true; } #endif + m_PainerDirty = true; + } - protected override void OnDestroy() + protected virtual void CheckComponent() + { + if (m_PainerDirty) { - for (int i = transform.childCount - 1; i >= 0; i--) - { - DestroyImmediate(transform.GetChild(i).gameObject); - } - } - - protected override void OnPopulateMesh(VertexHelper vh) - { - vh.Clear(); - } - - protected virtual void InitPainter() - { - m_Painter = ChartHelper.AddPainterObject("painter_b", transform, m_GraphMinAnchor, - m_GraphMaxAnchor, m_GraphPivot, new Vector2(m_GraphWidth, m_GraphHeight), chartHideFlags, 1); - m_Painter.type = Painter.Type.Base; - m_Painter.onPopulateMesh = OnDrawPainterBase; - m_Painter.transform.SetSiblingIndex(0); - } - - private void CheckSize() - { - var currWidth = rectTransform.rect.width; - var currHeight = rectTransform.rect.height; - - if (m_GraphWidth == 0 && m_GraphHeight == 0 && (currWidth != 0 || currHeight != 0)) - { - Awake(); - } - - if (m_GraphWidth != currWidth || - m_GraphHeight != currHeight || - m_GraphMinAnchor != rectTransform.anchorMin || - m_GraphMaxAnchor != rectTransform.anchorMax || - m_GraphAnchoredPosition != rectTransform.anchoredPosition) - { - UpdateSize(); - } - if (!ChartHelper.IsValueEqualsVector3(m_LastLocalPosition, transform.localPosition)) - { - m_LastLocalPosition = transform.localPosition; - OnLocalPositionChanged(); - } - } - - protected void UpdateSize() - { - m_GraphWidth = rectTransform.rect.width; - m_GraphHeight = rectTransform.rect.height; - - m_GraphMaxAnchor = rectTransform.anchorMax; - m_GraphMinAnchor = rectTransform.anchorMin; - m_GraphSizeDelta = rectTransform.sizeDelta; - m_GraphAnchoredPosition = rectTransform.anchoredPosition; - - rectTransform.pivot = LayerHelper.ResetChartPositionAndPivot(m_GraphMinAnchor, m_GraphMaxAnchor, - m_GraphWidth, m_GraphHeight, ref m_GraphX, ref m_GraphY); - m_GraphPivot = rectTransform.pivot; - - m_GraphRect.x = m_GraphX; - m_GraphRect.y = m_GraphY; - m_GraphRect.width = m_GraphWidth; - m_GraphRect.height = m_GraphHeight; - m_GraphPosition.x = m_GraphX; - m_GraphPosition.y = m_GraphY; - - OnSizeChanged(); - } - - private void CheckPointerPos() - { - if (!isPointerInChart) return; - if (canvas == null) return; - Vector2 local; - if (!ScreenPointToChartPoint(Input.mousePosition, out local)) - { - pointerPos = Vector2.zero; - } - else - { - pointerPos = local; - } - } - - protected virtual void CheckIsInScrollRect() - { - m_ScrollRect = GetComponentInParent<ScrollRect>(); - } - - protected virtual void CheckRefreshChart() - { - if (m_RefreshChart) - { - m_Painter.Refresh(); - m_RefreshChart = false; - } - } - - protected virtual void CheckRefreshPainter() - { - m_Painter.CheckRefresh(); - } - - internal virtual void RefreshPainter(Painter painter) - { - if (painter == null) return; - painter.Refresh(); - } - - protected virtual void OnSizeChanged() - { - m_RefreshChart = true; - } - - protected virtual void OnLocalPositionChanged() { } - - protected virtual void OnDrawPainterBase(VertexHelper vh, Painter painter) - { - DrawPainterBase(vh); - } - - protected virtual void DrawPainterBase(VertexHelper vh) { } - - public virtual void OnPointerClick(PointerEventData eventData) - { - if (m_OnPointerClick != null) m_OnPointerClick(eventData, this); - } - - public virtual void OnPointerDown(PointerEventData eventData) - { - if (m_OnPointerDown != null) m_OnPointerDown(eventData, this); - } - - public virtual void OnPointerUp(PointerEventData eventData) - { - if (m_OnPointerUp != null) m_OnPointerUp(eventData, this); - } - - public virtual void OnPointerEnter(PointerEventData eventData) - { - isPointerInChart = true; - if (m_OnPointerEnter != null) m_OnPointerEnter(eventData, this); - } - - public virtual void OnPointerExit(PointerEventData eventData) - { - isPointerInChart = false; - if (m_OnPointerExit != null) m_OnPointerExit(eventData, this); - } - - public virtual void OnBeginDrag(PointerEventData eventData) - { - if (m_ScrollRect != null) m_ScrollRect.OnBeginDrag(eventData); - if (m_OnBeginDrag != null) m_OnBeginDrag(eventData, this); - } - - public virtual void OnEndDrag(PointerEventData eventData) - { - if (m_ScrollRect != null) m_ScrollRect.OnEndDrag(eventData); - if (m_OnEndDrag != null) m_OnEndDrag(eventData, this); - } - - public virtual void OnDrag(PointerEventData eventData) - { - if (m_ScrollRect != null) m_ScrollRect.OnDrag(eventData); - if (m_OnDrag != null) m_OnDrag(eventData, this); - } - - public virtual void OnScroll(PointerEventData eventData) - { - if (m_ScrollRect != null) m_ScrollRect.OnScroll(eventData); - if (m_OnScroll != null) m_OnScroll(eventData, this); + InitPainter(); + m_PainerDirty = false; } } + + private void CheckTextMeshPro() + { +#if dUI_TextMeshPro + var enableTextMeshPro = true; +#else + var enableTextMeshPro = false; +#endif + if (m_EnableTextMeshPro != enableTextMeshPro) + { + m_EnableTextMeshPro = enableTextMeshPro; + RebuildChartObject(); + } + } + +#if UNITY_EDITOR + protected override void Reset() { } + + protected override void OnValidate() + { + m_IsOnValidate = true; + } +#endif + + protected override void OnDestroy() + { + for (int i = transform.childCount - 1; i >= 0; i--) + { + DestroyImmediate(transform.GetChild(i).gameObject); + } + } + + protected override void OnPopulateMesh(VertexHelper vh) + { + vh.Clear(); + } + + protected virtual void InitPainter() + { + m_Painter = ChartHelper.AddPainterObject("painter_b", transform, m_GraphMinAnchor, + m_GraphMaxAnchor, m_GraphPivot, new Vector2(m_GraphWidth, m_GraphHeight), chartHideFlags, 1); + m_Painter.type = Painter.Type.Base; + m_Painter.onPopulateMesh = OnDrawPainterBase; + m_Painter.transform.SetSiblingIndex(0); + } + + private void CheckSize() + { + var currWidth = rectTransform.rect.width; + var currHeight = rectTransform.rect.height; + + if (m_GraphWidth == 0 && m_GraphHeight == 0 && (currWidth != 0 || currHeight != 0)) + { + Awake(); + } + + if (m_GraphWidth != currWidth || + m_GraphHeight != currHeight || + m_GraphMinAnchor != rectTransform.anchorMin || + m_GraphMaxAnchor != rectTransform.anchorMax || + m_GraphAnchoredPosition != rectTransform.anchoredPosition) + { + UpdateSize(); + } + if (!ChartHelper.IsValueEqualsVector3(m_LastLocalPosition, transform.localPosition)) + { + m_LastLocalPosition = transform.localPosition; + OnLocalPositionChanged(); + } + } + + protected void UpdateSize() + { + m_GraphWidth = rectTransform.rect.width; + m_GraphHeight = rectTransform.rect.height; + + m_GraphMaxAnchor = rectTransform.anchorMax; + m_GraphMinAnchor = rectTransform.anchorMin; + m_GraphSizeDelta = rectTransform.sizeDelta; + m_GraphAnchoredPosition = rectTransform.anchoredPosition; + + rectTransform.pivot = LayerHelper.ResetChartPositionAndPivot(m_GraphMinAnchor, m_GraphMaxAnchor, + m_GraphWidth, m_GraphHeight, ref m_GraphX, ref m_GraphY); + m_GraphPivot = rectTransform.pivot; + + m_GraphRect.x = m_GraphX; + m_GraphRect.y = m_GraphY; + m_GraphRect.width = m_GraphWidth; + m_GraphRect.height = m_GraphHeight; + m_GraphPosition.x = m_GraphX; + m_GraphPosition.y = m_GraphY; + + OnSizeChanged(); + } + + private void CheckPointerPos() + { + if (!isPointerInChart) return; + if (canvas == null) return; + Vector2 mousePos = Input.mousePosition; + Vector2 local; + if (!ScreenPointToChartPoint(mousePos, out local)) + { + pointerPos = Vector2.zero; + } + else + { + pointerPos = local; + } + } + + protected virtual void CheckIsInScrollRect() + { + m_ScrollRect = GetComponentInParent<ScrollRect>(); + } + + protected virtual void CheckRefreshChart() + { + if (m_RefreshChart) + { + m_Painter.Refresh(); + m_RefreshChart = false; + } + } + + protected virtual void CheckRefreshPainter() + { + m_Painter.CheckRefresh(); + } + + internal virtual void RefreshPainter(Painter painter) + { + if (painter == null) return; + painter.Refresh(); + } + + protected virtual void OnSizeChanged() + { + m_RefreshChart = true; + } + + protected virtual void OnLocalPositionChanged() { } + + protected virtual void OnDrawPainterBase(VertexHelper vh, Painter painter) + { + DrawPainterBase(vh); + } + + protected virtual void DrawPainterBase(VertexHelper vh) { } + + public virtual void OnPointerClick(PointerEventData eventData) + { + if (m_OnPointerClick != null) m_OnPointerClick(eventData, this); + } + + public virtual void OnPointerDown(PointerEventData eventData) + { + if (m_OnPointerDown != null) m_OnPointerDown(eventData, this); + } + + public virtual void OnPointerUp(PointerEventData eventData) + { + if (m_OnPointerUp != null) m_OnPointerUp(eventData, this); + } + + public virtual void OnPointerEnter(PointerEventData eventData) + { + isPointerInChart = true; + if (m_OnPointerEnter != null) m_OnPointerEnter(eventData, this); + } + + public virtual void OnPointerExit(PointerEventData eventData) + { + isPointerInChart = false; + if (m_OnPointerExit != null) m_OnPointerExit(eventData, this); + } + + public virtual void OnBeginDrag(PointerEventData eventData) + { + if (m_ScrollRect != null) m_ScrollRect.OnBeginDrag(eventData); + if (m_OnBeginDrag != null) m_OnBeginDrag(eventData, this); + } + + public virtual void OnEndDrag(PointerEventData eventData) + { + if (m_ScrollRect != null) m_ScrollRect.OnEndDrag(eventData); + if (m_OnEndDrag != null) m_OnEndDrag(eventData, this); + } + + public virtual void OnDrag(PointerEventData eventData) + { + if (m_ScrollRect != null) m_ScrollRect.OnDrag(eventData); + if (m_OnDrag != null) m_OnDrag(eventData, this); + } + + public virtual void OnScroll(PointerEventData eventData) + { + if (m_ScrollRect != null) m_ScrollRect.OnScroll(eventData); + if (m_OnScroll != null) m_OnScroll(eventData, this); + } + } } \ No newline at end of file diff --git a/Runtime/Internal/Utilities/InputHelper.cs b/Runtime/Internal/Utilities/InputHelper.cs new file mode 100644 index 00000000..94870706 --- /dev/null +++ b/Runtime/Internal/Utilities/InputHelper.cs @@ -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 diff --git a/Runtime/Internal/Utilities/InputHelper.cs.meta b/Runtime/Internal/Utilities/InputHelper.cs.meta new file mode 100644 index 00000000..fddfc435 --- /dev/null +++ b/Runtime/Internal/Utilities/InputHelper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5069defa9fe8c7a43843e1189e2d606c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Internal/XChartsMgr.cs b/Runtime/Internal/XChartsMgr.cs index 64265b24..5de9998d 100644 --- a/Runtime/Internal/XChartsMgr.cs +++ b/Runtime/Internal/XChartsMgr.cs @@ -2,8 +2,9 @@ using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.SceneManagement; +using System.Linq; #if UNITY_EDITOR -using UnityEditor; +using ADB = UnityEditor.AssetDatabase; #endif namespace XCharts.Runtime @@ -127,6 +128,7 @@ namespace XCharts.Runtime } #if UNITY_EDITOR + public static string GetPackageFullPath() { string packagePath = Path.GetFullPath("Packages/com.monitor1394.xcharts"); @@ -134,38 +136,13 @@ namespace XCharts.Runtime { return packagePath; } - packagePath = Path.GetFullPath("Assets/.."); - if (Directory.Exists(packagePath)) - { - if (File.Exists(packagePath + "/Assets/Packages/XCharts/package.json")) - { - return packagePath + "/Assets/Packages/XCharts"; - } - - if (File.Exists(packagePath + "/Assets/XCharts/package.json")) - { - return packagePath + "/Assets/XCharts"; - } - - string[] matchingPaths = Directory.GetDirectories(packagePath, "XCharts", SearchOption.AllDirectories); - string path = ValidateLocation(matchingPaths, packagePath); - if (path != null) return Path.Combine(packagePath, path); - } - return null; - } - - private static string ValidateLocation(string[] paths, string projectPath) - { - for (int i = 0; i < paths.Length; i++) - { - if (File.Exists(paths[i] + "/package.json")) - { - string folderPath = paths[i].Replace(projectPath, ""); - folderPath = folderPath.TrimStart('\\', '/'); - return folderPath; - } - } - return null; + packagePath = ADB.FindAssets("t:Script") + .Where(v => Path.GetFileNameWithoutExtension(ADB.GUIDToAssetPath(v)) == "XChartsMgr") + .Select(id => ADB.GUIDToAssetPath(id)) + .FirstOrDefault(); + packagePath = Path.GetDirectoryName(packagePath); + packagePath = packagePath.Substring(0, packagePath.LastIndexOf("Runtime")); + return packagePath; } [UnityEditor.Callbacks.DidReloadScripts] @@ -185,133 +162,6 @@ namespace XCharts.Runtime } } } - - public static void EnableTextMeshPro() - { - DefineSymbolsUtil.AddGlobalDefine("dUI_TextMeshPro"); - RemoveAllChartObject(); - } - - public static void DisableTextMeshPro() - { - DefineSymbolsUtil.RemoveGlobalDefine("dUI_TextMeshPro"); - RemoveAllChartObject(); - } - - public static bool IsExistTMPAssembly() - { - -#if UNITY_2018_1_OR_NEWER - foreach (var assembly in UnityEditor.Compilation.CompilationPipeline.GetAssemblies(UnityEditor.Compilation.AssembliesType.Player)) - { - if (assembly.name.Equals("Unity.TextMeshPro")) return true; - } -#elif UNITY_2017_3_OR_NEWER - foreach (var assembly in UnityEditor.Compilation.CompilationPipeline.GetAssemblies()) - { - if (assembly.name.Equals("Unity.TextMeshPro")) return true; - } -#endif - return false; - } - - public static bool ModifyTMPRefence(bool removeTMP = false) - { - var packagePath = GetPackageFullPath(); - if (!ModifyTMPRefence(packagePath + "/Runtime/XCharts.Runtime.asmdef", removeTMP)) return false; - if (!ModifyTMPRefence(packagePath + "/Editor/XCharts.Editor.asmdef", removeTMP)) return false; - return true; - } - - private static bool ModifyTMPRefence(string asmdefPath, bool removeTMP = false) - { - if (!File.Exists(asmdefPath)) - { - Debug.LogError("AddTMPRefence ERROR: can't find: " + asmdefPath); - return false; - } - try - { - var dest = new List<string>(); - var refs = new List<string>(); - var lines = File.ReadAllLines(asmdefPath); - var referencesStart = false; - var addedTMP = false; - var removedTMP = false; - var tmpName = "\"Unity.TextMeshPro\""; - var refCount = 0; - foreach (var line in lines) - { - if (string.IsNullOrEmpty(line)) continue; - if (line.Contains("\"references\": [")) - { - dest.Add(line); - referencesStart = true; - } - else if (referencesStart) - { - if (line.Contains("],")) - { - referencesStart = false; - if (refCount > 0) - { - var old = dest[dest.Count - 1]; - if (old.EndsWith(",")) - dest[dest.Count - 1] = old.Substring(0, old.Length - 1); - } - if (!removeTMP && !refs.Contains(tmpName)) - { - if (refs.Count > 0) - dest[dest.Count - 1] = dest[dest.Count - 1] + ","; - dest.Add(" " + tmpName); - dest.Add(line); - addedTMP = true; - } - else - { - dest.Add(line); - } - } - else - { - if (removeTMP) - { - if (!line.Contains(tmpName)) - { - dest.Add(line); - refCount++; - } - else - { - removedTMP = true; - } - } - else - { - dest.Add(line); - refs.Add(line.Trim()); - } - } - } - else - { - dest.Add(line); - } - } - if (addedTMP || removedTMP) - { - File.WriteAllText(asmdefPath, string.Join("\n", dest.ToArray())); - AssetDatabase.SaveAssets(); - AssetDatabase.Refresh(); - } - return true; - } - catch (System.Exception e) - { - Debug.LogError("AddTMPRefence ERROR:" + e.Message); - return false; - } - } #endif } } \ No newline at end of file diff --git a/Runtime/XCharts.Runtime.asmdef b/Runtime/XCharts.Runtime.asmdef index aa9babc9..64609360 100644 --- a/Runtime/XCharts.Runtime.asmdef +++ b/Runtime/XCharts.Runtime.asmdef @@ -1,13 +1,13 @@ { "name": "XCharts.Runtime", - "references": [ - ], - "optionalUnityReferences": [], + "references": [], "includePlatforms": [], "excludePlatforms": [], "allowUnsafeCode": false, "overrideReferences": false, "precompiledReferences": [], "autoReferenced": true, - "defineConstraints": [] + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false } \ No newline at end of file