using UnityEngine; namespace EasyChart.UGUI { /// /// Runtime component for testing JSON data injection into UGUIChartBridge. /// Provides an Inspector interface similar to the EasyChart Library Window's JSON Injection panel. /// [RequireComponent(typeof(UGUIChartBridge))] [AddComponentMenu("EasyChart/UGUI Runtime JSON Injection")] public class UGUIRuntimeJsonInjection : MonoBehaviour { [Header("JSON Generation Settings")] [Tooltip("The format mode for generating example JSON")] [SerializeField] private ChartJsonExampleMode _exampleMode = ChartJsonExampleMode.Standard; [Tooltip("The data format mode for series data")] [SerializeField] private ChartJsonDatasMode _datasMode = ChartJsonDatasMode.Standard; [Tooltip("Wrap JSON in API response envelope")] [SerializeField] private bool _useApiEnvelope = false; [Tooltip("Automatically regenerate JSON when ExampleMode or DatasMode changes")] [SerializeField] private bool _autoGenerateJson = false; [Header("JSON Content")] [Tooltip("The JSON string to inject into the chart")] [TextArea(10, 30)] [SerializeField] private string _jsonContent = ""; private UGUIChartBridge _bridge; private ChartProfile _runtimeProfile; private bool _profileCloned; /// /// The JSON content to inject. /// public string JsonContent { get => _jsonContent; set => _jsonContent = value; } /// /// The example mode for JSON generation. /// public ChartJsonExampleMode ExampleMode { get => _exampleMode; set => _exampleMode = value; } /// /// The data format mode for series data. /// public ChartJsonDatasMode DatasMode { get => _datasMode; set => _datasMode = value; } /// /// Whether to wrap JSON in API response envelope. /// public bool UseApiEnvelope { get => _useApiEnvelope; set => _useApiEnvelope = value; } /// /// Whether to automatically regenerate JSON when ExampleMode or DatasMode changes. /// public bool AutoGenerateJson { get => _autoGenerateJson; set => _autoGenerateJson = value; } private void Awake() { _bridge = GetComponent(); } /// /// Apply the current JSON content to the chart. /// public void ApplyJsonToChart() { if (_bridge == null) { _bridge = GetComponent(); } if (_bridge == null || _bridge.Profile == null) { Debug.LogWarning("[UGUIRuntimeJsonInjection] No UGUIChartBridge or ChartProfile found."); return; } if (string.IsNullOrWhiteSpace(_jsonContent)) { Debug.LogWarning("[UGUIRuntimeJsonInjection] JSON content is empty."); return; } string json = _jsonContent; // Try to extract data from API envelope if present if (ChartJsonUtils.TryExtractWrappedDataJson(json, out var dataJson) && !string.IsNullOrEmpty(dataJson)) { json = dataJson; } // Deserialize and apply JSON to profile if (!ChartJsonUtils.TryDeserializeFeed(json, out var feed)) { Debug.LogError("[UGUIRuntimeJsonInjection] Failed to parse JSON."); return; } // Clone profile on first injection to avoid modifying the original asset EnsureRuntimeProfile(); ChartJsonUtils.ApplyFeedToProfile(_runtimeProfile, feed); // Refresh the chart _bridge.Refresh(); Debug.Log("[UGUIRuntimeJsonInjection] JSON applied to chart."); } /// /// Ensures we have a runtime copy of the profile to avoid modifying the original asset. /// private void EnsureRuntimeProfile() { if (_profileCloned) return; var originalProfile = _bridge.Profile; if (originalProfile == null) return; // Create a runtime copy _runtimeProfile = Object.Instantiate(originalProfile); _runtimeProfile.name = originalProfile.name + " (Runtime)"; _runtimeProfile.hideFlags = HideFlags.DontSave; // Assign the cloned profile to the bridge _bridge.Profile = _runtimeProfile; _profileCloned = true; Debug.Log($"[UGUIRuntimeJsonInjection] Created runtime copy of profile: {_runtimeProfile.name}"); } /// /// Generate example JSON from the current chart profile. /// public void GenerateExampleJson() { if (_bridge == null) { _bridge = GetComponent(); } if (_bridge == null || _bridge.Profile == null) { Debug.LogWarning("[UGUIRuntimeJsonInjection] No UGUIChartBridge or ChartProfile found."); return; } var profile = _bridge.Profile; profile.EnsureRuntimeData(); string json = ChartJsonUtils.BuildInjectionJson(profile, profile.chartId, _exampleMode, _datasMode); _jsonContent = _useApiEnvelope ? ChartJsonUtils.WrapAsApiResponse(json) : json; } } }