当前位置:网站首页>Record the method of reading excel provided by unity and the solution to some pits encountered
Record the method of reading excel provided by unity and the solution to some pits encountered
2022-06-27 02:39:00 【Nokiagame studio】
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Excel;
using System.IO;
using UnityEngine.Networking;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
public class MoveCameraByMouse : MonoBehaviour
{
public Transform CenObj;// Surrounding objects
public float speed;// Rotation speed
public float speedup;// Speed of rise
public GameObject CubePre;// Generated block
float xuanzhaun=180;// The rotation angle when generating
public GameObject weizhi;// Where the block is generated
int gaodu = 2;// Initial block height
int shuliang;// Read file assignment count
public GameObject ui;// Generated ui
public GameObject flaggo;// Generated red flag
int toindex;// Read resource count
public Image[] tempImage;// Components
private Texture2D pic;
private Sprite[] sp;//??
public string[] diyihang;// Read excel Acceptance of the first line
public string[] dierhang;// The second line
public string[] disanhang;// The third line
public List<Texture> textureList;// Read all png
private void Awake()
{
InvokeRepeating("shengcheng", 1f, 2f);// Generate the Ctrip of the object
GameReadExcel(); // Read excel
}
void Start()
{
string[] filePath = Directory.GetFiles(Application.streamingAssetsPath,"*.png");// Read all png
StartCoroutine(LoadImage(filePath));
//StartCoroutine(LoadTextureFromInternet());
}
void Update()
{
if (toindex<=0)// prevent index Crossing the boundary
{
return;
}
transform.Rotate(Vector3.down*Time.deltaTime * speed, Space.World);// rotate
//transform.RotateAround(CenObj.position, Vector3.up, -30 * Time.deltaTime * speed);
transform.Translate(transform.up * Time.deltaTime * speedup);
xuanzhaun=xuanzhaun++;
}
// The distance and proximity of the lens
public void shengcheng()// Generate
{
if (toindex <= 0)
{
return;
}
gaodu = gaodu + 1;
sp=new Sprite[toindex];
GameObject go= Instantiate(CubePre, weizhi.transform.position , Quaternion.Euler(0,-90, 0));
go.GetComponent<Transform>().localScale = new Vector3(1f, gaodu, 1);
GameObject to = Instantiate(ui, new Vector3(go.transform.position.x,go.transform.position.y+(go.transform.position.y/2)-0.5f,go.transform.position.z-0.53f), Quaternion.Euler(0,0,0));
to.transform.parent = go.transform;
Text[] ziwuti=to.GetComponentsInChildren<Text>();
ziwuti[0].text = toindex.ToString();
Text[] ziwutiname = to.GetComponentsInChildren<Text>();
ziwutiname[1].text= diyihang[shuliang];
Image[] ziwutiico=to.GetComponentsInChildren<Image>();
//StartCoroutine(LoadTextureFromInternet());
ziwutiico[0].sprite = Sprite.Create((Texture2D)textureList[shuliang], new Rect(0, 0, textureList[shuliang].width, textureList[shuliang].height), Vector2.zero);
GameObject flag = Instantiate(flaggo, new Vector3(go.transform.position.x+0.2f, go.transform.position.y + (go.transform.position.y / 2)+1.5f, go.transform.position.z), Quaternion.Euler(0,-180 ,90));
flag.transform.parent = go.transform;
MeshRenderer mr = flag.GetComponent<MeshRenderer>();
mr.material.mainTexture = textureList[shuliang];
shuliang = shuliang + 1;
toindex = toindex - 1;
}
IEnumerator LoadImage(string[] filePath)// Read all png
{
foreach (var item in filePath)
{
UnityWebRequest request = UnityWebRequestTexture.GetTexture(item);
yield return request.SendWebRequest();
textureList.Add(DownloadHandlerTexture.GetContent(request));
}
}
//[System.Obsolete]
//IEnumerator LoadTextureFromInternet()
//{
// tempImage = new Image[toindex];
// for (int i = 0; i < toindex; i++)
// {
// UnityWebRequest request = new UnityWebRequest(Application.streamingAssetsPath +"/"+ dierhang[i]);
// DownloadHandlerTexture texture = new DownloadHandlerTexture(true);
// request.downloadHandler = texture;
// yield return request.Send();
// if (string.IsNullOrEmpty(request.error))
// {
// pic = texture.texture;
// }
// //tempImage = GameObject.Find("Image").GetComponent<Image>();
// sp = Sprite.Create((Texture2D)pic, new Rect(0, 0, pic.width, pic.height), Vector2.zero);
// tempImage[i].sprite = sp;
// //Debug.Log(dierhang[i]);
// //GameObject go = GameObject.Find("Cube");
// //go.GetComponent<MeshRenderer>().material.mainTexture = pic;
// }
//}
public void GameReadExcel()// Read excel
{
FileStream stream = File.Open(Application.streamingAssetsPath +"/config.xlsx", FileMode.Open, FileAccess.Read);
//IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
DataSet result = excelReader.AsDataSet();
toindex = result.Tables[0].Columns.Count;// Get the number of columns
int rows = result.Tables[0].Rows.Count;// Get the number of lines
// Start with the second line
//for (int i = 1; i < rows; i++)
//{
diyihang = new string[toindex];
dierhang = new string[toindex];
disanhang = new string[toindex];
for (int j = 0; j < toindex; j++)
{
diyihang[j] = result.Tables[0].Rows[1][j].ToString();
//Debug.Log(diyihang[j]);
}
//}
for (int j = 0; j < toindex; j++)
{
dierhang[j] = result.Tables[0].Rows[2][j].ToString();
//Debug.Log(dierhang);
}
for (int j = 0; j < toindex; j++)
{
disanhang[j] = result.Tables[0].Rows[3][j].ToString();
// Debug.Log(disanhang);
}
}
}
On the first code , edition 2020.3.26f1c1, First, notice the namespace , The second import link :https://pan.baidu.com/s/1N8tZ7nCg2Ak4LDplwYt3tQ?pwd=1234
Extraction code :1234
To Assets Decompress , modify 
118 If you don't download it here, go to the corresponding address of your installation package , The key point here is to remember !!! My address :C:\Program Files\Unity\Hub\Editor\2020.3.26f1c1\Editor\Data\MonoBleedingEdge\lib\mono\unityjit
Remember to remember to remember !!!
The code is messy , After all, I don't just do this function ! Again, this is just a record of the pit encountered , Choose the function you want , The function of this code is also relatively complete .
边栏推荐
猜你喜欢

TopoLVM: 基于LVM的Kubernetes本地持久化方案,容量感知,动态创建PV,轻松使用本地磁盘

学习太极创客 — MQTT(六)ESP8266 发布 MQTT 消息

执念斩长河暑期规划
![[array] sword finger offer II 012 The sum of left and right subarrays is equal | sword finger offer II 013 Sum of two dimensional submatrix](/img/e4/7bae2a109dcf5e2a8f032e73b89479.png)
[array] sword finger offer II 012 The sum of left and right subarrays is equal | sword finger offer II 013 Sum of two dimensional submatrix

P5.js death planet

Learn Tai Chi maker mqtt (IX) esp8266 subscribe to and publish mqtt messages at the same time

Yiwen teaches you Kali information collection

docker部署redis集群

canvas粒子篇之鼠标跟随js特效

Dameng database installation
随机推荐
Yalm 100b: 100billion parameter open source large model from yandex, Russia, allowing commercial use
流沙画模拟器源码
1、项目准备与新建
h5液体动画js特效代码
Flink学习4:flink技术栈
Lodash get JS code implementation
Oracle/PLSQL: Translate Function
ORM cache package for laravel
How does the C # TCP server limit the number of connections to the same IP?
Would rather go to 996 than stay at home! 24 years old, unemployed for 7 months, worse than work, no work
Learning Tai Chi Maker - mqtt Chapter 2 (II) esp8266 QoS application
Memcached basics 13
mmdetection 用yolox训练自己的coco数据集
2022茶艺师(高级)上岗证题库模拟考试平台操作
memcached基础12
XSS attack (note)
Docker deploy redis cluster
我是怎样简化开源系统中的接口的开发的?
执念斩长河暑期规划
Microsoft365开发人员申请