//检查root下的材质能否合并,能则尝试合并 void CombineItem() { bool combine = true; Material mat = null; //金华城 D_itBBox33 for (int i = 0; i < root.childCount; i++) { MeshRenderer mr = root.GetChild(i).GetComponent <MeshRenderer>(); if (mr != null && mr.sharedMaterial != mat && mat != null) { combine = false; break; } if (mat == null) { mat = mr.sharedMaterial; } } if (combine) { CombineChildren cm = root.gameObject.AddComponent <CombineChildren>(); cm.Combine(); BoxCollider b = root.gameObject.AddComponent <BoxCollider>(); b.isTrigger = true; MeshCollider[] co = GetComponentsInChildren <MeshCollider>(); for (int i = 0; i < co.Length; i++) { Destroy(co[i]); } } else { Debug.LogError("无法合并材质"); } }
public void Build() { Clear(); // delete everything in the map before loading the tiles JSONNode json = JSON.Parse(Map.ToString()); int layerCount = json["layers"].Count; int width = json["width"].AsInt; int height = json["height"].AsInt; for (int layer = 0; layer < layerCount; layer++) { GameObject layerObject = new GameObject(); CombineChildren combine = layerObject.AddComponent <CombineChildren>(); layerObject.transform.parent = transform; JSONArray map = json["layers"][layer]["data"].AsArray; string layerName = json["layers"][layer]["name"]; layerObject.name = "Layer" + layerName; int depth; if (!int.TryParse(layerName, out depth)) { depth = 0; } if (depth != PreviousDepth) { CurrentDepth += LayerDepth * LayerScales[depth].y; } PreviousDepth = depth; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int i = x + y * width; int tile = map[i].AsInt - 1; if (tile == -1) { continue; // bit of spaghetti } GameObject obj = Tiles[tile].obj; if (obj == null) { continue; // skip if undefined } float angle = Tiles[tile].angle; float objectX = (x - width / 2) * TileSize; float objectY = CurrentDepth; float objectZ = (y - height / 2) * TileSize; Vector3 position = new Vector3(objectX, objectY, objectZ) + transform.position; Quaternion rotation = Quaternion.Euler(0, angle, 0); GameObject instance = (GameObject)Instantiate(obj, position, rotation); instance.transform.parent = layerObject.transform; instance.transform.position = position; instance.layer = layerObject.layer; // check if this object has a script that needs to be run on map build RunOnMapBuild[] scripts = instance.GetComponents <RunOnMapBuild>(); foreach (RunOnMapBuild script in scripts) { script.Run(); } } } if (CombineMeshes) { combine.Combine(); } foreach (Transform child in layerObject.transform) { child.gameObject.layer = gameObject.layer; } if (LayerScales.Length > 0 && LayerScales[depth] != null) { layerObject.transform.localScale = LayerScales[depth]; } } foreach (Transform child in transform) { child.gameObject.isStatic = true; } }
//受击框 public void RefreshCollision() { collisions.Clear(); if (MethodOnAttack != null || OnAttackCallBack != null) { ///针对能击碎的物件,合并碰撞盒放到顶级 if (name.StartsWith("D_BBox") || name.StartsWith("D_BBBox") || name.StartsWith("D_RJug")) { CombineChildren combine = gameObject.GetComponent <CombineChildren>(); if (combine != null) { combine.Combine(); } BoxCollider b = gameObject.GetComponent <BoxCollider>(); if (b == null) { b = gameObject.AddComponent <BoxCollider>(); } //部分官卡不会合并网格,圣诞夜,宝箱材质是多个无法合并 if (combine == null) { if (name.StartsWith("D_BBox")) { b.size = new Vector3(40, 40, 40); b.center = new Vector3(0, 20, 0); } } MeshCollider[] co = GetComponentsInChildren <MeshCollider>(); for (int i = 0; i < co.Length; i++) { Destroy(co[i]); } } if (name.ToLower().Contains("chair") || name.ToLower().Contains("desk")) { } else { Collider[] co = GetComponentsInChildren <Collider>(); for (int i = 0; i < co.Length; i++) { //不显示出来的都没有受击框 MeshRenderer mr = co[i].GetComponent <MeshRenderer>(); if (mr != null && mr.enabled) { collisions.Add(co[i]); } } } } else //不能受击的物件,可能可以拾取,或者 { if (ItemInfo != null && ItemInfo.IsItem() && root != null) { //炼化 if (ItemInfo.Idx == 9 || ItemInfo.Idx == 23 || ItemInfo.Idx == 24 || ItemInfo.Idx == 25) { BoxCollider b = root.GetComponent <BoxCollider>(); if (b == null) { b = root.gameObject.AddComponent <BoxCollider>(); } b.center = new Vector3(0, 7.5f, 0); b.size = new Vector3(15, 15, 15); b.isTrigger = true; MeshCollider[] co = GetComponentsInChildren <MeshCollider>(); for (int i = 0; i < co.Length; i++) { Destroy(co[i]); } } else { //其他道具类 ReplaceMeshCollider(); } } else if (name.StartsWith("D_itBBox")) { ReplaceMeshCollider(); } else if (name.StartsWith("D_wpBBox")) { ProcessWeaponCollider(); } else if (name.StartsWith("D_wpBBBox")) { ProcessWeaponCollider(); } else if (name.StartsWith("D_Wpn") || name.StartsWith("D_wpn") || name.StartsWith("D_Wp") || name.StartsWith("D_wp")) { ProcessWeaponCollider(); } else if (name.StartsWith("D_sn03")) { //尖刺-如果有box的对象,取消box对象上的触发器 ProcessSn03Collider(); } else if (name.StartsWith("D_ston")) { //可击碎的石头-击碎后可攻击角色 } else if (name.StartsWith("D_tp")) { } /*else*/ //Debug.LogError("not processed:" + name); } }