using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;
using ZenFulcrum.EmbeddedBrowser;
using static 网页测试;
public class PieText : PieHtml
{
private List<PieData> list = new List<PieData>();
private const string url = "localGame://Pie.html";
private void Awake()
{
base.Init();
base.Browser.Url = url;
}
public void Start()
{
Debug.Log($"++++");
Browser.ShowDevTools();
Browser.RegisterFunction("testAgain", (JSONNode jk) =>
{
Debug.Log(jk.AsJSON);
Debug.Log($"++++:{jk.AsJSON}");
//var Result = "[{\"name\":\"张三\",\"value\":10}]";
var Result = jk.AsJSON.ToString();
var cList = JsonConvert.DeserializeObject<List<JsonTexts>>(Result);
var obj = cList.First().name;
Debug.Log($"{obj}");
});
}
public void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
Debug.Log($"A");
if (list.Count != 0)
{
list.Clear();
}
list.Add(new PieData() { name = "张三", value = 10f });
list.Add(new PieData() { name = "李四", value = 90f });
base.AddSeries(list);
}
if (Input.GetKeyDown(KeyCode.C))
{
base.ClearSeries();
}
}
}
public class JsonTexts
{
public string name;
public string value;
}
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using ZenFulcrum.EmbeddedBrowser;
public class PieHtml : MonoBehaviour
{
public Browser Browser { get; private set; }
public void Init()
{
Browser = this.GetComponent<Browser>();
}
public void ClearSeries()
{
Browser.CallFunction("UnityToHtmlClear").Done();
}
public void AddSeries(List<PieData> list)
{
Browser.CallFunction("UnityToHtml", JsonConvert.SerializeObject(list)).Done();
}
public class PieData
{
public string name { get; set; }
public float value { get; set; }
}
}
Unity pc端内嵌网页插件Embedded Browser基本使用流程以下是使用插件的地址
(https://blog.csdn.net/a497507718/article/details/131083922)
Html 代码
<!DOCTYPE html>
<html lang="zh-CN" style="height: 100%">
<head>
<meta charset="utf-8">
</head>
<body style="height: 100%; margin: 0">
<div id="container" style="height: 100%"></div>
<script type="text/javascript" src="echarts/dist/echarts.min.js"></script>
<div class="left">
<button type="button" onclick="test()">15栋教学楼</button>
</div>
<script type="text/javascript">
var dom = document.getElementById('container');
var myChart = echarts.init(dom, null, {
renderer: 'canvas',
useDirtyRect: false
});
var app = {};
var option;
var mdata;
function UnityToHtmlClear() {
myChart.clear();
}
function test() {
var a = 21
// test1("15");
testAgain(mdata)
return a
}
function testAgain(item) {
console(item);
}
//Unity
function UnityToHtml(item, unityColor) {
myChart.clear();
mdata = item;
//data = genData(60);
option = {
tooltip: {
trigger: 'item',
formatter: '{a} <br/>{b} : {c} ({d}%)',
},
color: ['#ffffff', '#5470c6', '#91cd77', '#ef6567', '#f9c956', '#75bedc'], //饼图颜色
textStyle: {
color: '#f0f',
fontSize: 30
},
series: [{
label: {
color: '#f0f',
formatter: '{b}{c}',
},
name: '姓名',
type: 'pie',
radius: '50%',
center: ['50%', '50%'],
data: JSON.parse(item),
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}]
};
myChart.setOption(option);
// console.log("js接受到参数", item);
console.log(JSON.stringify(item));
}
if (option && typeof option === 'object') {
myChart.setOption(option);
}
myChart.on('mousedown', function (params) {
var dataIndex = params.dataIndex;
var arr = JSON.parse(mdata);
var clickedData = arr[dataIndex];
console.warn('点击参数:', params);
console.warn('点击后的数据:', arr, arr[dataIndex]);
testAgain(arr[dataIndex]);
});
window.addEventListener('resize', myChart.resize);
</script>
</body>
</html>
以下是<script type="text/javascript" src="echarts/dist/echarts.min.js"></script> 包链接
链接:https://pan.baidu.com/s/1hUeeU-kSg0vvM3p1jIuABg?pwd=24ur
提取码:24ur
--来自百度网盘超级会员V6的分享