static void DeleteLinkNodes() { if (Selection.objects != null && Selection.objects.Length == 2) { if (Selection.objects[0] is GameObject && Selection.objects[1] is GameObject) { GameObject node0 = Selection.objects[0] as GameObject; ScenePathNode path0 = node0.GetComponent <ScenePathNode>(); GameObject node1 = Selection.objects[1] as GameObject; ScenePathNode path1 = node1.GetComponent <ScenePathNode>(); if (path0 && path1) { int id0 = path0.nodeID; int id1 = path1.nodeID; if (path0.prevNodeIDs.Contains(id1)) { path0.prevNodeIDs.Remove(id1); } if (path1.prevNodeIDs.Contains(id0)) { path1.prevNodeIDs.Remove(id0); } } else { EditorUtility.DisplayDialog("错误 ", "必须都是ScenePathNode", "我的错"); } } } else { EditorUtility.DisplayDialog("错误 ", "必须有且只有两个节点", "我的错"); } }
static void DropToSurface(string layer) { if (Selection.objects != null) { for (int i = 0; i < Selection.objects.Length; i++) { GameObject node0 = Selection.objects[i] as GameObject; ScenePathNode node = node0.GetComponent <ScenePathNode>(); Vector3 origin = node.gameObject.transform.position; RaycastHit hit; if (Physics.Raycast(origin, Vector3.down, out hit, 1000, LayerMask.GetMask(layer))) { node.gameObject.transform.position = hit.point; } NavMeshHit Navhit; bool blocked = false; blocked = NavMesh.Raycast(origin, origin + 1000 * Vector3.down, out Navhit, 1); Debug.DrawLine(origin, origin + 1000 * Vector3.down, blocked ? Color.red : Color.green); Debug.Log("blocked" + blocked + Navhit.position + Navhit.mask); if (blocked) { Debug.DrawRay(Navhit.position, Vector3.up, Color.red); } } } }
static void DeleteOnePathNode(ScenePathNode node) { int id = node.nodeID; ScenePathNode[] list = node.gameObject.transform.parent.GetComponentsInChildren <ScenePathNode>(); for (int j = 0; j < list.Length; j++) { if (list[j].prevNodeIDs.Contains(id)) { list[j].prevNodeIDs.Remove(id); } } GameObject.DestroyImmediate(node.gameObject); }
static void DeleteNode() { if (Selection.objects != null) { for (int i = 0; i < Selection.objects.Length; i++) { if (Selection.objects[i] is GameObject) { GameObject node0 = Selection.objects[i] as GameObject; ScenePathNode node = node0.GetComponent <ScenePathNode>(); DeleteOnePathNode(node); } } } }
public static ScenePathNode CreatePathNode(SceneMap map, int nodeID, List <int> prevNodeIDs) { Transform parent = map.transform.Find("Path"); if (parent == null) { GameObject temp = new GameObject(); temp.name = "Path"; parent = temp.transform; parent.parent = map.transform; //GameBase.GameCommon.ResetTrans(parent); parent.localScale = Vector3.one; parent.localPosition = Vector3.zero; parent.localRotation = Quaternion.identity; } if (parent == null) { return(null); } GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere); go.name = "PathNode-" + nodeID; ScenePathNode node = go.AddComponent <ScenePathNode>(); node.nodeID = nodeID; node.prevNodeIDs = new List <int>(); if (prevNodeIDs != null && prevNodeIDs.Count > 0) { node.prevNodeIDs.AddRange(prevNodeIDs); } node.map = map; go.transform.parent = parent; go.transform.localScale = Vector3.one; go.transform.localPosition = Vector3.zero; go.transform.localRotation = Quaternion.identity; go.transform.position = selectionPosition; _isUpdated = true; Selection.activeGameObject = go; return(null); }
static public void AddNode() { Transform parent = MapManager.current.transform.Find("Path"); ScenePathNode[] nodes = parent.gameObject.GetComponentsInChildren <ScenePathNode>(); int N = nodes.Length; quick_sort(nodes, 0, N - 1); int id = nodes[N - 1].nodeID + 1; GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere); go.name = "PathNode-" + id; ScenePathNode node = go.AddComponent <ScenePathNode>(); node.nodeID = id; node.map = MapManager.current; go.transform.parent = parent; go.transform.localScale = Vector3.one; go.transform.localPosition = Vector3.zero; go.transform.localRotation = Quaternion.identity; go.transform.position = MapManager.selectionPosition; }
static void DropToMavMesh() { if (Selection.objects != null) { List <ScenePathNode> deleteList = new List <ScenePathNode>(); for (int i = 0; i < Selection.objects.Length; i++) { GameObject node0 = Selection.objects[i] as GameObject; ScenePathNode node = node0.GetComponent <ScenePathNode>(); Vector3 origin = node.gameObject.transform.position; NavMeshHit Navhit; bool blocked = false; blocked = NavMesh.Raycast(origin, origin + 1000 * Vector3.down, out Navhit, 1); Debug.DrawLine(origin, origin + 1000 * Vector3.down, blocked ? Color.red : Color.green); if (blocked) { Debug.DrawRay(Navhit.position, Vector3.up, Color.red); deleteList.Add(node); } else { if (Navhit.position.x != Mathf.Infinity && Navhit.position.y != Mathf.Infinity && Navhit.position.z != Mathf.Infinity) { node.gameObject.transform.position = Navhit.position; } } } for (int i = 0; i < deleteList.Count; i++) { DeleteOnePathNode(deleteList[i]); } deleteList.Clear(); deleteList = null; } }
public static List <ScenePathNode> CreateRectNodes(SceneMap map, int startID, float l, float a, Vector3 up, Vector3 startPos, Vector3 endPos, int M, int N) { List <ScenePathNode> nodelist = new List <ScenePathNode>(); Vector3 dir = (endPos - startPos); float length = dir.magnitude; dir = dir.normalized; Vector3 crossDir = Vector3.Cross(dir, up).normalized; List <Vector3> startposlist = new List <Vector3>(); for (int i = 0; i < M; i++) { int index = M / 2 - i; Vector3 pos = startPos + index * crossDir * a; startposlist.Add(pos); } Transform parent = map.transform.Find("Path"); if (parent == null) { GameObject temp = new GameObject(); temp.name = "Path"; parent = temp.transform; parent.parent = map.transform; parent.localScale = Vector3.one; parent.localPosition = Vector3.zero; parent.localRotation = Quaternion.identity; } for (int i = 0; i <= N; i++) { float per = i / (float)N; if (M > 1) { for (int j = 0; j <= M - 1; j++) { List <int> prevNodeIDs = new List <int>(); int nodeID = startID + i * M + j; int preid = startID + (i - 1) * M + j; int preid1 = startID + (i - 1) * M + j + 1; int preid2 = startID + (i - 1) * M + j - 1; int nodeID1 = nodeID + 1; int nodeID2 = nodeID - 1; int afterid = startID + (i + 1) * M + j; int afterid1 = startID + (i + 1) * M + j + 1; int afterid2 = startID + (i + 1) * M + j - 1; int last = startID + (N + 1) * M - 1; int prestart = startID + (i - 1) * M; int preend = startID + i * M - 1; int afterstart = startID + (i + 1) * M; int afterend = startID + (i + 2) * M - 1; if (preid >= 0 && preid >= prestart && preid <= preend) { prevNodeIDs.Add(preid); } if (preid1 >= 0 && preid1 >= prestart && preid1 <= preend) { prevNodeIDs.Add(preid1); } if (preid2 >= 0 && preid2 >= prestart && preid2 <= preend) { prevNodeIDs.Add(preid2); } if (nodeID1 >= 0 && nodeID1 >= startID + i * M && nodeID1 <= startID + (i + 1) * M - 1) { prevNodeIDs.Add(nodeID1); } if (nodeID2 >= 0 && nodeID2 >= startID + i * M && nodeID2 <= startID + (i + 1) * M - 1) { prevNodeIDs.Add(nodeID2); } if (afterid >= 0 && afterid <= last && afterid >= afterstart && afterid <= afterend) { prevNodeIDs.Add(afterid); } if (afterid1 >= 0 && afterid1 <= last && afterid1 >= afterstart && afterid1 <= afterend) { prevNodeIDs.Add(afterid1); } if (afterid2 >= 0 && afterid2 <= last && afterid2 >= afterstart && afterid2 <= afterend) { prevNodeIDs.Add(afterid2); } /* * if (i > 0) * { * for (int k = 0; k <= M - 1; k++) * { * prevNodeIDs.Add(startID + (i - 1) * M + k); * } * } * if (j == 0) * { * if (!prevNodeIDs.Contains(nodeID + 1)) * prevNodeIDs.Add(nodeID + 1); * } * else if (j == M - 1) * { * if (!prevNodeIDs.Contains(nodeID - 1)) * prevNodeIDs.Add(nodeID - 1); * } * else * { * if (!prevNodeIDs.Contains(nodeID + 1)) * prevNodeIDs.Add(nodeID + 1); * if (!prevNodeIDs.Contains(nodeID - 1)) * prevNodeIDs.Add(nodeID - 1); * }*/ GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere); go.name = "PathNode-" + nodeID; ScenePathNode node = go.AddComponent <ScenePathNode>(); node.nodeID = nodeID; node.prevNodeIDs = new List <int>(); if (prevNodeIDs != null && prevNodeIDs.Count > 0) { node.prevNodeIDs.AddRange(prevNodeIDs); } node.map = map; go.transform.parent = parent; go.transform.localScale = Vector3.one; go.transform.localPosition = Vector3.zero; go.transform.localRotation = Quaternion.identity; go.transform.position = startposlist[j] + per * length * dir; nodelist.Add(node); } } else if (M == 1) { List <int> prevNodeIDs = new List <int>(); int nodeID = startID + i; if (i > 0) { prevNodeIDs.Add(startID + (i - 1)); } GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere); go.name = "PathNode-" + nodeID; ScenePathNode node = go.AddComponent <ScenePathNode>(); node.nodeID = nodeID; node.prevNodeIDs = new List <int>(); if (prevNodeIDs != null && prevNodeIDs.Count > 0) { node.prevNodeIDs.AddRange(prevNodeIDs); } node.map = map; go.transform.parent = parent; go.transform.localScale = Vector3.one; go.transform.localPosition = Vector3.zero; go.transform.localRotation = Quaternion.identity; go.transform.position = startposlist[0] + per * length * dir; nodelist.Add(node); } } return(nodelist); }
private void OnDrawGizmos() { if (map == null) { map = transform.parent.parent.gameObject.GetComponent <SceneMap> (); } if (mat == null) { render = gameObject.GetComponent <Renderer>(); if (render != null) { Material[] arr = render.sharedMaterials; if (arr.Length > 0) { Shader shader = Shader.Find("Standard"); mat = new Material(shader); render.sharedMaterials = new Material[] { mat }; } } } if (mat != null) { if (type == Example.ScenePathNode.PathNodeType.PNT_TRANSMIT) { mat.color = Color.green; } else if (type == Example.ScenePathNode.PathNodeType.PNT_KEY) { mat.color = Color.red; } else { mat.color = Color.white; } } if (prevNodeIDs.Count >= 0) { while (prevNodes.Count > prevNodeIDs.Count) { prevNodes.RemoveAt(prevNodes.Count - 1); } for (int i = 0, count = prevNodes.Count; i < count; i++) { ScenePathNode node = prevNodes[i]; if (node.nodeID != prevNodeIDs[i]) { Transform trans = map.transform.Find("Path/PathNode-" + prevNodeIDs[i]); if (trans) { ScenePathNode prevNode = trans.GetComponent <ScenePathNode>(); if (prevNode) { prevNodes[i] = prevNode; } else { Debug.LogError("Scene Path Node:" + nodeID + " prevNodeID is invalid->" + prevNodeIDs[i]); } } } if (type == node.type && node.type == Example.ScenePathNode.PathNodeType.PNT_KEY) { Gizmos.color = Color.HSVToRGB(Level / 255.0f, 1, 1); } Gizmos.DrawLine(transform.position, prevNodes[i].transform.position); Gizmos.color = Color.white; } while (prevNodes.Count < prevNodeIDs.Count) { int index = prevNodes.Count; //Debug.LogError ("map=>" + (map == null)); Transform trans = map.transform.Find("Path/PathNode-" + prevNodeIDs[index]); if (trans) { ScenePathNode prevNode = trans.GetComponent <ScenePathNode>(); if (prevNode) { prevNodes.Add(prevNode); Gizmos.DrawLine(transform.position, prevNode.transform.position); } else { Debug.LogError("Scene Path Node:" + nodeID + " prevNodeID is invalid->" + prevNodeIDs[index]); } } } /* * if (prevNode == null || prevNodeID != prevNode.nodeID) * { * Transform trans = map.transform.Find("Path/PathNode-" + prevNodeID); * if (trans) * { * prevNode = trans.GetComponent<ScenePathNode>(); * } * } * * if (prevNode == null) * { * if (nodeID > 0) * Debug.LogError("pathNode prevNodeID must be set a valid value->" + prevNodeID); * return; * } * * Gizmos.DrawLine(transform.position, prevNode.transform.position); */ } }
public static void ExportMap(string path, SceneMap map) { Example.MapUnit mapUnit = new Example.MapUnit(); mapUnit.Id = map.sceneId; mapUnit.ResID = map.resId; List <Example.NPCUnit> npcUnits = ExportNpcList(map.npcList); mapUnit.Npcs = npcUnits; int npcGroupId = 0; int lastNpcGroupId = 0; Example.NPCGroup npcGroup = null; List <Example.NPCGroup> npcGroups = new List <Example.NPCGroup> (); foreach (var group in map.GetComponentsInChildren <SceneNPCGroup>()) { npcGroupId = group.groupId; if (npcGroupId - lastNpcGroupId > 1) { for (int i = lastNpcGroupId + 1; i < npcGroupId; ++i) { npcGroup = new Example.NPCGroup(); npcGroup.DelayTime = -1; npcGroups.Add(npcGroup); } lastNpcGroupId = npcGroupId; } npcGroup = new Example.NPCGroup(); npcGroup.DelayTime = group.delayTime; npcGroup.NextGroup = -1; List <Example.NPCPass> npcPasses = new List <Example.NPCPass> (); npcGroup.Passes = npcPasses; foreach (var wave in group.GetComponentsInChildren <SceneNPCWave>()) { Example.NPCPass pass = new Example.NPCPass(); pass.DelayTime = wave.delayTime; List <int> npcIds = new List <int> (); foreach (var npc in wave.GetComponentsInChildren <SceneNPC>()) { npcIds.Add(npc.objectId); //Debug.LogWarningFormat ("Export Group {0} Npc {1}",npcGroupId,npc.objectId); } pass.Npcs = npcIds; npcPasses.Add(pass); } npcGroups.Add(npcGroup); lastNpcGroupId = npcGroupId; } List <Example.TransmitNode> transmitNodes = new List <Example.TransmitNode> (); int nodeId = 0; int lastNodeId = 0; Example.TransmitNode transmitNode = null; foreach (var transmit in map.GetComponentsInChildren <TransmitNode>()) { nodeId = transmit.objectId; if (nodeId - lastNodeId > 1) { for (int i = lastNodeId + 1; i < nodeId; ++i) { transmitNode = new Example.TransmitNode(); transmitNode.Id = -1; transmitNodes.Add(transmitNode); } lastNodeId = nodeId; } Debug.LogFormat("transmit.nodeId={0}", transmit.objectId); transmitNode = new Example.TransmitNode(); transmitNode.Id = transmit.objectId; transmitNode.Map = transmit.mapId; transmitNode.Node = transmit.nodeId; transmitNode.Position = MapUtil.ToVector3f(transmit.transform.position); transmitNode.Rotation = MapUtil.ToVector3f(transmit.transform.rotation.eulerAngles); transmitNode.Eff = transmit.effectName; transmitNodes.Add(transmitNode); } ; List <Example.FlyingPath> flyingPaths = new List <Example.FlyingPath> (); nodeId = 0; lastNodeId = 0; Example.FlyingPath flyingPath = null; foreach (var flying in map.GetComponentsInChildren <FlyingPath>()) { nodeId = flying.objectId; if (nodeId - lastNodeId > 1) { for (int i = lastNodeId + 1; i < nodeId; ++i) { flyingPath = new Example.FlyingPath(); flyingPath.Id = -1; flyingPaths.Add(flyingPath); } lastNodeId = nodeId; } List <Example.FlyingPoint> points = new List <Example.FlyingPoint> (); foreach (var p in flying.GetComponentsInChildren <FlyingPoint>()) { List <Example.FlyingAction> actions = new List <Example.FlyingAction> (); foreach (var a in p.GetComponentsInChildren <FlyingAction>()) { List <string> args = new List <string> (); args.Add(a.arg1); args.Add(a.arg2); var action = new Example.FlyingAction(); action.Action = a.action; action.Args = args; action.Id = a.objectId; actions.Add(action); } var flyingPoint = new Example.FlyingPoint(); flyingPoint.Id = p.objectId; flyingPoint.Position = MapUtil.ToVector3f(p.transform.position); flyingPoint.Action = actions; points.Add(flyingPoint); } Debug.LogFormat("path.nodeId={0}", flying.objectId); flyingPath = new Example.FlyingPath(); flyingPath.Id = flying.objectId; flyingPath.Points = points; flyingPaths.Add(flyingPath); } ; mapUnit.TransmitNodes = transmitNodes; mapUnit.Groups = npcGroups; mapUnit.FlyingPaths = flyingPaths; //pathnode Transform pathParent = map.transform.Find("Path"); if (pathParent != null) { List <Example.ScenePathNode> list = new List <Example.ScenePathNode>(); ScenePathNode[] nodes = pathParent.GetComponentsInChildren <ScenePathNode>(); List <List <Example.ScenePathNodeContext> > prevLists = new List <List <Example.ScenePathNodeContext> >(); if (nodes != null && nodes.Length > 0) { List <Example.ScenePathNodeContext> prevList = null; for (int i = 0, count = nodes.Length; i < count; i++) { ScenePathNode node = nodes[i]; Example.ScenePathNode dataNode = new Example.ScenePathNode(); dataNode.Id = node.nodeID; dataNode.Pos = MapUtil.Vector3ToVector3f(node.transform.position); //dataNode.PrevNodes = new List<Example.ScenePathNodeContext>(); dataNode.AdjacentNodes = new List <Example.ScenePathNodeContext>(); dataNode.Type = node.type; if (dataNode.Id >= prevLists.Count) { for (int j = prevLists.Count; j <= dataNode.Id; j++) { prevLists.Add(new List <Example.ScenePathNodeContext>()); } } prevList = prevLists[dataNode.Id]; for (int j = 0, jcount = node.prevNodeIDs.Count; j < jcount; j++) { Example.ScenePathNodeContext context = new Example.ScenePathNodeContext(); context.Cid = node.prevNodeIDs[j]; //dataNode.PrevNodes.Add(context); prevList.Add(context); } for (int j = list.Count; j <= node.nodeID; j++) { Example.ScenePathNode nn = new Example.ScenePathNode(); nn.Id = -1; list.Add(nn); } list[node.nodeID] = dataNode; } } for (int i = 0, count = list.Count; i < count; i++) { Example.ScenePathNode node = list[i]; if (node != null && node.Id >= 0) { List <Example.ScenePathNodeContext> prevList = prevLists[node.Id]; //if (node.PrevNodes.Count == 0) if (prevList.Count == 0) { if (node.Id > 0) { Debug.LogError("scene path node:" + node.Id + " prev node is invalid 0"); return; } } int prevID = -1; //for (int j = 0, jcount = node.PrevNodes.Count; j < jcount; j++) for (int j = 0, jcount = prevList.Count; j < jcount; j++) { //Example.ScenePathNodeContext c1 = node.PrevNodes[j]; Example.ScenePathNodeContext c1 = prevList[j]; prevID = c1.Cid; if (prevID < 0) { if (node.Id > 0) { Debug.LogError("scene path node:" + node.Id + " prev node is invalid 1->" + prevID); return; } } if (prevID >= 0) { if (prevID >= list.Count) { Debug.LogError("scene path node:" + node.Id + " prev node is invalid 1->" + prevID + "^" + list.Count); return; } Example.ScenePathNode temp = list[prevID]; if (temp == null || temp.Id < 0) { Debug.LogError("scene path node:" + node.Id + " prev node is invalid 2"); return; } //if (temp.NextNodes == null) // temp.NextNodes = new List<Example.ScenePathNodeContext>(); Example.ScenePathNodeContext context = new Example.ScenePathNodeContext(); context.Cid = node.Id; context.Cost = Vector3.Distance(MapUtil.Vector3fToVector3(node.Pos), MapUtil.Vector3fToVector3(temp.Pos)); c1.Cost = context.Cost; //temp.NextNodes.Add(context); temp.AdjacentNodes.Add(context); } } node.AdjacentNodes.AddRange(prevList); } } /* * for (int i = 0, count = list.Count; i < count; i++) * { * Example.ScenePathNode node = list[i]; * Debug.Log("-----log node----->" + node.Id); * for (int j = 0, jcount = node.AdjacentNodes.Count; j < jcount; j++) * { * Debug.Log("adjacent->" + j + "^" + node.AdjacentNodes[j].Cid); * } * }*/ if (mapUnit.ScenePath == null) { mapUnit.ScenePath = new Example.ScenePath(); mapUnit.ScenePath.Nodes = new List <Example.ScenePathNode>(); } mapUnit.ScenePath.Nodes.AddRange(list); } //var bytes = MapInfo.SerializeToBytes (mapInfo); var bytes = Example.MapUnit.SerializeToBytes(mapUnit); FileStream fs = new FileStream(path, FileMode.Create); fs.Write(bytes, 0, bytes.Length); fs.Close(); /* * using (fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None)) * { * byte[] arr = new byte[fs.Length]; * fs.Read(arr, 0, (int)fs.Length); * MemoryStream ms = new MemoryStream(arr); * Example.MapUnit m = Example.MapUnit.Deserialize(ms); * * for (int i = 0, count = m.ScenePath.Nodes.Count; i < count; i++) * { * Example.ScenePathNode node = m.ScenePath.Nodes[i]; * Debug.Log("-------------node-------------->" + node.Id); * if (node != null) * { * for (int j = 0, jcount = node.AdjacentNodes.Count; j < jcount; j++) * { * Example.ScenePathNodeContext context = node.AdjacentNodes[j]; * Debug.Log("adjacent context------------>" + context.Cid + "^" + context.Cost); * } * } * } * } */ /*var prefab = PrefabUtility.GetPrefabParent(map.gameObject); * PrefabUtility.ReplacePrefab(map.gameObject, prefab);*/ }