/// <summary> /// 递归遍历所有初始UI节点,将其加入uiNodes字典,以便后面查找父节点 /// </summary> /// <param name="trans"></param> private void RecurseAllUIElements(Transform trans) { int childCount = trans.childCount; for (int i = 0; i < childCount; i++) { Transform child = trans.GetChild(i); if (child.GetComponent <InfoNode>() == null) { Debug.Log("UI元素:" + child.name + "没有挂载InfoNode脚本!"); } else { InfoNode node = child.GetComponent <InfoNode>(); if (!uiNodes.ContainsKey(node.GetNodeIdOfName(currentUIName))) { uiNodes.Add(node.GetNodeIdOfName(currentUIName), child); } else { Debug.Log("存在重复的UiNode,重复序号为:" + node.GetNodeIdOfName(currentUIName) + ",名字为:" + child.name); } } RecurseAllUIElements(child); } }
private void RecurseAllMapElements(Transform root, string mapName) { int childCount = root.childCount; for (int i = 0; i < childCount; i++) { Transform child = root.GetChild(i); if (child.GetComponent <InfoNode>() == null) { Debug.Log("map元素:" + child.name + "没有挂载InfoNode脚本!"); } else { InfoNode node = child.GetComponent <InfoNode>(); if (!mapNodes.ContainsKey(node.GetNodeIdOfName(mapName))) { mapNodes.Add(node.GetNodeIdOfName(mapName), child); } else { Debug.Log("存在重复的MapNode,重复序号为:" + node.GetNodeIdOfName(mapName) + ",名字为:" + child.name); } } RecurseAllMapElements(child, mapName); } }
/// <summary> /// 利用传入的map元素信息新建一个map元素,并根据信息对其初始化,再加入节点字典 /// </summary> /// <param name="info"></param> public GameObject NewMapElement(Object obj) { GameObject go = (GameObject)Instantiate(obj); InfoNode node = go.GetComponent <InfoNode>(); if (node == null) { Debug.Log("Map元素预设未挂载GameObjNodeInfo脚本,实例化失败!"); Destroy(go); return(null); } int parentId = node.GetParentIdOfName(currentMapName); if (parentId == 0) { go.transform.parent = mapRoot; } else { if (mapNodes.ContainsKey(parentId)) { go.transform.parent = mapNodes[parentId]; } else { Debug.Log("没有找到Map元素:" + go.name + "的父节点:" + parentId); Destroy(go); return(null); } } if (!mapNodes.ContainsKey(node.GetNodeIdOfName(currentMapName))) { mapNodes.Add(node.GetNodeIdOfName(currentMapName), go.transform); } else { Debug.Log("Map中已存在相同的元素:" + go.name + ",请检查重复调用!"); Destroy(go); return(null); } RecurseAllMapElements(go.transform, currentMapName); GameObjInfo[] goInfos = go.GetComponents <GameObjInfo>(); foreach (GameObjInfo info in goInfos) { if (info.IndexName == currentMapName) { info.SetGameObj(); break; } } return(go); }
/// <summary> /// 利用传入的ui元素信息新建一个UI元素,并根据信息对其初始化,再加入节点字典 /// </summary> /// <param name="info"></param> public GameObject NewUIElement(Object obj) { GameObject go = (GameObject)Instantiate(obj); InfoNode node = go.GetComponent <InfoNode>(); if (node == null) { Debug.Log("UI元素预设未挂载InfoNode脚本,实例化失败!"); Destroy(go); return(null); } int parentId = node.GetParentIdOfName(currentUIName); if (parentId == 0) { go.transform.SetParent(UI.transform); } else { if (uiNodes.ContainsKey(parentId)) { go.transform.SetParent(uiNodes[parentId]); } else { Debug.Log("没有找到UI元素:" + go.name + "的父节点:" + parentId); Destroy(go); return(null); } } if (!uiNodes.ContainsKey(node.GetNodeIdOfName(currentUIName))) { uiNodes.Add(node.GetNodeIdOfName(currentUIName), go.transform); } else { Debug.Log("UI界面中已存在相同的元素:" + go.name + ",请检查重复调用!"); Destroy(go); return(null); } RecurseAllUIElements(go.transform); foreach (RecTransformInfo info in go.GetComponents <RecTransformInfo>()) { if (info.IndexName == currentUIName) { info.SetRecTransform(); break; } } return(go); }
/// <summary> /// 递归遍历所有子节点,将每个需要做成预制物体的预制信息存储到对应的脚本,并将脚本挂载上去 /// </summary> /// <param name="trans"></param> private static void recursePrefabs(Transform trans, string indexName, Dictionary <int, HierachyInfo> elementList) { int childCount = trans.childCount; for (int i = 0; i < childCount; i++) { Transform child = trans.GetChild(i); string filePath = string.Empty; string fileName = string.Empty; if ((child.tag == "uiPrefab") || (child.tag == "commonUI")) { RecTransformInfo info = child.gameObject.AddComponent <RecTransformInfo>(); RectTransform rec = child.GetComponent <RectTransform>(); info.IndexName = indexName; info.SiblingIndex = child.GetSiblingIndex(); info.Pivot = rec.pivot; info.SizeDelta = rec.sizeDelta; info.AnchoredPosition = rec.anchoredPosition; info.AnchoredPosition3D = rec.anchoredPosition3D; info.AnchorMax = rec.anchorMax; info.AnchorMin = rec.anchorMin; info.LocalEulerAngles = rec.localEulerAngles; info.LocalPosition = rec.localPosition; info.LocalScale = rec.localScale; info.OffsetMax = rec.offsetMax; info.OffsetMin = rec.offsetMin; if (child.tag != "commonUI") { filePath = "Assets/DynamicResources/UIPrefabs/UI/" + indexName + '/'; } else { filePath = "Assets/DynamicResources/UIPrefabs/UI/COMMON/"; } fileName = child.name; } else if (child.tag == "mapElements") { GameObjInfo goInfo = child.gameObject.AddComponent <GameObjInfo>(); goInfo.IndexName = indexName; goInfo.LocalPosition = child.localPosition; goInfo.LocalScale = child.localScale; goInfo.LocalEulerAngles = child.localEulerAngles; goInfo.SiblingId = child.GetSiblingIndex(); GameObject[] mapElements = GameObject.FindGameObjectsWithTag("mapElements"); bool isChild = false; foreach (GameObject o in mapElements) { if ((child != o.transform) && child.IsChildOf(o.transform)) { isChild = true; } } if (isChild) { filePath = "Assets/prefabs/map/" + indexName + "/childs/"; fileName = child.name; } else { filePath = "Assets/prefabs/map/" + indexName + "/"; fileName = child.name + "#" + child.GetComponent <InfoNode>().GetNodeIdOfName(indexName); } } if (!string.IsNullOrEmpty(filePath) && !Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } if (!string.IsNullOrEmpty(fileName) && !string.IsNullOrEmpty(filePath)) { string preFileName = filePath + fileName + ".prefab"; if (File.Exists(preFileName)) { File.Delete(preFileName); } Object pre = PrefabUtility.CreateEmptyPrefab(preFileName); PrefabUtility.ReplacePrefab(child.gameObject, pre); if (!preFileName.Contains("/childs/")) { InfoNode nInfo = child.gameObject.GetComponent <InfoNode>(); HierachyInfo hInfo = new HierachyInfo(); hInfo.name = preFileName.ToLower(); hInfo.parentId = nInfo.GetParentIdOfName(indexName); hInfo.siblingId = nInfo.GetSiblingIdOfName(indexName); elementList.Add(nInfo.GetNodeIdOfName(indexName), hInfo); } } recursePrefabs(child, indexName, elementList); } }