xml文件自己建立放置到streamingAsset下
需要添加org.in2bits.myxls 网上可以下载此处不提供(另外如果出错需要改一下 版本file=》buildsetting=》Playersetting=》.net 2.0)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;
public class Player_four : MonoBehaviour {
public static Player_four instance;
public Transform _rightHand;
public SteamVR_TrackedObject _rightTrackedObj;
private bool rightshexian = false;
//右手激光
private Transform _rightLineR;
public Transform _rightModel;
public Ray _rightRay { get { return new Ray(_rightModel.position, _rightModel.up); } }
public RaycastHit _rightHit;
private bool leftui = false;
public Transform _leftHand;
public SteamVR_TrackedObject _leftTrackedObj;
//左手激光
public LineRenderer leftjiguang;
public Transform _leftModel;
public Ray _leftRay { get { return new Ray(_leftModel.position, _leftModel.forward); } }
public RaycastHit _leftHit;
public SteamVR_Controller.Device rightDevice;
public SteamVR_Controller.Device leftDevice;
public float lastAngle;
public float currentAngle;
public float righthanddistance = 10;
//场景内ui选择
public GameObject xuanzeui;
public List<GameObject> newuichose = new List<GameObject>();
public bool stopandnet;
////模式选择 考核为true,练习为false
//public static bool moshixuanze;
Exceldemo obj;
public static List<Exceldemo> listInfos;
//路径
public string xmlpath;
string excelAddrespath;
//计时器
string str = null;
static float time;
static float second;
static float hour;
static float minute;
//练习ui
public GameObject[] lianxiuiArr;
void Start()
{
xmlpath = Application.streamingAssetsPath + "/XMLFile1.xml";
Debug.Log(xmlpath);
ExcelMakerManager.CreateExcelMakerManager();
listInfos = new List<Exceldemo>();
for (int j = 0; j < xuanzeui.transform.childCount; j++)
{
xuanzeui.transform.GetChild(j).gameObject.SetActive(false);
newuichose.Add(xuanzeui.transform.GetChild(j).gameObject);
}
//实验结果 更换图片
instance = this;
_rightHand = transform.Find("Controller (right)");
_rightTrackedObj = _rightHand.GetComponent<SteamVR_TrackedObject>();
//右手射线
_rightModel = _rightHand.Find("right");
//_rightLineR = _rightModel.Find("right");
_rightModel.gameObject.SetActive(false);
_leftHand = transform.Find("Controller (left)");
_leftTrackedObj = _leftHand.GetComponent<SteamVR_TrackedObject>();
leftjiguang = _leftHand.GetComponent<LineRenderer>();
}
void Update()
{
PlayerInput();
//射线更新
_rightModel.transform.localScale = new Vector3(0.05f, righthanddistance * 10.1f, 0.05f);
//-------------------------------------------------
time += Time.deltaTime;
if (time >= 1)
{
second++;
time -= 1;
}
if (second >= 60)
{
minute++;
second -= 60;
}
if (minute >= 60)
{
hour++;
minute -= 60;
}
str = string.Format("{0}时:{1}分:{2}秒", hour, minute, second);
//----------------------
}
public void PlayerInput()
{
try
{
rightDevice = SteamVR_Controller.Input((int)_rightTrackedObj.index);
leftDevice = SteamVR_Controller.Input((int)_leftTrackedObj.index);
//右手显示射线
if (rightDevice.GetPressDown(SteamVR_Controller.ButtonMask.Grip))
{
if (rightshexian == false)
{
_rightModel.gameObject.SetActive(true);
}
else
{
_rightModel.gameObject.SetActive(false);
}
rightshexian = !rightshexian;
}
//右手检测
if (Physics.Raycast(_rightRay, out _rightHit, Mathf.Infinity))
{
//sheixianjuli
righthanddistance = Vector3.Distance(_rightModel.position, _rightHit.point);
Debug.DrawLine(_rightModel.position, _rightHit.point);
Interactiable inter = _rightHit.transform.GetComponent<Interactiable>();
if (rightDevice.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
{
if (inter != null)
{
inter.Interactive();
}
}
}
//设置选择UI
if (leftDevice.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
{
print("菜单");
if (leftui == false)
{
newuichose[0].SetActive(true);
}
else
{
newuichose[0].SetActive(false);
}
leftui = !leftui;
}
}
catch (Exception e)
{
// Debug.Log(e.Message);
}
}
string info;
bool dayin;
private void OnGUI()
{
GUI.Label(new Rect(0, 0, 500, 20), str);
GUI.Label(new Rect(0, 80, 500, 20), info);
if (GUI.Button(new Rect(0, 120, 150, 20), "点击打印excel"))
{
Test();
}
if (GUI.Button(new Rect(0, 160, 150, 20), "点击清理数据"))
{
Delegate();
}
}
public void Test()
{
try
{
XmlDocument document = new XmlDocument();
document.Load(xmlpath);
//获取根节点
XmlNode rootNode = document.SelectSingleNode("Root");
//把字符串加载到对象中
XmlNodeList nodelist = rootNode.ChildNodes;
Debug.Log(nodelist.Count);
//if (dayin == false)
//{
// Debug.Log(ConfigManager.booscfgDic.Count);
for (int i = 0; i < nodelist.Count; i++)
{
XmlElement element = nodelist[i] as XmlElement;
Debug.LogFormat("姓名{0}学号{1}c操作分数{2}答题分数{3}总分数{4}", element.GetAttribute("name"), element.GetAttribute("password"), int.Parse(element.GetAttribute("caozuoscores")), int.Parse(element.GetAttribute("datiscores")), int.Parse(element.GetAttribute("ScoresAll")));
obj = new Exceldemo();
obj.name = element.GetAttribute("name");
obj.password = element.GetAttribute("password");
obj.caozuoscores = int.Parse(element.GetAttribute("caozuoscores"));
obj.datiscores = int.Parse(element.GetAttribute("datiscores"));
obj.ScoresAll = int.Parse(element.GetAttribute("ScoresAll"));
Debug.Log("222");
listInfos.Add(obj);
}
if (nodelist.Count > 0)
{
PrintExcel();
info = "已打印,请在D盘下查看!";
}
if (nodelist.Count == 0)
{
info = "别费劲了!没有数据怎么打印?";
}
}
catch (Exception e)
{
info = "e.Message";
Debug.Log(e.Message);
}
}
//清除数据
public void Delegate()
{
try
{
XmlDocument document = new XmlDocument();
document.Load(xmlpath);
//获取根节点
XmlNode rootNode = document.SelectSingleNode("Root");
//把字符串加载到对象中
XmlNodeList nodelist = rootNode.ChildNodes;
Debug.Log("源文件");
Debug.Log(nodelist.Count);
foreach (XmlNode node in nodelist)
{
//XmlElement element = node as XmlElement;
//Debug.Log(element.Name);
rootNode.RemoveChild(node);
}
if (nodelist.Count != 0)
{
Delegate();
}
if (nodelist.Count == 0)
{
info = "已清理,干干净净的!";
document.Save(xmlpath);
}
}
catch (Exception e)
{
info = e.Message;
Debug.Log(e.Message);
}
}
//打印表格
public void PrintExcel()
{
excelAddrespath = @"D:\" + "/Excel_" + System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".xls";
ExcelMakerManager.eInstance.ExcelMaker(excelAddrespath, listInfos);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using org.in2bits.MyXls;
public class Exceldemo
{
public string name;
public string password;
public int caozuoscores;
public int datiscores;
public int ScoresAll;
};
public class ExcelMakerManager
{
//单例
public static ExcelMakerManager eInstance;
public static ExcelMakerManager CreateExcelMakerManager()
{
if (eInstance == null)
{
eInstance = new ExcelMakerManager();
}
return eInstance;
}
XlsDocument xls = new XlsDocument();//新建一个xls文档
//链表为 物体信息 .
public void ExcelMaker(string name, List<Exceldemo> listInfo)
{
xls.FileName = name;// @"D:\tests.xls";//设定文件名
//Add some metadata (visible from Excel under File -> Properties)
xls.SummaryInformation.Author = "fan"; //填加xls文件作者信息
xls.SummaryInformation.Subject = "test";//填加文件主题信息
string sheetName = "Sheet0";
Worksheet sheet = xls.Workbook.Worksheets.AddNamed(sheetName);//填加名为"chc 实例"的sheet页
Cells cells = sheet.Cells;//Cells实例是sheet页中单元格(cell)集合
int rowNum = listInfo.Count;
int rowMin = 1;
int row = 0;
for (int x = 0; x < rowNum + 1; x++)
{
if (x == 0)
{
//根据具体的物体信息 .需要重新写
cells.Add(1, 1, "名字");
cells.Add(1, 2, "学号");
cells.Add(1, 3, "操作分数");
cells.Add(1, 4, "答题分数");
cells.Add(1, 5, "总分数");
}
else
{
cells.Add(rowMin + x, 1, listInfo[row].name);
cells.Add(rowMin + x, 2, listInfo[row].password);
cells.Add(rowMin + x, 3, listInfo[row].caozuoscores);
cells.Add(rowMin + x, 4, listInfo[row].datiscores);
cells.Add(rowMin + x, 5, listInfo[row].ScoresAll);
row++;
}
}
xls.Save();
}
}