最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity 讀取文件 TextAsset讀取配置文件方式

 更新時間:2021年04月13日 14:41:02   作者:gd_2015  
這篇文章主要介紹了Unity 讀取文件 TextAsset讀取配置文件的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

1 支持文件類型

.txt

.html

.htm

.xml

.bytes

.json

.csv

.yaml

.fnt

2 尋找文件

1 //Load texture from disk

        TextAsset bindata= Resources.Load("Texture") as TextAsset;
        Texture2D tex = new Texture2D(1,1);
        tex.LoadImage(bindata.bytes); 

2 直接在編輯器中賦值

        public TextAsset textFile;

3 配置文件通常分行配置屬性

例如:

英雄名稱,等級,生命,攻擊

        hero1,1,1,1
        hero2,1,1,1
    string[] lines = textFile.text.Split("\n"[0]); 可以讀出屬性
        lines[0] = "英雄名稱,等級,生命,攻擊"
        lines[1] = "hero1,1,1,1"
        lines[2] = "hero2,1,1,1"

然后可以讀出每條數(shù)據(jù)的具體屬性

        for (int i = 0; i < lines.Length - 2; i++) {
            string[] parts = lines[i + 1].Split(","[0]);
        }
    parts[0] = "hero1" parts[1] = "1" parts[2] = "1" parts[2] = "1

補充:Unity加載TextAsset中的內(nèi)容為空

需求需要從Resources目錄下加載json文件,于是在目錄下創(chuàng)建了個txt文本,然后修改后綴名為.Json,

用Resource.Load<TextAsset>() 發(fā)現(xiàn)TextAsset.text竟然為空,里面什么都沒有,以為是.Json首字母大寫的緣故,于是改為.json,結(jié)果還沒有用,

新開工程卻發(fā)現(xiàn)用腳本生成的json文件卻是可以獲取到的,再仔細(xì)檢查發(fā)現(xiàn)選中json文件的時候?qū)傩悦姘寰谷皇裁炊紱]顯示出來,于是懷疑json文件不正確沒有被unity識別到,用vs高級保存選項發(fā)現(xiàn)文本竟然是GBK2312格式,改為UTF-8格式發(fā)現(xiàn)可以顯示出來并能讀取到text。

于是懷疑是創(chuàng)建txt文本時默認(rèn)編碼格式不正確,發(fā)現(xiàn)默認(rèn)編碼格式是ASCII格式,看來以后創(chuàng)建json的時候更要注意編碼格式的問題了。

補充:Unity 簡易讀取配置文件內(nèi)容(txt,懶人用)

翻以前別人寫的一個項目,看到讀取本地配置文件的,目前雖然不太懂,但是先放著,估摸以后牛B了就看懂了

首先自定義一個config類:

using UnityEngine;
using System.Collections;
using System;
[Serializable] //一定要有,同時不能繼承MonoBehaviour
public class Config  {
    public int Num1; //我文檔里有2個int,2個string類型
    public int Num2;
    public string String1;
    public string String2;
    // Use this for initialization
    void Start () {
}
    private static Config _data;
    public static Config _Data
    {
        get
        {
            if (_data == null)
            {
                string json = System.IO.File.ReadAllText(Application.streamingAssetsPath + "/test.txt");
                _data = JsonUtility.FromJson<Config>(json);
            }
            return _data;
        }
    }   
}

然后在unity 的StreamingAssets文件夾下創(chuàng)建個test.txt

里面內(nèi)容:(一定要有花括號)

{"Num1":30,"Num2":60,"String1":"aaa","String2":"bbb"}

然后再其他腳本里,直接調(diào)用就好了

例如:

using UnityEngine;
using System.Collections;
public class LoadConfig : MonoBehaviour {
// Use this for initialization
void Start () {
        print(Config._Data.Num1);
        print(Config._Data.String2);
    }
// Update is called once per frame
void Update () {
}
}

2017.3.27更新:自己又理解了一點

如果要讀取網(wǎng)頁某天氣信息,但是不止一個花括號,分了幾層,需要怎么做?

例如信息:

{"weatherinfo":{"city":"北京","cityid":"101010100","temp":"18","WD":"東南風(fēng)","WS":"1級","SD":"17%","WSE":"1","time":"17:05","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暫無實況","qy":"1011","rain":"0"}}

上面的就不多復(fù)述,大概思路就是再建立一個類保存二級信息

Config類修改如下(之前的全部刪除):

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
[Serializable]  //一定要有,同時不能繼承MonoBehaviour
public class Config  {
    public Weatherinfo weatherinfo; //這里的weatherinfo 就是上面信息的第一層weatherinfo,建立父級保存信息(名字要對應(yīng)天氣的weatherinfo)
}
[Serializable]
public class Weatherinfo //weatherinfo下的信息保存,類里的city 對應(yīng)信息里的city,一次類推,我就寫2個了;
{
    public string city;
    public int cityid;
}

再新建一個名字為LoadWWW的腳本,用于讀取網(wǎng)絡(luò)的數(shù)據(jù):

using UnityEngine;
using System.Collections;
using System;
public class LoadWWW : MonoBehaviour {
    Config _config = new Config();
// Use this for initialization
void Start () {
        StartCoroutine("load");
        Invoke("LoadMessage", 1f);
}
// Update is called once per frame
void Update () {
}
    IEnumerator load()
    {
        WWW w = new WWW("http://www.weather.com.cn/data/sk/101010100.html");//加載某網(wǎng)頁數(shù)據(jù),根據(jù)自己需求改
        yield return w;
        string json = w.text; 
        print(json);
        _config = JsonUtility.FromJson<Config>(json);       
    }
    void LoadMessage()
    {
        print(_config.weatherinfo.city);
        print(_config.weatherinfo.cityid);
    }
}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

最新評論

会同县| 谢通门县| 钦州市| 张北县| 沐川县| 罗定市| 习水县| 洛扎县| 芒康县| 仁布县| 紫云| 罗甸县| 贵定县| 宁乡县| 安义县| 杭锦旗| 樟树市| 台湾省| 渭源县| 通山县| 金平| 万州区| 曲周县| 望城县| 阿荣旗| 许昌市| 屯门区| 鹤山市| 康平县| 开封市| 德庆县| 铁力市| 夏津县| 噶尔县| 紫阳县| 昌图县| 凉城县| 长海县| 新河县| 赫章县| 织金县|