public static MapObjConfManager GetEditorMapObjConfManager() { if (editorMapObjConfManager == null) { editorMapObjConfManager = new MapObjConfManager(); editorMapObjConfManager.Load(); } return(editorMapObjConfManager); }
public static void CreatePrefabs() { //获取场景中全部道具 Object[] objects = Object.FindObjectsOfType(typeof(GameObject)); GameObject prefabListObj = null; foreach (GameObject sceneObject in objects) { if (sceneObject.name == "prefabs") { prefabListObj = sceneObject; break; } } if (prefabListObj != null) { DestroyImmediate(prefabListObj); } prefabListObj = new GameObject("prefabs"); //==========================生成预制体================================ List <MapObjConf> confs = ConfigManager.GetEditorMapObjConfManager().datas; MapObjConfManager confMgr = ConfigManager.GetEditorMapObjConfManager(); UnityEngine.Object[] arr = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.TopLevel); //for (int i = 0; i < arr.Length; i++) //{ // string path = AssetDatabase.GetAssetPath(arr[i]); // Debug.Log(path); //} //GameObject[] gos = Selection.gameObjects; Material mat = Resources.Load <Material>("Materials/item_obj"); for (int i = 0; i < arr.Length; i++) { string path = AssetDatabase.GetAssetPath(arr[i]); Debug.Log(path); int indexnum = path.LastIndexOf('/') + 1; string prefabFullName = path.Substring(indexnum, path.Length - indexnum); Debug.Log(prefabFullName); indexnum = prefabFullName.LastIndexOf('.') + 1; string prefabName = prefabFullName.Substring(0, indexnum - 1); Debug.Log(prefabName); MapObjConf conf = confMgr.GetConfByPrefabName(prefabName); if (conf == null) { Debug.LogError(prefabName + " config is null!!"); continue; } GameObject gameObj = AssetDatabase.LoadAssetAtPath <GameObject>("Assets/Resources/" + conf.path + ".prefab"); if (gameObj != null) { continue; } Debug.Log("create " + prefabName + " prefab !"); //创建gameobject GameObject prefabObj = new GameObject(prefabName); InGameBaseMapObj mapobj = prefabObj.AddComponent <InGameBaseMapObj>(); mapobj.confid = conf.id; //加载sprite Sprite sprite = AssetDatabase.LoadAssetAtPath <Sprite>(path); if (sprite == null) { Debug.LogError(prefabName + " sprite is null !!!!"); continue; } //创建spriterenderer GameObject spriteObj = new GameObject("sprite"); SpriteRenderer sr = spriteObj.AddComponent <SpriteRenderer>(); spriteObj.transform.parent = prefabObj.transform; sr.sprite = sprite; spriteObj.transform.localScale = new Vector3(2.2f, 2.2f, 1f); spriteObj.transform.localPosition = new Vector3(0f, 0f, 0f); spriteObj.GetComponent <Renderer>().material = mat; //创建预制体 PrefabUtility.CreatePrefab("Assets/Resources/" + conf.path + ".prefab", prefabObj); //生成预制体物体 //GameObject tempObj = Resources.Load(conf.path) as GameObject; GameObject pre = AssetDatabase.LoadAssetAtPath("Assets/Resources/" + conf.path + ".prefab", typeof(GameObject)) as GameObject; pre = PrefabUtility.InstantiatePrefab(pre) as GameObject; //Debug.Log("create : " + prefabName); pre.transform.parent = prefabListObj.transform; DestroyImmediate(prefabObj); } }
public void ReloadScene(DataStream datastream) { Debug.Log("ReloadScene"); LevelOption me = null; List <GameObject> delarr = new List <GameObject>(); foreach (GameObject sceneObject in Object.FindObjectsOfType(typeof(GameObject))) { if (sceneObject.name == "MapEditor") { me = sceneObject.GetComponent <LevelOption>(); me.deserialize(datastream); GameCommon.GAME_DATA_VERSION = me.version; continue; } else if (sceneObject.name != "Main Camera" && sceneObject.name != "Directional Light") { delarr.Add(sceneObject); } } //创建关卡配置物体 if (me == null) { GameObject tGO = new GameObject("MapEditor"); me = tGO.AddComponent <LevelOption>(); me.deserialize(datastream); MapEditor me1 = tGO.AddComponent <MapEditor>(); me1.lo = me; } Debug.Log(me.levelName + " / " + me.version); foreach (GameObject obj in delarr) { DestroyImmediate(obj); } int objcount = datastream.ReadSInt32(); Debug.Log("objcount : " + objcount); Dictionary <int, InGameBaseObj> dic = new Dictionary <int, InGameBaseObj>(); MapObjConfManager mapObjConfManager = ConfigManager.GetEditorMapObjConfManager(); for (int i = 0; i < objcount; i++) { //MSBaseObject.CreateObj(datastream); //从字节流中获取id int confid = 0; float x = 0, y = 0, sx = 1, sy = 1; MapObjConf conf = null; GameObject go = null, tempObj; InGameBaseObj baseObj = null; int dataid = datastream.ReadByte(); while (dataid != 0) { switch (dataid) { case 1: confid = datastream.ReadSInt32(); conf = mapObjConfManager.map[confid]; tempObj = Resources.Load(conf.path) as GameObject; if (tempObj == null) { Debug.Log(confid + " is null!"); } go = (GameObject)Instantiate(tempObj); baseObj = go.GetComponent <InGameBaseObj>(); break; case 2: x = datastream.ReadSInt32() / 1000f; break; case 3: y = datastream.ReadSInt32() / 1000f; break; case 4: int parentid = datastream.ReadSInt32(); if (dic.ContainsKey(parentid)) { go.transform.parent = dic[parentid].transform; } else { go.transform.parent = me.transform; } break; case 5: int instanceid = datastream.ReadSInt32(); dic.Add(instanceid, baseObj); break; case 7: go.name = datastream.ReadString16(); break; case 8: sx = datastream.ReadSInt32() / 1000f; break; case 9: sy = datastream.ReadSInt32() / 1000f; break; case 6: baseObj.Deserialize(datastream); break; } dataid = datastream.ReadByte(); } go.transform.position = new Vector3(x, y); go.transform.localScale = new Vector3(sx, sy, 1); GameCommon.SetObjZIndex(go, conf.depth); } }