Skip to content

Instantly share code, notes, and snippets.

@jake1256
Created December 1, 2014 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jake1256/2c046b88391d30320c67 to your computer and use it in GitHub Desktop.
Save jake1256/2c046b88391d30320c67 to your computer and use it in GitHub Desktop.
【Unity】簡単明快なdrawcall削減の例 ref: http://qiita.com/kuuki_yomenaio/items/eaea133479bb8be96870
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class LoadText {
// Resources/text/から指定のテキストファイルを改行区切りで読む
public List<string> loadTextFile(string textFileName){
TextAsset textAsset = Resources.Load<TextAsset>("text/" + textFileName);
string[] strs = textAsset.text.Split ("\n" [0]);
foreach(string str in strs){
txtList.Add(str);
}
return txtList;
}
}
1,1.0,-2.0
1,2.3,-0.8
2,1.4,-1.6
2,1.8,-1.2
2,1.5,-0.6
2,1.0,-0.5
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MapCreate : MonoBehaviour {
// Inspectorでprefabを紐付け
public GameObject mapParent;
public GameObject map1;
public GameObject map2;
// Use this for initialization
void Start () {
// テキストファイルを読み出す
LoadText loadText = new LoadText ();
List<string> loadMapTextList = loadText.loadTextFile ("map_1");
// 文字列からMapDataを生成
List<MapData> mapDataList = new List<MapData> ();
foreach (string str in loadMapTextList) {
MapData map = new MapData();
map.parse(str);
mapDataList.Add(map);
}
createMap (mapDataList);
}
// MapDataからマスをポチポチ置いていく
private void createMap(List<MapData> mapList){
GameObject obj = null;
foreach (MapData map in mapList) {
switch(map.ImgId){
case 1:
obj = Instantiate(map1) as GameObject;
break;
case 2:
obj = Instantiate(map2) as GameObject;
break;
}
if(obj != null){
obj.transform.position = new Vector3(map.X , map.Y , 0);
obj.transform.parent = mapParent.transform;
obj = null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment