当前位置:网站首页>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 .
边栏推荐
- Test the respective roles of nohup and &
- h5液体动画js特效代码
- Dameng database installation
- [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
- 平均风向风速计算(单位矢量法)
- lottie.js创意开关按钮动物头像
- Fork (), exec (), waitpid (), $? > > in Perl 8 combination
- TopoLVM: 基于LVM的Kubernetes本地持久化方案,容量感知,动态创建PV,轻松使用本地磁盘
- Look! In June, 2022, the programming language ranking list was released! The first place is awesome
- memcached基础12
猜你喜欢

Don't be brainwashed. This is the truth about the wages of 90% of Chinese people

Introduction to stm32

谷歌开始卷自己,AI架构Pathways加持,推出200亿生成模型

Flink learning 3: data processing mode (stream batch)

学习太极创客 — MQTT 第二章(三)保留消息

【一起上水硕系列】Day 6

Parameter estimation -- Chapter 7 study report of probability theory and mathematical statistics (point estimation)

Flink Learning 2: Application Scenarios

Is the division of each capability domain of Dama, dcmm and other data management frameworks reasonable? Is there internal logic?

What if asreml-r does not converge in operation?
随机推荐
Microsoft365开发人员申请
C language -- Design of employee information management system
TopoLVM: 基于LVM的Kubernetes本地持久化方案,容量感知,动态创建PV,轻松使用本地磁盘
Flink学习5:工作原理
SQLite reader plug-in tests SQLite syntax
Flink Learning 2: Application Scenarios
Learn Tai Chi Maker - mqtt (VI) esp8266 releases mqtt message
jwt的认证流程和使用案例
超级详细,2 万字详解,吃透 ES!
Yalm 100b: 100billion parameter open source large model from yandex, Russia, allowing commercial use
Oracle/PLSQL: Upper Function
Paddlepaddle 21 is implemented based on dropout with 4 lines of code droplock
Memcached basics 13
Flink learning 3: data processing mode (stream batch)
2022中式面点师(高级)复训题库及在线模拟考试
Docker deploy redis cluster
dat.gui.js星星圆圈轨迹动画js特效
使用命令行安装达梦数据库
人群模拟
Precautions for using sneakemake