///// <summary> ///// ///// </summary> ///// <param name="fileName">相对于 Application.streamingAssetsPath 的路径 (/Scene/Scene_1_1.json)</param> ///// <returns></returns> //public string LoadJson(string fileName) { // string path = Application.streamingAssetsPath + fileName; // if (!File.Exists(path)) { // Debug.LogError("文件不存在:" + path); // } // //加载配置文件 // return File.ReadAllText(path); //} /// <summary> /// 加载场景 /// </summary> /// <param name="sceneName">例如: Scene_1_1 酱紫 </param> public GameObject LoadScene(string sceneName) { string json = IOTool.LoadJson("Scene/" + sceneName + ".json"); SceneConf conf_scene = JsonMapper.ToObject <SceneConf>(json); if (conf_scene == null) { Debug.LogError("Load Json ERROR!"); return(null); } if (conf_scene.Bounds == null) { Debug.LogError("Load Json Bounds NULL!"); LoadScene(conf_scene); return(null); } // 应该在外部工具存储的时候转为世界坐标存储 GameObject scene = LoadScene(conf_scene); Transform parent = scene.transform.FindChild("Map/Bounds"); foreach (var item in conf_scene.Bounds) { // 相对坐标转世界坐标 _cur_scene_bounds.Add(parent.localToWorldMatrix.MultiplyPoint(item.GetPosition())); } return(scene); }
/// <summary> /// 加载场景 /// </summary> /// <param name="sceneName">例如: Scene_1_1 酱紫 </param> public GameObject LoadScene(SceneConf conf_scene) { GameObject scene_go = GameObject.Find("Scene"); if (scene_go == null) { Debug.LogError("导入场景数据出错,场景中无Scene节点!"); return(null); } //create light Object light_prefab = Resources.Load("UI/Prefabs/" + conf_scene.Light.PrefabName); GameObject light_go = GameObject.Instantiate(light_prefab) as GameObject; light_go.name = conf_scene.Light.Name; light_go.transform.localPosition = conf_scene.Light.GetPosition(); light_go.transform.localRotation = conf_scene.Light.GetRotation(); light_go.transform.localScale = conf_scene.Light.GetScale(); light_go.transform.parent = scene_go.transform; //create camera Object ui_prefab = Resources.Load("UI/" + conf_scene.UI.PrefabName); GameObject ui_go = GameObject.Instantiate(ui_prefab) as GameObject; ui_go.name = conf_scene.UI.PrefabName; GameObject camera_go = ui_go.transform.FindChild("CameraScene").gameObject; camera_go.transform.localPosition = conf_scene.UI.GetPosition(); camera_go.transform.localRotation = conf_scene.UI.GetRotation(); camera_go.transform.localScale = conf_scene.UI.GetScale(); ui_go.transform.parent = scene_go.transform; GameObject camera_ui = ui_go.transform.FindChild("Camera").gameObject; camera_ui.transform.localRotation = conf_scene.UI.GetRotation(); /* * //create scene * scene_go = new GameObject("Scene"); * scene_go.name = conf_scene.Name; * scene_go.transform.localPosition = conf_scene.GetPosition(); * scene_go.transform.localRotation = conf_scene.GetRotation(); * scene_go.transform.localScale = conf_scene.GetScale(); */ //create map GameObject map_go = new GameObject(conf_scene.Map.Name); map_go.transform.parent = scene_go.transform; map_go.transform.localPosition = conf_scene.Map.GetPosition(); map_go.transform.localRotation = conf_scene.Map.GetRotation(); map_go.transform.localScale = conf_scene.Map.GetScale(); //create bounds GameObject bounds_root_go = new GameObject("Bounds"); Object point_prefab = Resources.Load("UI/Prefabs/Point_10"); bounds_root_go.transform.parent = map_go.transform; bounds_root_go.transform.localRotation = Quaternion.identity; bounds_root_go.transform.localScale = Vector3.one; bounds_root_go.transform.localPosition = Vector3.zero; if (conf_scene.Bounds != null) { foreach (var item in conf_scene.Bounds) { GameObject point_go = GameObject.Instantiate(point_prefab) as GameObject; point_go.name = item.Name; point_go.transform.parent = bounds_root_go.transform; point_go.transform.localPosition = item.GetPosition(); point_go.transform.localRotation = item.GetRotation(); point_go.transform.localScale = item.GetScale(); } } //create ground GameObject ground_go = new GameObject(conf_scene.Map.Ground.Name); ground_go.transform.parent = map_go.transform; ground_go.transform.localPosition = conf_scene.Map.Ground.GetPosition(); ground_go.transform.localRotation = conf_scene.Map.Ground.GetRotation(); ground_go.transform.localScale = conf_scene.Map.Ground.GetScale(); if (conf_scene.PlaceHolder != null) { foreach (var info in conf_scene.PlaceHolder) { //Debug.Log("位置点信息: " + info.GetPosition()); //todo: FightScene.s_InstanceThis.PushPos(info.GetPosition()); } } else { Debug.Log("位置点信息为空"); } Mesh ground_mesh = PlaneInfo.CreateMesh((float)conf_scene.Map.Ground.CellWidth * 0.5f, (float)conf_scene.Map.Ground.CellHeight * 0.5f); foreach (var cell in conf_scene.Map.Ground.GroundCells) { GameObject cell_go; Object res = Resources.Load("Materials/Map/" + cell.Materials[0]); if (res != null) { Material matX = res as Material; cell_go = PlaneInfo.CreatePlaneWithMesh(cell.GetPosition(), ground_mesh, cell.Name, ground_go.transform, matX); } else { Debug.LogError("NotFound " + cell.Materials[0]); cell_go = PlaneInfo.CreatePlaneWithMesh(cell.GetPosition(), ground_mesh, cell.Name, ground_go.transform, null); if (cell.Materials.Count > 0) { //todo: FightScene.s_InstanceThis.m_MBM.AddMaterial(cell_go, cell.Materials[0]); } } cell_go.transform.localRotation = cell.GetRotation(); cell_go.transform.localScale = cell.GetScale(); } //create surface GameObject surface_go = new GameObject(conf_scene.Map.Surface.Name); surface_go.transform.parent = map_go.transform; surface_go.transform.localPosition = conf_scene.Map.Surface.GetPosition(); surface_go.transform.localRotation = conf_scene.Map.Surface.GetRotation(); surface_go.transform.localScale = conf_scene.Map.Surface.GetScale(); if (conf_scene.Map.Surface.Planes != null) { foreach (var item in conf_scene.Map.Surface.Planes) { GameObject item_go; Object res = Resources.Load("Materials/Map/" + item.Materials[0]); if (res != null) { Material mat = res as Material; item_go = PlaneInfo.CreatePlane((float)item.CellWidth * 0.5f, (float)item.CellHeight * 0.5f, item.Name, surface_go.transform, mat); } else { Debug.LogError("NotFound " + item.Materials[0]); item_go = PlaneInfo.CreatePlane((float)item.CellWidth * 0.5f, (float)item.CellHeight * 0.5f, item.Name, surface_go.transform, null); if (item.Materials.Count > 0) { //todo: FightScene.s_InstanceThis.m_MBM.AddMaterial(item_go, item.Materials[0]); } } item_go.transform.localPosition = item.GetPosition(); item_go.transform.localRotation = item.GetRotation(); item_go.transform.localScale = item.GetScale(); } } if (conf_scene.Map.Surface.Colliders != null) { foreach (var item in conf_scene.Map.Surface.Colliders) { GameObject go = new GameObject(item.Name); go.transform.parent = surface_go.transform; go.transform.localPosition = item.GetPosition(); go.transform.localRotation = item.GetRotation(); go.transform.localScale = item.GetScale(); BoxCollider comp = go.AddComponent <BoxCollider>(); comp.center = item.ListToVector3(item.Center); comp.size = item.ListToVector3(item.Size); } } if (conf_scene.Map.Surface.PlaneWithColliders != null) { foreach (var item in conf_scene.Map.Surface.PlaneWithColliders) { GameObject item_go; Object res = Resources.Load("Materials/Map/" + item.Plane.Materials[0]); if (res != null) { Material mat = res as Material; item_go = PlaneInfo.CreatePlane((float)item.Plane.CellWidth * 0.5f, (float)item.Plane.CellHeight * 0.5f, item.Name, surface_go.transform, mat); } else { Debug.LogError("NotFound " + item.Plane.Materials[0]); item_go = PlaneInfo.CreatePlane((float)item.Plane.CellWidth * 0.5f, (float)item.Plane.CellHeight * 0.5f, item.Name, surface_go.transform, null); if (item.Plane.Materials.Count > 0) { //todo: FightScene.s_InstanceThis.m_MBM.AddMaterial(item_go, item.Plane.Materials[0]); } } item_go.transform.localPosition = item.GetPosition(); item_go.transform.localRotation = item.GetRotation(); item_go.transform.localScale = item.GetScale(); foreach (var item_collider in item.Colliders) { GameObject go = new GameObject(item_collider.Name); go.transform.parent = item_go.transform; go.transform.localPosition = item_collider.GetPosition(); go.transform.localRotation = item_collider.GetRotation(); go.transform.localScale = item_collider.GetScale(); BoxCollider comp = go.AddComponent <BoxCollider>(); comp.center = item_collider.ListToVector3(item_collider.Center); comp.size = item_collider.ListToVector3(item_collider.Size); } } } // Create Over return(scene_go); }
/// <summary> /// 导出数据 /// </summary> /// <param name="fileName"></param> private void OnExportData(string fileName) { GameObject root_scene = getSceneRoot(); GameObject root_map = getMapRoot(); GameObject light = GameObject.Find(light_name); GameObject ui_root = GameObject.Find(ui_name); if (light == null) { Debug.LogError("没有添加方向光"); return; } if (ui_root == null) { Debug.LogError("没有添加摄像机"); return; } SceneConf conf_scene = new SceneConf() { //Trans = root_scene.transform, Map = new MapConf() { // Trans = root_map.transform, Ground = new GroundConf(), Surface = new SurfaceConf(), }, Light = new PrefabConf() { PrefabName = light_name }, UI = new PrefabConf() { PrefabName = ui_name } }; conf_scene.SetTransform(root_scene.transform); conf_scene.Map.SetTransform(root_map.transform); conf_scene.Light.SetTransform(light.transform); conf_scene.UI.SetTransform(ui_root.transform.FindChild("Camera")); //序列化小怪位置数据 Transform placeholder_root_trans = getPlaceHolderRoot(); conf_scene.PlaceHolder = new List <PlaceHolderConfig>(); for (int i = 0; i < placeholder_root_trans.childCount; i++) { PlaceHolderConfig v_point = new PlaceHolderConfig(); v_point.SetTransform(placeholder_root_trans.GetChild(i), false); conf_scene.PlaceHolder.Add(v_point); } //序列化Bounds数据 Transform bounds_root_trans = BoundsRoot; int bounds_point_count = bounds_root_trans.childCount; conf_scene.Bounds = new List <BoundsConfig>(); List <Transform> bounds_points_list = new List <Transform>(); for (int i = 0; i < bounds_point_count; i++) { Transform bounds_points_trans = bounds_root_trans.GetChild(i); bounds_points_list.Add(bounds_points_trans); } //按照字符串排序 var bounds_points_sorted = from point in bounds_points_list orderby point.name ascending select point; foreach (var item in bounds_points_sorted) { BoundsConfig v_point = new BoundsConfig(); v_point.SetTransform(item); conf_scene.Bounds.Add(v_point); } //序列化ground数据 Transform ground_root_trans = root_map.transform.FindChild(ground_root_name); if (ground_root_trans != null) { List <GroundCell> cells = new List <GroundCell>(); float w = 1, h = 1; int gournd_cell_count = ground_root_trans.childCount; if (gournd_cell_count > 0) { Mesh mesh = ground_root_trans.GetChild(0).GetComponent <MeshFilter>().sharedMesh; w = mesh.bounds.size.x; h = mesh.bounds.size.z; //Debug.Log(mesh.vertices[0] + "," + mesh.vertices[1] + "," + mesh.vertices[2] + "," + mesh.vertices[3]); } for (int i = 0; i < gournd_cell_count; i++) { GroundCell cell = new GroundCell(); // cell.Trans = ground_root_trans.GetChild(i); Transform item_trans = ground_root_trans.GetChild(i); cell.SetTransform(item_trans); List <string> mats = new List <string>(); Material[] mat = item_trans.GetComponent <MeshRenderer>().sharedMaterials; if (mat != null && mat.Length > 0) { for (int j = 0; j < mat.Length; j++) { if (mat[j] != null) { mats.Add(mat[j].name); } } } cell.Materials = mats; cells.Add(cell); } conf_scene.Map.Ground.SetTransform(ground_root_trans); conf_scene.Map.Ground.GroundCells = cells; conf_scene.Map.Ground.CellWidth = w; conf_scene.Map.Ground.CellHeight = h; } else { EditorUtility.DisplayDialog("提示", "亲,还没创建地图!", "确认"); return; } //序列化surface数据 Transform surface_root_trans = root_map.transform.FindChild(surface_root_name); if (surface_root_trans != null) { conf_scene.Map.Surface.SetTransform(surface_root_trans); List <SurfaceCellCollider> element_colliders = new List <SurfaceCellCollider>(); List <SurfaceCellPlaneWithCollider> element_planeWithColliders = new List <SurfaceCellPlaneWithCollider>(); List <SurfaceCellPlane> element_planes = new List <SurfaceCellPlane>(); for (int i = 0; i < surface_root_trans.childCount; i++) { Transform item_trans = surface_root_trans.GetChild(i); string name_post_fix = item_trans.name.Substring(item_trans.gameObject.name.LastIndexOf('_') + 1); if (name_post_fix.Equals("pc")) { SurfaceCellPlaneWithCollider cell = new SurfaceCellPlaneWithCollider(); cell.SetTransform(item_trans); List <string> mats = new List <string>(); Material[] mat = item_trans.gameObject.GetComponent <MeshRenderer>().sharedMaterials; if (mat != null && mat.Length > 0) { for (int j = 0; j < mat.Length; j++) { if (mat[j] != null) { mats.Add(mat[j].name); } } } cell.Plane = new SurfaceCellPlane() { Materials = mats }; cell.Plane.CellWidth = item_trans.GetComponent <MeshFilter>().sharedMesh.bounds.size.x; cell.Plane.CellHeight = item_trans.GetComponent <MeshFilter>().sharedMesh.bounds.size.z; List <SurfaceCellCollider> colliders = new List <SurfaceCellCollider>(); for (int j = 0; j < item_trans.childCount; j++) { Transform item_item_trans = item_trans.GetChild(j); SurfaceCellCollider cell_col = new SurfaceCellCollider(); cell_col.SetTransform(item_item_trans); BoxCollider col = item_item_trans.GetComponent <BoxCollider>(); cell_col.Center = cell_col.Vector3ToList(col.center); cell_col.Size = cell_col.Vector3ToList(col.size); cell_col.IsTrigger = col.isTrigger; colliders.Add(cell_col); } cell.Colliders = colliders; element_planeWithColliders.Add(cell); } else if (name_post_fix.Equals("p")) { SurfaceCellPlane cell = new SurfaceCellPlane(); cell.SetTransform(item_trans); List <string> mats = new List <string>(); Material[] mat = item_trans.gameObject.GetComponent <MeshRenderer>().sharedMaterials; if (mat != null && mat.Length > 0) { for (int j = 0; j < mat.Length; j++) { if (mat[j] != null) { mats.Add(mat[j].name); } } } cell.Materials = mats; Mesh mesh = item_trans.GetComponent <MeshFilter>().sharedMesh; cell.CellWidth = mesh.bounds.size.x; cell.CellHeight = mesh.bounds.size.z; element_planes.Add(cell); } else if (name_post_fix.Equals("c")) { SurfaceCellCollider cell = new SurfaceCellCollider(); cell.SetTransform(item_trans); BoxCollider col = item_trans.GetComponent <BoxCollider>(); cell.Center = cell.Vector3ToList(col.center); cell.Size = cell.Vector3ToList(col.size); cell.IsTrigger = col.isTrigger; element_colliders.Add(cell); } } conf_scene.Map.Surface.Colliders = element_colliders; conf_scene.Map.Surface.PlaneWithColliders = element_planeWithColliders; conf_scene.Map.Surface.Planes = element_planes; } string json = JsonMapper.ToJson(conf_scene); if (File.Exists(fileName)) { File.Delete(fileName); } File.WriteAllText(fileName, json); SceneConf obj = JsonMapper.ToObject <SceneConf>(File.ReadAllText(fileName)); bool success = json.Equals(JsonMapper.ToJson(obj)); if (success) { Debug.Log(fileName + ",导出成功!"); } else { Debug.LogError("shit,导出貌似出错了,问问程序猿吧!"); } }