/// <summary> /// 设置render /// </summary> private void LoadRenderToObj(XmlElement node, GameObject go) { Debug.Log("setting render to " + go.name); foreach (XmlElement renderNode in node.ChildNodes) { string renderName = renderNode.GetAttribute("name"); bool receiverShadows = renderNode.GetAttribute("receiveShadows") == "True" ? true : false; List <string> temp; temp = FuncUtil.GetInstance().StrToStrList(renderNode.GetAttribute("boundsMin")); Vector3 boundsMin = new Vector3(float.Parse(temp[0]), float.Parse(temp[1]), float.Parse(temp[2])); temp = FuncUtil.GetInstance().StrToStrList(renderNode.GetAttribute("boundsMax")); Vector3 boundsMax = new Vector3(float.Parse(temp[0]), float.Parse(temp[1]), float.Parse(temp[2])); int lightmapIndex = int.Parse(renderNode.GetAttribute("lightmapIndex")); temp = FuncUtil.GetInstance().StrToStrList(renderNode.GetAttribute("lightmapTilingOffset")); Vector4 lightmapTilingOffset = new Vector4(float.Parse(temp[0]), float.Parse(temp[1]), float.Parse(temp[2]), float.Parse(temp[3])); Renderer render; //动画文件应该配置的是其子节点的render if (go.name.Contains("Anim")) { render = go.transform.Find(renderName).GetComponent <Renderer>(); } else { render = go.GetComponent <Renderer>(); } render.lightmapIndex = lightmapIndex; render.lightmapScaleOffset = lightmapTilingOffset; render.receiveShadows = receiverShadows; render.bounds.SetMinMax(boundsMin, boundsMax); } }
//全部texture和mat放进各自的list里面 void LoadAsset(string asset, string occid) { var depends = assetsDependDic[asset]; for (int i = 0; i < depends.Count; i++) { //剔除shader if (!depends[i].Contains("shader") && !depends[i].Contains("FBX")) { var url = SceneConfig.GetInstance().prefix + depends[i].Substring(0, depends[i].IndexOf(".")).ToLower() + SceneConfig.GetInstance().suffix; if (!assetWaitingList.Contains(url)) { assetWaitingList.Add(url); totalAssetNum++; //根据assetName将url放进对应的list if (FuncUtil.GetInstance().IsAssetTexture(depends[i])) { textureWaitingList.Add(url); } if (depends[i].Contains(".mat")) { matWaitingList.Add(url); } if (depends[i] == asset) { prefabWaitingList.Add(url); mainAsseturlDict.Add(asset, url); } } } } }
private void LoadGameObject(XmlElement node, Transform parent = null) { GoConfig config = new GoConfig(); config.name = node.GetAttribute("name"); config.occid = node.GetAttribute("occid"); config.isPerLoad = node.GetAttribute("isPerLoad") == "True" ? true : false; config.isSurface = node.GetAttribute("isSurface") == "True" ? true : false; config.asset = node.GetAttribute("asset"); config.parent = parent; config.id = node.GetAttribute("id"); config.unitType = node.GetAttribute("unitType"); config.node = node; config.tag = node.GetAttribute("tag"); List <string> temp; temp = FuncUtil.GetInstance().StrToStrList(node.GetAttribute("position")); config.position = new Vector3(float.Parse(temp[0]), float.Parse(temp[1]), float.Parse(temp[2])); temp = FuncUtil.GetInstance().StrToStrList(node.GetAttribute("rotation")); config.rotation = new Quaternion(float.Parse(temp[0]), float.Parse(temp[1]), float.Parse(temp[2]), float.Parse(temp[3])); temp = FuncUtil.GetInstance().StrToStrList(node.GetAttribute("scale")); config.scale = new Vector3(float.Parse(temp[0]), float.Parse(temp[1]), float.Parse(temp[2])); configDict.Add(config.occid, config); //Debug.Log("Load GO " + config.occid); LoadAsset(config.asset, config.occid); }
private void LoadAssetsXml() { XmlDocument xml = new XmlDocument(); xml.Load(assetXmlPath); XmlNode root = xml.SelectSingleNode("Root"); foreach (XmlElement node in root.ChildNodes) { var assetDependInfo = node.SelectSingleNode("Info"); var assetsList = FuncUtil.GetInstance().StrToStrList(assetDependInfo.InnerText); var assetsName = node.SelectSingleNode("Title"); assetsDependDic.Add(assetsName.InnerText, assetsList); } }
//split a str into strs by ',' private void LoadRender(XmlElement node) { RenderSettings.fog = node.GetAttribute("fog") == "True"?true:false; List <string> temp; temp = FuncUtil.GetInstance().StrToStrList(node.GetAttribute("fogColor")); RenderSettings.fogColor = new Color(float.Parse(temp[0]), float.Parse(temp[1]), float.Parse(temp[2])); RenderSettings.fogMode = (FogMode)int.Parse(node.GetAttribute("fogMode")); RenderSettings.fogDensity = float.Parse(node.GetAttribute("fogDensity")); RenderSettings.fogStartDistance = float.Parse(node.GetAttribute("fogStartDistance")); RenderSettings.fogEndDistance = float.Parse(node.GetAttribute("fogEndDistance")); temp = FuncUtil.GetInstance().StrToStrList(node.GetAttribute("ambientLight")); RenderSettings.ambientLight = new Color(float.Parse(temp[0]), float.Parse(temp[1]), float.Parse(temp[2])); RenderSettings.haloStrength = float.Parse(node.GetAttribute("haloStrength")); RenderSettings.flareStrength = float.Parse(node.GetAttribute("flareStrength")); }
private void InitGameScene() { assetsDependDic = new Dictionary <string, List <string> >(); //assetsLoadedDic = new Dictionary<string, bool>(); assetXmlPath = Application.dataPath + "/" + SceneConfig.GetInstance().sceneConfigList[Application.loadedLevelName]; LoadAssetsXml(); XmlDocument xml = new XmlDocument(); xml.Load(Application.dataPath + "/Resources/XMLLib/Map/Map_" + SceneConfig.GetInstance().sceneConfigList[Application.loadedLevelName]); XmlNodeList rootNode = xml.SelectSingleNode("root").ChildNodes; XmlElement sceneNode = (XmlElement)rootNode.Item(0); sceneName = sceneNode.GetAttribute("name"); sceneID = sceneNode.GetAttribute("ID"); var pathsLightMapFar = FuncUtil.GetInstance().StrToStrList(sceneNode.GetAttribute("pathLightMapFar")); var pathsLightMapNear = FuncUtil.GetInstance().StrToStrList(sceneNode.GetAttribute("pathLightMapNear")); LoadLightMapFromHttp(pathsLightMapFar, pathsLightMapNear); goEffects = new GameObject("Effects"); goMap = new GameObject("Map"); surfaceLayer = new GameObject("SurfaceLayer"); surfaceLayer.transform.parent = goMap.transform; preloadLayer = new GameObject("PerloadLayer"); preloadLayer.transform.parent = goMap.transform; otherLayer = new GameObject("OtherLayer"); otherLayer.transform.parent = goMap.transform; foreach (XmlElement node in sceneNode) { if (node.Name == "Render") { LoadRender(node); } if (node.Name == "Map") { LoadMapFromXML(node); } if (node.Name == "Effects") { LoadEffectFromXML(node, false); } } }
private static void CompressAndSaveData(XmlDocument xml, Dictionary <int, MapData> mapDic) { int num = 0;//处理了的data的数量,用来判断是否遇到map结尾 int startIndex = 0; int endIndex = 0; Vector3 startPoint = Vector3.zero; float curZ = 0f; var itr = mapDic.GetEnumerator(); StringBuilder totalStr = new StringBuilder(); StringBuilder str = null; while (itr.MoveNext()) { //换行 //保存当前有效段信息并且存入XML if (curZ != itr.Current.Value.middle.z || num == mapDic.Count) { if (curZ != 0) { str.Append(startIndex.ToString() + "," + endIndex.ToString()); totalStr.Append(str.ToString() + "|"); } curZ = itr.Current.Value.middle.z; startIndex = 0; endIndex = 0; startPoint = itr.Current.Value.middle; str = new StringBuilder(); Vector2 xzCoordinate = new Vector2(startPoint.x, startPoint.z); //str.Append(startPoint.ToString() + ";"); str.Append(FuncUtil.GetInstance().RemoveStrInStr(xzCoordinate.ToString(), new List <string>() { "(", ")" }) + ";"); } //Not the End of Line //如果当前行刚开始,设置startIndex if (startIndex == 0) { startIndex = itr.Current.Key; endIndex = itr.Current.Key; } else { //当前行已经开始 //如果两个点是相邻的,即其中间没有因为空缺点被删除出现间隔 //endIndex设置为当前index //否则就遇到了空点,把当前的有效段写入当前字符串段,并设置新的startIndex if ((itr.Current.Key - endIndex) == 1) { endIndex = itr.Current.Key; } else { str.Append(startIndex.ToString() + "," + endIndex.ToString() + ";"); startIndex = itr.Current.Key; endIndex = itr.Current.Key; } } num++; } AddNodeToXML(xml, totalStr.ToString().Remove(totalStr.ToString().LastIndexOf("|"))); }
public static void GenerateMapdata() { System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); Dictionary <int, MapData> mapDic = new Dictionary <int, MapData>(); List <Vector3> pointList = new List <Vector3>(); var goList = Selection.gameObjects; foreach (var go in goList) { Vector3 pos = go.transform.position; Quaternion rot = go.transform.rotation; Vector3 scale = go.transform.localScale; foreach (var point in go.GetComponent <MeshFilter>().sharedMesh.vertices) { Vector3 p = new Vector3(point.x * scale.x, point.y * scale.y, point.z * scale.z); pointList.Add(rot * p + pos); } } //这里生成的地图数据是在对象处于Rotation为0,position为0,但scale不为0的状态 float cubeWidth = 1f; Vector3 lV = new Vector3(); Vector3 rV = new Vector3(); Vector3 tV = new Vector3(); Vector3 bV = new Vector3(); float minX = pointList[0].x; float maxX = pointList[0].x; float maxZ = pointList[0].z; float minZ = pointList[0].z; float maxY = pointList[0].y; float minY = pointList[0].y; //trans.GetComponent<GameObject>().isStatic = false; //Selection.activeGameObject.transform.position = Vector3.zero; //Selection.activeGameObject.transform.rotation = new Quaternion(0,0,0,0); //var vertics = Selection.activeGameObject.GetComponent<MeshFilter>().sharedMesh.vertices; for (int i = 0; i < pointList.Count; i++) { var vertic = pointList[i]; if (vertic.x < minX) { minX = vertic.x; lV = vertic; } if (vertic.x > maxX) { maxX = vertic.x; rV = vertic; } if (vertic.z < minZ) { minZ = vertic.z; bV = vertic; } if (vertic.z > maxZ) { maxZ = vertic.z; tV = vertic; } if (vertic.y > maxY) { maxY = vertic.y; } if (vertic.y < minY) { minY = vertic.y; } } Debug.Log(lV + "/n" + rV + "/n" + tV + "/n" + bV); int rows = (int)((maxZ - minZ) / cubeWidth) + 1; int cols = (int)((maxX - minX) / cubeWidth) + 1; int totalNum = 0; for (float j = minZ + cubeWidth / 2f; j < maxZ; j += cubeWidth) { for (float i = minX + cubeWidth / 2f; i < maxX; i += cubeWidth) { MapData data = new MapData(new Vector3(i + cubeWidth / 2f, maxY, j + cubeWidth / 2f)); int col = (int)((data.middle.x - minX - cubeWidth / 2f) / cubeWidth); int row = (int)((data.middle.z - minZ - cubeWidth / 2f) / cubeWidth); int index = (row * cols + col); //Debug.Log(data.middle+":"+index); if (mapDic.ContainsKey(index)) { Debug.Log("Index exist for" + index + ":" + mapDic[index].middle); } mapDic.Add(index, data); totalNum++; } } //for (int i = 0; i < vertics.Length; i++) { // var vertic = vertics[i] * scale; // int col = (int)((vertic.x - left) / cubeWidth); // int row = (int)((vertic.z - bottom) / cubeWidth); // var p =row * cols + col; // //if (!mapDic.ContainsKey(p)){ // // MapData data = new MapData(new Vector3(left+col * cubeWidth + cubeWidth / 2f, 0, bottom + row * cubeWidth + cubeWidth / 2f), cubeWidth); // // data.isActive = true; // // mapDic.Add(p, data); // //} // var mapData = mapDic[p]; // if (mapData.isActive == false) { // mapData.isActive = true; // } //} var itr = mapDic.GetEnumerator(); while (itr.MoveNext()) { var point = itr.Current.Value.middle + new Vector3(0, 10, 0); RaycastHit hit; //Debug.DrawLine(point, itr.Current.Value.middle, Color.white); if (Physics.Raycast(point, Vector3.down, out hit)) { if (hit.transform.tag.Contains("surface")) { Debug.DrawLine(hit.point + Vector3.up, hit.point, Color.red); itr.Current.Value.middle.y = hit.point.y; itr.Current.Value.isActive = true; } } } for (int i = 0; i < totalNum; i++) { if (mapDic[i].isActive == false) { mapDic.Remove(i); } } Debug.Log(mapDic.Count); itr = mapDic.GetEnumerator(); while (itr.MoveNext()) { Debug.DrawLine(itr.Current.Value.middle + Vector3.up, itr.Current.Value.middle); } XmlDocument xml = new XmlDocument(); xml.AppendChild(xml.CreateElement("Root")); AddNodeToXML(xml, "SizeInfo", cubeWidth.ToString() + "," + minX.ToString() + "," + maxX.ToString() + "," + minY.ToString() + "," + maxY.ToString() + "," + minZ.ToString() + "," + maxZ.ToString()); CompressAndSaveData(xml, mapDic); string scenePath = EditorApplication.currentScene; xml.Save(Application.dataPath + "/" + FuncUtil.GetInstance().GetFileNameByAssetName(scenePath) + "Map.xml"); stopwatch.Stop(); System.TimeSpan timespan = stopwatch.Elapsed; double milliseconds = timespan.TotalMilliseconds; Debug.Log(milliseconds); }
public void LoadMapDataFromXML(string path) { XmlDocument xml = new XmlDocument(); xml.Load(path); XmlNode root = xml.SelectSingleNode("Root"); List <string> sizeInfo = null; List <string> mapDataStr = null; foreach (XmlElement node in root.ChildNodes) { if (node.SelectSingleNode("Title") != null) { sizeInfo = FuncUtil.GetInstance().StrToStrList(node.SelectSingleNode("Info").InnerText); } else { mapDataStr = FuncUtil.GetInstance().SplitStrBy(node.InnerText, "|"); } } //SizeInfo cubeWidth = float.Parse(sizeInfo[0]); minX = float.Parse(sizeInfo[1]); maxX = float.Parse(sizeInfo[2]); minY = float.Parse(sizeInfo[3]); maxY = float.Parse(sizeInfo[4]); minZ = float.Parse(sizeInfo[5]); maxZ = float.Parse(sizeInfo[6]); rows = (int)((maxZ - minZ) / cubeWidth) + 1; cols = (int)((maxX - minX) / cubeWidth) + 1; foreach (var dataStr in mapDataStr) { //data 例子: --72.1,38.0;13635,13635;13689,13710 var strList = FuncUtil.GetInstance().SplitStrBy(dataStr, ";"); //72.1,38.0=>72.1, 54.9, 38.0 string vecStr = strList[0]; //计算初始点,暂时将最大Y作为每个点的Y坐标,之后会进行调整 Vector3 basePoint = FuncUtil.GetInstance().BuildVector3FromStrList(FuncUtil.GetInstance().StrToStrList(vecStr), maxY); //get 13635 in 13635,13635;13689,13710 int baseIndex = int.Parse(FuncUtil.GetInstance().StrToStrList(strList[1])[0]); for (int i = 1; i < strList.Count; i++) { var indexList = FuncUtil.GetInstance().StrToStrList(strList[i]); int beginIndex = int.Parse(indexList[0]); int endIndex = int.Parse(indexList[1]); var tempBasePoint = basePoint + (new Vector3(cubeWidth, 0, 0) * (beginIndex - baseIndex)); for (int index = beginIndex; index <= endIndex; index++) { Vector3 middle = tempBasePoint; var data = new MapData(middle); data.isActive = true; mapDic.Add(index, data); tempBasePoint += new Vector3(cubeWidth, 0, 0); } } //int index = int.Parse(dataStr.Substring(0, dataStr.IndexOf(","))); //var pointStr = dataStr.Substring(dataStr.IndexOf("(") + 1, dataStr.LastIndexOf(")") - dataStr.IndexOf("(") - 1); //string isActiveStr = dataStr.Substring(dataStr.LastIndexOf(",")+1,dataStr.Length-dataStr.LastIndexOf(",")-1); //Vector3 middle = FuncUtil.GetInstance().BuildVector3FromStrList(FuncUtil.GetInstance().StrToStrList(pointStr)); //var data = new MapData(middle); //data.isActive = bool.Parse(isActiveStr); //mapDic.Add(index, data); } Debug.Log("MapData Load Complete" + mapDic.Count); }