当前位置:网站首页>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  If you don't modify it, you will report an error
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 .

原网站

版权声明
本文为[Nokiagame studio]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270230388303.html