Exemplo n.º 1
0
    public void Init(ObjRefInfo objRefInfo, int mapSizeW, int mapSizeH, int w, int h, Vector2Int view, Vector2 offset)
    {
        m_objRefInfoAsset = objRefInfo;
        m_visibleView     = view;
        m_offset          = offset;
        m_visibleObj      = new VisiableObj[view.x, view.y];

        m_map_W = mapSizeW;
        m_map_H = mapSizeH;

        m_tilemap_W   = w;
        m_tilemap_H   = h;
        m_tilemapInfo = new TileMapInfo[mapSizeW / w, mapSizeH / h];
    }
Exemplo n.º 2
0
    public static void Export()
    {
        //加载MapInfo
        Dictionary <string, List <GameObject> > layerObjDic;
        string mapInfo;

        LoadMap(m_MapName, out layerObjDic, out mapInfo);

        //加载全局设置信息
        var          settingsInfoPath = AssetDatabase.GUIDToAssetPath(uteGLOBAL3dMapEditor.uteSettingstxt);
        StreamReader rd      = new StreamReader(settingsInfoPath);
        string       setting = rd.ReadToEnd();

        rd.Close();
        string[] infoSplited     = setting.Split(':');
        var      globalGridSizeX = System.Convert.ToInt32(infoSplited[3]);
        var      globalGridSizeZ = System.Convert.ToInt32(infoSplited[4]);

        //计算map分块大小,创建分块map配置
        int map_w = globalGridSizeX / m_MapSpiltSize.x;
        int map_h = globalGridSizeZ / m_MapSpiltSize.y;

        TileMapInfo[,] mapList = new TileMapInfo[m_MapSpiltSize.x, m_MapSpiltSize.y];
        for (int w = 0; w < m_MapSpiltSize.x; w++)
        {
            for (int h = 0; h < m_MapSpiltSize.y; h++)
            {
                mapList[w, h]             = GetStriptableObject <TileMapInfo>(string.Format("Assets/Map/MapInfo_{0}_{1}.asset", w, h));
                mapList[w, h].mapIndex    = h * map_w + w;
                mapList[w, h].posIndex    = new List <int>();
                mapList[w, h].posY        = new List <double>();
                mapList[w, h].objInfoList = new List <TileMapObjInfo>();
            }
        }

        //创建地形obj配置
        ObjRefInfo objRefInfo = GetStriptableObject <ObjRefInfo>("Assets/Map/ObjRefInfo.asset");

        objRefInfo.objRef = new List <ObjInfo>();
        objRefInfo.mats   = new List <Material>();
        var terrainObjList = layerObjDic["DEFAULT"];

        objRefInfo.gameObjectRef = terrainObjList;
        for (int i = 0; i < terrainObjList.Count; i++)
        {
            //顶点和UV
            MeshFilter     meshFilter = terrainObjList[i].GetComponent <MeshFilter>();
            List <Vector3> v          = new List <Vector3>();
            List <Vector2> v2         = new List <Vector2>();
            meshFilter.sharedMesh.GetVertices(v);
            for (int j = 0; j < v.Count; j++)
            {
                v2.Add(new Vector2(v[j].x, v[j].z));
            }
            List <Vector2> uvs = new List <Vector2>();
            meshFilter.sharedMesh.GetUVs(0, uvs);
            //材质
            MeshRenderer meshRender = terrainObjList[i].GetComponent <MeshRenderer>();
            Material     mat        = meshRender.sharedMaterial;
            int          matIndex   = 0;
            if (!objRefInfo.mats.Contains(mat))
            {
                matIndex = objRefInfo.mats.Count;
                objRefInfo.mats.Add(mat);
            }
            else
            {
                for (int j = 0; j < objRefInfo.mats.Count; j++)
                {
                    if (objRefInfo.mats[j].name == mat.name)
                    {
                        matIndex = j;
                    }
                }
            }
            objRefInfo.objRef.Add(new ObjInfo(v2.ToArray(), uvs.ToArray(), matIndex));
        }
        UnityEditor.EditorUtility.SetDirty(objRefInfo);

        //地形分块数据
        string[] myMapInfoAll = mapInfo.Split("$"[0]);
        for (int i = 0; i < myMapInfoAll.Length - 1; i++)
        {
            string[] myMapParts = myMapInfoAll[i].Split(":"[0]);
            string   layerName  = myMapParts[10].ToString();
            if (layerName == "DEFAULT")
            {
                int    objID      = System.Convert.ToInt32(myMapParts[0]);
                int    pX         = System.Convert.ToInt32(myMapParts[1]) - 500 + globalGridSizeX / 2 - 1;
                double pY         = System.Convert.ToDouble(myMapParts[2]);
                int    pZ         = System.Convert.ToInt32(myMapParts[3]) - 500 + globalGridSizeZ / 2 - 1;
                int    rY         = System.Convert.ToInt32(myMapParts[5]);
                string staticInfo = myMapParts[7];                //1:static

                int index_x   = Mathf.FloorToInt(pX / map_w);
                int index_z   = Mathf.FloorToInt(pZ / map_h);
                var _mapinfo  = mapList[index_x, index_z];
                int _posIndex = pZ * globalGridSizeX + pX;
                if (_mapinfo.posIndex.Contains(_posIndex))
                {
                    Debug.LogWarningFormat("有地块重叠:X:{0},Z:{1},会取Y比较大的地块", System.Convert.ToInt32(myMapParts[1]), System.Convert.ToInt32(myMapParts[3]));
                    int index = 0;
                    for (int j = 0; j < _mapinfo.posIndex.Count; j++)
                    {
                        if (_mapinfo.posIndex[j] == _posIndex)
                        {
                            index = j;
                            break;
                        }
                    }
                    if (_mapinfo.posY[index] < pY)
                    {
                        _mapinfo.posIndex[index]    = _posIndex;
                        _mapinfo.posY[index]        = pY;
                        _mapinfo.objInfoList[index] = new TileMapObjInfo(objID, rY);
                    }
                }
                else
                {
                    _mapinfo.posIndex.Add(_posIndex);
                    _mapinfo.posY.Add(pY);
                    _mapinfo.objInfoList.Add(new TileMapObjInfo(objID, rY));
                }

                UnityEditor.EditorUtility.SetDirty(_mapinfo);
            }
        }

        UnityEditor.AssetDatabase.SaveAssets();
    }