You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.6 KiB
63 lines
1.6 KiB
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using UnityEngine.UI; |
|
using LitJson; |
|
|
|
public class BlockInit : MonoBehaviour { |
|
public List<BlockInstance> blockList = new List<BlockInstance>(); |
|
public List<TextAsset> jsonfiles; |
|
public Transform blockUIContent; |
|
public GameObject blockUIPreferb; |
|
|
|
void Start() |
|
{ |
|
initBlockListUI(); |
|
} |
|
|
|
public string ReadJson(TextAsset file) { |
|
return file.text; |
|
} |
|
|
|
//读取到的json文件转换成实体类 |
|
public List<BlockInstance> JsonToBlockInstance(string json) { |
|
JsonData blockMsgArray = JsonMapper.ToObject(json); |
|
List<BlockInstance> blockinsts = new List<BlockInstance>(); |
|
foreach (JsonData bjson in blockMsgArray) { |
|
//print(JsonMapper.ToObject<BlockInstance>(bjson).name); |
|
blockinsts.Add(JsonMapper.ToObject<BlockInstance>(bjson.ToJson())); |
|
} |
|
return blockinsts; |
|
} |
|
|
|
public void addToBlockList(List<BlockInstance> insts) { |
|
foreach (BlockInstance inst in insts) { |
|
blockList.Add(inst); |
|
} |
|
} |
|
|
|
public void refreshUI() { |
|
ClearTransfromChild(blockUIContent); |
|
foreach (BlockInstance block in blockList) |
|
{ |
|
GameObject blockUI = Instantiate(blockUIPreferb,blockUIContent); |
|
blockUI.GetComponentInChildren<Text>().text = block.name; |
|
} |
|
} |
|
public void ClearTransfromChild(Transform group) |
|
{ |
|
foreach (Transform TransformChild in group) |
|
{ |
|
Destroy(TransformChild.gameObject); |
|
} |
|
} |
|
|
|
public void initBlockListUI() { |
|
foreach(TextAsset jsonfile in jsonfiles) |
|
{ |
|
List<BlockInstance> instances = JsonToBlockInstance(jsonfile.text); |
|
addToBlockList(instances); |
|
} |
|
refreshUI(); |
|
} |
|
}
|
|
|