internal static bool SetTile(MapTileRenderer renderer, string atlasName, string picName) { if (atlasDic.ContainsKey(atlasName)) { MapTileAtlas atlas = atlasDic[atlasName]; foreach (var pi in atlas.info.Pictures) { if (pi.name == picName) { // 找到了,开始整理renderer renderer.mr.material = atlas.material; renderer.SetRect(pi.rect); return(true); } } return(false); } else { Texture2D tex = Resources.Load <Texture2D>("Atlas/" + atlasName); string json = Resources.Load <TextAsset>("Atlas/" + atlasName + "_config").text; MapTileAtlasInfo info = JsonUtility.FromJson <MapTileAtlasInfo>(json); MapTileAtlas atlas = new MapTileAtlas() { texture = tex, info = info, material = new Material(defaultMaterial) }; atlas.material.SetTexture("_MainTex", tex); atlasDic.Add(atlasName, atlas); foreach (var pi in atlas.info.Pictures) { if (pi.name == picName) { // 找到了,开始整理renderer renderer.mr.material = atlas.material; renderer.SetRect(pi.rect); return(true); } } return(false); } }
private static void MakeAtlas() { string rootDir = "Map/AtlasSources"; string rootPathInWin = Path.Combine(Application.dataPath, rootDir); string[] subDirs = Directory.GetDirectories(rootPathInWin, "*", SearchOption.TopDirectoryOnly); foreach (var dir in subDirs) { List <Texture2D> texs = new List <Texture2D>(); //dir : D:/Test/QuadDemo/Assets\Map/AtlasSources\Test string d = dir.Replace("\\", "/"); //d : D:/Test/QuadDemo/Assets/Map/AtlasSources/Test string[] files = Directory.GetFiles(d, "*.png", SearchOption.TopDirectoryOnly); foreach (var file in files) { string f = file.Replace("\\", "/").Substring(0, file.Length - 4); f = f.Replace(Application.dataPath, "Assets") + ".png"; //f : Assets/Map/AtlasSources/Test/cswy_icon_rcq var t = AssetDatabase.LoadAssetAtPath <Texture2D>(f); texs.Add(t); } List <MapTilePicInfo> infos; RenderTexture rt; MapTileTheme.MakeAtlasWithPictures(texs.ToArray(), 1, out rt, out infos); MapTileAtlasInfo atlasInfo = new MapTileAtlasInfo() { atlasName = Path.GetFileNameWithoutExtension(dir), Count = infos.Count, Pictures = infos, }; atlasInfo.AdjustChildrenAtlasName(); MapTileTheme.SavePNG(rt, "Resources/Atlas/" + atlasInfo.atlasName); string json = JsonUtility.ToJson(atlasInfo); string path = Path.Combine(Application.dataPath, "Resources/Atlas/" + atlasInfo.atlasName + ".json"); File.WriteAllText(path, json); } AssetDatabase.Refresh(); }