改了格子大小
This commit is contained in:
@@ -1,23 +1,13 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using HxGame;
|
||||
using HxGame.Data;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Runtime.Remoting.Metadata.W3cXsd2001;
|
||||
using System.Security.Policy;
|
||||
using System.Threading;
|
||||
using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.UIElements;
|
||||
using static MapManager;
|
||||
using static UnityEngine.Rendering.DebugUI.Table;
|
||||
using System.Linq;
|
||||
|
||||
public partial class MapManager : MonoBehaviour
|
||||
{
|
||||
@@ -50,8 +40,112 @@ public partial class MapManager : MonoBehaviour
|
||||
return;
|
||||
}
|
||||
_curMapRegions?.SaveXML(mapId);
|
||||
}
|
||||
|
||||
public void SaveAtlasToFile()
|
||||
{
|
||||
int atlasWidth = _curMapRegions.regionRowNum * (int)_curMapRegions.regionWidth;
|
||||
int atlasHeight = _curMapRegions.regionColNum * (int)_curMapRegions.regionHeight;
|
||||
if (atlasWidth < 8192)
|
||||
{
|
||||
SaveAtlasToFileSmall();
|
||||
return;
|
||||
}
|
||||
// ʹ<><CAB9><EFBFBD>ڴ<EFBFBD><DAB4>Ż<EFBFBD><C5BB><EFBFBD>Bitmap<61><70><EFBFBD><EFBFBD>
|
||||
Bitmap atlas = new Bitmap(atlasWidth, atlasHeight, PixelFormat.Format24bppRgb);
|
||||
|
||||
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(atlas))
|
||||
{
|
||||
g.Transform = new System.Drawing.Drawing2D.Matrix(1, 0, 0, -1, 0, atlas.Height);
|
||||
for (int row = 0; row < _curMapRegions.regionRowNum; row++)
|
||||
{
|
||||
for (int col = 0; col < _curMapRegions.regionColNum; col++)
|
||||
{
|
||||
Texture2D tileTex = _curMapRegions.regions[row, col].texture;
|
||||
if (tileTex == null) continue;
|
||||
|
||||
Bitmap tileBitmap = TextureToBitmap(tileTex);
|
||||
g.DrawImage(
|
||||
tileBitmap,
|
||||
row * (int)_curMapRegions.regionWidth,
|
||||
col * (int)_curMapRegions.regionHeight
|
||||
);
|
||||
tileBitmap.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>JPG<50><47><EFBFBD><EFBFBD>Alphaʱ<61><CAB1>
|
||||
string outputPath = $"{PathUtil.GetBigMapPath(_curOpenMapId)}/map_{_curOpenMapId}.jpg";
|
||||
atlas.Save(outputPath, ImageFormat.Jpeg);
|
||||
atlas.Dispose();
|
||||
|
||||
Debug.Log($"<22><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD>ߴ<EFBFBD>: {atlasWidth}x{atlasHeight}");
|
||||
UIWindow.Instance.ShowMessage("<22><>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>");
|
||||
}
|
||||
|
||||
private Bitmap TextureToBitmap(Texture2D texture)
|
||||
{
|
||||
bool hasAlpha = texture.GetPixels().Any(c => c.a < 1f);
|
||||
Bitmap bitmap = new Bitmap(
|
||||
texture.width,
|
||||
texture.height,
|
||||
hasAlpha ? PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb
|
||||
);
|
||||
|
||||
BitmapData data = bitmap.LockBits(
|
||||
new Rectangle(0, 0, texture.width, texture.height),
|
||||
ImageLockMode.WriteOnly,
|
||||
bitmap.PixelFormat
|
||||
);
|
||||
|
||||
byte[] bytes = new byte[texture.width * texture.height * (hasAlpha ? 4 : 3)];
|
||||
UnityEngine.Color[] pixels = texture.GetPixels();
|
||||
|
||||
for (int i = 0; i < pixels.Length; i++)
|
||||
{
|
||||
bytes[i * (hasAlpha ? 4 : 3)] = (byte)(pixels[i].b * 255); // B
|
||||
bytes[i * (hasAlpha ? 4 : 3) + 1] = (byte)(pixels[i].g * 255); // G
|
||||
bytes[i * (hasAlpha ? 4 : 3) + 2] = (byte)(pixels[i].r * 255); // R
|
||||
if (hasAlpha) bytes[i * 4 + 3] = (byte)(pixels[i].a * 255); // A
|
||||
}
|
||||
|
||||
System.Runtime.InteropServices.Marshal.Copy(bytes, 0, data.Scan0, bytes.Length);
|
||||
bitmap.UnlockBits(data);
|
||||
return bitmap;
|
||||
}
|
||||
public void SaveAtlasToFileSmall()
|
||||
{
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD>ijߴ<C4B3>
|
||||
int atlasWidth = _curMapRegions.regionRowNum * (int)_curMapRegions.regionWidth;
|
||||
int atlasHeight = _curMapRegions.regionColNum * (int)_curMapRegions.regionHeight;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD>յĴ<D5B5>ͼ
|
||||
Texture2D atlas = new Texture2D(atlasWidth, atlasHeight);
|
||||
atlas.filterMode = FilterMode.Bilinear; // <20><><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Сͼ<D0A1><CDBC><EFBFBD>ϲ<EFBFBD>
|
||||
for (int row = 0; row < _curMapRegions.regionRowNum; row++)
|
||||
{
|
||||
for (int col = 0; col < _curMapRegions.regionColNum; col++)
|
||||
{
|
||||
HxGame.Data.Region region = _curMapRegions.regions[row, col];
|
||||
Texture2D tileTex = region.texture; // <20><><EFBFBD><EFBFBD>Сͼ<D0A1><CDBC><EFBFBD><EFBFBD>
|
||||
atlas.SetPixels(
|
||||
row * (int)_curMapRegions.regionWidth,
|
||||
col * (int)_curMapRegions.regionHeight,
|
||||
(int)_curMapRegions.regionWidth,
|
||||
(int)_curMapRegions.regionHeight,
|
||||
tileTex.GetPixels()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
atlas.Apply(); // Ӧ<><D3A6><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>
|
||||
byte[] pngData = atlas.EncodeToJPG();
|
||||
File.WriteAllBytes($"{PathUtil.GetBigMapPath(_curOpenMapId)}/map_{_curOpenMapId}.jpg", pngData);
|
||||
UIWindow.Instance.ShowMessage("<22><>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>");
|
||||
}
|
||||
|
||||
public void LoadMapRegionSprites(int mapId)
|
||||
{
|
||||
if (!allMaps.TryGetValue(mapId.ToString(),out var mapInfo))
|
||||
@@ -69,7 +163,7 @@ public partial class MapManager : MonoBehaviour
|
||||
_curMapRegions.regionHeight = PicMapPixel;
|
||||
_curMapRegions.cellWidthPixel = widthPixel;
|
||||
_curMapRegions.cellHeightPixel = heightPixel;
|
||||
_curMapRegions.regions = new Region[mapRownum, mapColumn];
|
||||
_curMapRegions.regions = new HxGame.Data.Region[mapRownum, mapColumn];
|
||||
|
||||
int maxRow = mapColumn;
|
||||
int maxCol = mapRownum;
|
||||
@@ -87,10 +181,10 @@ public partial class MapManager : MonoBehaviour
|
||||
float x = row * jpgscenew;
|
||||
float y = col * jpgscenew;
|
||||
obj.transform.position = new Vector2(x, y);
|
||||
_curMapRegions.regions[row, col] = new Region();
|
||||
_curMapRegions.regions[row, col] = new HxGame.Data.Region();
|
||||
_curMapRegions.regions[row, col].indexX = row;
|
||||
_curMapRegions.regions[row, col].indexY = col;
|
||||
multithreadLoadTextrue(spPath, sr);
|
||||
multithreadLoadTextrue(spPath, sr, _curMapRegions.regions[row, col]);
|
||||
_curMapRegions.regions[row, col].regionName = filename;
|
||||
}
|
||||
}
|
||||
@@ -111,13 +205,14 @@ public partial class MapManager : MonoBehaviour
|
||||
float jpgscenew = PicMapPixel / 100.0f;
|
||||
MapManager.Instance.GenerateMap(mapid, jpgscenew * _curMapRegions.regionRowNum, jpgscenew * _curMapRegions.regionColNum, _curMapRegions.cellWidthPixel / 100.0f, _curMapRegions.cellHeightPixel / 100.0f);
|
||||
}
|
||||
private async void multithreadLoadTextrue(string fullPath,SpriteRenderer sr)
|
||||
private async void multithreadLoadTextrue(string fullPath,SpriteRenderer sr, HxGame.Data.Region region)
|
||||
{
|
||||
var texture = await loadASyncTexture2D(fullPath, this.GetCancellationTokenOnDestroy());
|
||||
if (texture != null)
|
||||
{
|
||||
sr.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero, 100f);
|
||||
}
|
||||
region.texture = texture;
|
||||
}
|
||||
|
||||
private UniTask<Texture2D> loadASyncTexture2D(string fullPath,CancellationToken token)
|
||||
|
||||
Reference in New Issue
Block a user