public static void SetObjPos(GameObject obj, Vector3 pos) { InGameBaseObj ingameobj = obj.GetComponent <InGameBaseObj>(); if (ingameobj == null) { return; } if (ingameobj.GetObjType() != InGameBaseObj.enObjType.map) { return; } Vector2 mappos = GameCommon.GetMapPos(pos); obj.transform.position = GameCommon.GetWorldPos(mappos); MapObjConf conf = ConfigManager.GetEditorMapObjConfManager().map[ingameobj.confid]; if (conf == null) { return; } GameCommon.SetObjZIndex(obj, conf.depth); return; //MapEditor me = GameObject.Find("levelOption").transform.GetComponent<MapEditor>(); //if (me == null) //{ // EditorUtility.DisplayDialog("ERROR", "Cant't find the 'levelOption' !!", "ENTRY"); // return; //} }
//从池里取出一个物体 protected InGameBaseMapObj GetPoolObj(MapObjConf conf) { InGameBaseMapObj obj = null; if (!objPool.ContainsKey(conf.id) || objPool[conf.id].Count <= 0) { } else { List <InGameBaseMapObj> objlise = objPool[conf.id]; for (int i = 0; i < objlise.Count; i++) { obj = objlise[i]; if (obj.isAction) { continue; } objlise.RemoveAt(i); mapObjActionList.Add(obj); return(obj); } } return(CreateNewObj(conf)); }
public MapPointObj(MapObjConf conf, InGameBaseMapObj obj, Vector2 pos, Vector3 scale) { this.conf = conf; this.obj = obj; this.pos = pos; this.scale = scale; }
public void Load() { if (datas != null) { datas.Clear(); } datas = ConfigManager.Load <MapObjConf>(); dic.Clear(); for (int i = 0; i < datas.Count; i++) { MapObjConf obj = datas[i]; dic.Add(obj.id, obj); if (!dicByType.ContainsKey(obj.type)) { List <MapObjConf> typelist = new List <MapObjConf>(); typelist.Add(obj); dicByType.Add(obj.type, typelist); } else { dicByType[obj.type].Add(obj); } obj.SetPropertyvaluse(); } }
// public override bool Hurt(InGameBaseObj obj) { MapObjConf conf = ConfigManager.mapObjConfManager.map[this.confid]; InGameManager.GetInstance().inGameLevelManager.gameMap.DelMapObj(transform.position, conf); transform.position = new Vector3(-9999, -9999, 0); return(true); }
protected void SetGroupPoint(Vector2 pos, MapObjConf objConf, Vector3 scale) { int x = (int)pos.x, y = (int)pos.y; if (map[x, y] == null) { map[x, y] = new InGameMapPointData(MazeCreate.PointType.wall, pos); } map[x, y].AddObj(new MapPointObj(objConf, null, pos, scale)); SetWayProperty(pos, objConf); }
InGameBaseMapObj CreateNewObj(MapObjConf conf) { GameObject column = (GameObject)Resources.Load(conf.path); if (column == null) { Debug.LogError(conf.path + " is null!!!!"); return(null); } GameObject insobj = MonoBehaviour.Instantiate(column); insobj.transform.parent = mapObj.transform; InGameBaseMapObj obj = insobj.GetComponent <InGameBaseMapObj>(); return(obj); }
public void Load() { if (datas != null) { datas.Clear(); } datas = ConfigManager.Load <MapObjConf>(); groupMap.Clear(); map.Clear(); prefabNameMap.Clear(); for (int i = 0; i < datas.Count; i++) { MapObjConf obj = datas[i]; map.Add(obj.id, obj); if (!groupMap.ContainsKey(obj.group)) { groupMap.Add(obj.group, new Dictionary <int, List <MapObjConf> >()); } if (!groupMap[obj.group].ContainsKey(obj.sizeX)) { groupMap[obj.group].Add(obj.sizeX, new List <MapObjConf>()); } groupMap[obj.group][obj.sizeX].Add(obj); string[] names = obj.path.Split('/'); string _n = names[names.Length - 1]; if (!prefabNameMap.ContainsKey(_n)) { prefabNameMap.Add(_n, obj); } else { Debug.Log(_n); } } }
protected virtual void CreateGround(Vector3 v, int group, int scale) { if (!ConfigManager.mapObjConfManager.groupMap[group].ContainsKey(scale)) { return; } List <MapObjConf> list = ConfigManager.mapObjConfManager.groupMap[group][scale]; MapObjConf conf = list[Random.Range(0, list.Count)]; //GameObject column = (GameObject)Resources.Load(conf.path); //column = MonoBehaviour.Instantiate(column); //Vector3 worldpos = GameCommon.GetWorldPos(v); //column.transform.position = worldpos ; //GameCommon.SetObjZIndex(column, 0); //column.transform.parent = mapObj.transform; map[(int)v.x, (int)v.y].AddObj(new MapPointObj(conf, null, v, new Vector3(1, 1, 1))); }
static void OnSceneGUI(SceneView sceneView) { if (Event.current.type == EventType.MouseDown) { mouseStartDownpos = Event.current.mousePosition; return; } if (Event.current.type == EventType.MouseUp) { if (Vector3.Distance(mouseStartDownpos, Event.current.mousePosition) > 0.3f) { return; } //Event.current.Use(); if (Selection.gameObjects.Length <= 0) { return; } GameObject obj = Selection.gameObjects[0]; //Debug.Log(obj.name); InGameBaseMapObj mapObj = obj.GetComponent <InGameBaseMapObj>(); if (mapObj == null) { return; } MapObjConf conf = ConfigManager.GetEditorMapObjConfManager().map[mapObj.confid]; GameObject tempObj = Resources.Load(conf.path) as GameObject; tempObj = (GameObject)Instantiate(tempObj); tempObj.transform.position = GetWorldPosition(sceneView); tempObj.transform.parent = obj.transform.parent; GameCommon.SetObjZIndex(tempObj, conf.depth); //聚焦到当前物体 Selection.activeGameObject = tempObj; copyObj = tempObj; //Event.current.Use(); } }
//从地图中删除一个障碍 public void DelMapObj(Vector3 pos, MapObjConf conf) { Vector2 startMapPos = GameCommon.GetMapPos(pos); int startX = (int)startMapPos.x; int startY = (int)startMapPos.y; InGameMapPointData data = map[startX, startY]; for (int i = 0; i < data.objList.Count; i++) { MapPointObj mapPointObj = data.objList[i]; if (mapPointObj.obj == null) { if (mapPointObj.conf.id == conf.id) { AddPoolObj(conf.id, mapPointObj.obj); data.objList.RemoveAt(i); if (map[startX, startX].type == MazeCreate.PointType.wallfull) { continue; } if (mapPointObj.conf.depth >= 3) { map[startX, startX].type = MazeCreate.PointType.wallfull; astarArray[startX, startX] = 0; } else { map[startX, startX].type = MazeCreate.PointType.way; astarArray[startX, startX] = 1; } } } } }
protected void SetWayProperty(Vector2 pos, MapObjConf objconf) { int x = (int)pos.x, y = (int)pos.y; MazeCreate.PointType type; int arrval = 0; if (objconf.depth >= 3) { type = MazeCreate.PointType.wallfull; arrval = 0; } else { type = MazeCreate.PointType.way; arrval = 1; } for (int i = 0; i < objconf.sizeX; i++) { for (int j = 0; j < objconf.sizeY; j++) { int _x = x + i; int _y = y + j; if (map[_x, _y] == null) { map[_x, _y] = new InGameMapPointData(MazeCreate.PointType.wall, new Vector2(_x, _y)); } if (map[_x, _y].type == MazeCreate.PointType.wallfull) { continue; } map[_x, _y].type = type; astarArray[_x, _y] = arrval; } } }
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); } }
protected void InsertGroup(Vector2 pos, int groupid) { Debug.Log(groupid); int x = (int)pos.x, y = (int)pos.y; MapGroupConf conf = ConfigManager.mapGroupConfManager.dataMap[groupid]; if (conf == null) { return; } TextAsset text = Resources.Load(conf.path) as TextAsset; byte[] data = GameCommon.UnGZip(text.bytes); DataStream datastream = new DataStream(data, true); //配置LevelOption GameObject logo = new GameObject(conf.path); //logo.transform.parent = this.mapObj.transform; logo.transform.position = GameCommon.GetWorldPos(pos); LevelOption me = logo.AddComponent <LevelOption>(); me.deserialize(datastream); //生成物体 int objcount = datastream.ReadSInt32(); for (int i = 0; i < objcount; i++) { //MSBaseObject.CreateObj(datastream); //从字节流中获取id //int confid = datastream.ReadSInt32(); //float objx = datastream.ReadSInt32() / 1000f; //float objy = datastream.ReadSInt32() / 1000f; int confid = 0; float objx = 0, objy = 0, objsx = 0, objsy = 0; MapObjConf objconf = null; GameObject column = null; int dataid = datastream.ReadByte(); string goname = ""; while (dataid != 0) { switch (dataid) { case 1: confid = datastream.ReadSInt32(); objconf = ConfigManager.mapObjConfManager.map[confid]; break; case 2: objx = datastream.ReadSInt32() / 1000f; break; case 3: objy = datastream.ReadSInt32() / 1000f; break; case 4: int parentid = datastream.ReadSInt32(); break; case 5: int instanceid = datastream.ReadSInt32(); break; case 7: goname = datastream.ReadString16(); break; case 8: objsx = datastream.ReadSInt32() / 1000f; break; case 9: objsy = datastream.ReadSInt32() / 1000f; break; case 6: if (objconf.isstatic == 1) { column = (GameObject)Resources.Load(objconf.path); column = MonoBehaviour.Instantiate(column); column.transform.parent = mapObj.transform; column.transform.position = GameCommon.GetWorldPos(pos) + new Vector2(objx, objy); InGameBaseObj point = column.GetComponent <InGameBaseObj>(); point.Deserialize(datastream); GameCommon.SetObjZIndex(column, objconf.depth); } break; } dataid = datastream.ReadByte(); } if (confid == 4000002) { startPointList.Add(pos + GameCommon.GetMapPos(new Vector2(objx, objy))); continue; } if (objconf.isstatic == 1) { SetWayProperty(pos + GameCommon.GetMapPos(new Vector2(objx, objy)), objconf); column.transform.localScale = new Vector3(objsx, objsy, 1); continue; } SetGroupPoint(pos + GameCommon.GetMapPos(new Vector2(objx, objy)), objconf, new Vector3(objsx, objsy, 1)); } }