当前位置:网站首页>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 .
边栏推荐
- Flink学习5:工作原理
- Uninstallation of Dameng database
- JWT certification process and use cases
- pytorch 23 hook的使用与介绍 及基于hook实现即插即用的DropBlock
- ConstraintLayout(约束布局)开发指南
- Canvas particles: mouse following JS effect
- Summer planning for the long river
- Memcached basics 11
- 【数组】剑指 Offer II 012. 左右两边子数组的和相等 | 剑指 Offer II 013. 二维子矩阵的和
- 记录unity 自带读取excel的方法和遇到的一些坑的解决办法
猜你喜欢
mmdetection 用yolox训练自己的coco数据集
C language -- Design of employee information management system
元透实盘周记20220627
学习太极创客 — MQTT(七)MQTT 主题进阶
How does source insight (SI) display the full path? (do not display omitted paths) (turn off trim long path names with ellipses)
STM32入门介绍
h5液体动画js特效代码
[micro service sentinel] degradation rules slow call proportion abnormal proportion abnormal constant
svg拖拽装扮Kitty猫
流沙画模拟器源码
随机推荐
TopoLVM: 基于LVM的Kubernetes本地持久化方案,容量感知,动态创建PV,轻松使用本地磁盘
Laravel 的 ORM 缓存包
对数器
Fork (), exec (), waitpid (), $? > > in Perl 8 combination
Sword finger offer 𞓜: stack and queue (simple)
正则表达式:语法
Flink learning 4:flink technology stack
I earned 3W yuan a month from my sideline: the industry you despise really makes money!
pytorch 23 hook的使用与介绍 及基于hook实现即插即用的DropBlock
How does the brain do arithmetic? Both addition and subtraction methods have special neurons, and the symbol text can activate the same group of cell sub journals
Oracle/PLSQL: Translate Function
pytorch 22 8种Dropout方法的简介 及 基于Dropout用4行代码快速实现DropBlock
docker部署redis集群
Leetcode 785: judgment bipartite graph
455. distribute biscuits [distribution questions]
bluecms代码审计入门
mmdetection ValueError: need at least one array to concatenate解决方案
1. Project preparation and creation
解决cherry pick提交报错问题
2022中式面点师(高级)复训题库及在线模拟考试