public void CopyFromSource() { if (obj.spriteDataList == null) { obj.spriteDataList = new SpriteAtlasData.SpriteData[0]; } spriteDataList = new List <SpriteAtlasData.SpriteData>(obj.spriteDataList.Length); foreach (var v in obj.spriteDataList) { if (v == null) { spriteDataList.Add(null); } else { var t = new SpriteAtlasData.SpriteData(); t.CopyFrom(v); spriteDataList.Add(t); } } }
public static bool Build(SpriteAtlasProxy gen) { if (gen == null) { return(false); } string prefabDirName = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(gen.obj)) + "/"; //强制导图设置配置 int numTexturesReimported = 0; for (int i = gen.spriteDataList.Count - 1; i >= 0; i--) { Texture2D curTexture = AssetDatabase.LoadAssetAtPath(gen.spriteDataList[i].imagePath, typeof(Texture2D)) as Texture2D; string thisTexturePath = AssetDatabase.GetAssetPath(curTexture); if (!String.IsNullOrEmpty(thisTexturePath)) { if (ConfigureSpriteTextureImporter(thisTexturePath)) { numTexturesReimported++; AssetDatabase.ImportAsset(thisTexturePath); } //去除透明 if (gen.disableTrimming) { gen.spriteDataList[i].bound = new Rect(0, 0, curTexture.width, curTexture.height); } else { gen.spriteDataList[i].bound = GameEditorUtility.GetTextureBound(curTexture); } gen.spriteDataList[i].sourceWidth = curTexture.width; gen.spriteDataList[i].sourceHeight = curTexture.height; } else { gen.spriteDataList.RemoveAt(i); } } if (numTexturesReimported > 0) { AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } //计算小图排列 bool forceAtlasSize = gen.forceTextureSize; int atlasWidth = forceAtlasSize ? gen.forcedTextureWidth : gen.maxTextureSize; int atlasHeight = forceAtlasSize ? gen.forcedTextureHeight : gen.maxTextureSize; bool forceSquareAtlas = forceAtlasSize ? false : gen.forceSquareAtlas; bool allowFindingOptimalSize = !forceAtlasSize; bool allowRotation = !gen.disableRotation; Builder atlasBuilder = new Builder(atlasWidth, atlasHeight, gen.allowMultipleAtlases ? 64 : 1, allowFindingOptimalSize, forceSquareAtlas, allowRotation); for (int i = 0; i < gen.spriteDataList.Count; ++i) { Rect curRect = gen.spriteDataList[i].bound; atlasBuilder.AddRect((int)curRect.width + gen.padding, (int)curRect.height + gen.padding); } if (atlasBuilder.Build() != 0) { if (atlasBuilder.HasOversizeTextures()) { EditorUtility.DisplayDialog("提示", "小图片太大超过图集尺寸,图集放不下,请修改小图片尺寸", "Ok"); } else { EditorUtility.DisplayDialog("提示", "图片太多了,图集放不下,请减少图片数量", "Ok"); } return(false); } //拼出大图 SpriteAnimEdit.Editor.Atlas.Data[] atlasData = atlasBuilder.GetAtlasData(); gen.atlasTextures = new Texture2D[atlasData.Length]; gen.atlasMaterials = new Material[atlasData.Length]; for (short atlasIndex = 0; atlasIndex < atlasData.Length; ++atlasIndex) { Texture2D tex = new Texture2D(atlasData[atlasIndex].width, atlasData[atlasIndex].height, TextureFormat.ARGB32, false); tex.SetPixels(new Color[tex.height * tex.width]); gen.atlasWastage = (1.0f - atlasData[0].occupancy) * 100.0f; gen.atlasWidth = atlasData[0].width; gen.atlasHeight = atlasData[0].height; for (int i = 0; i < atlasData[atlasIndex].entries.Length; ++i) { var entry = atlasData[atlasIndex].entries[i]; SpriteAtlasData.SpriteData sd = gen.spriteDataList[entry.index]; Texture2D source = AssetDatabase.LoadAssetAtPath(sd.imagePath, typeof(Texture2D)) as Texture2D; Color[] newColors = gen.disableTrimming ? source.GetPixels() : GameEditorUtility.GetTextureColor(source, new Rect(sd.bound.x, sd.bound.y, sd.bound.width, sd.bound.height)); int width = (int)sd.bound.width; int height = (int)sd.bound.height; sd.regionX = entry.x + gen.padding / 2; sd.regionY = entry.y + gen.padding / 2; sd.regionW = width; sd.regionH = height; sd.padding = gen.padding; sd.atlasIndex = atlasIndex; sd.anchorX = sd.regionX + (int)(sd.sourceWidth * 0.5f + gen.anchorPointInImage.x - sd.bound.x); sd.anchorY = sd.regionY + (int)(gen.anchorPointInImage.y - sd.bound.y); if (!entry.flipped) { tex.SetPixels(sd.regionX, sd.regionY, sd.regionW, sd.regionH, newColors); // 如果勾选了输出调试图形,则在图片上绘制调试信息 if (gen.outputDebugGraphic) { // 画边框 for (int y = 0; y < sd.regionH; y++) { tex.SetPixel(sd.regionX, sd.regionY + y, Color.green); } for (int y = 0; y < sd.regionH; y++) { tex.SetPixel(sd.regionX + sd.regionW, sd.regionY + y, Color.green); } for (int x = 0; x < sd.regionW; x++) { tex.SetPixel(sd.regionX + x, sd.regionY, Color.green); } for (int x = 0; x < sd.regionW; x++) { tex.SetPixel(sd.regionX + x, sd.regionY + sd.regionH, Color.green); } // 画锚点 tex.SetPixel(sd.anchorX, sd.anchorY, Color.red); } // for (int y = 0; y < height; ++y) // { // for (int x = 0; x < width; ++x) // { // tex.SetPixel(entry.x + x, entry.y + y, newColors[y * width + x]); // } // } } else { //翻转 for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { tex.SetPixel(entry.x + y + gen.padding, entry.y + x + gen.padding, newColors[y * width + x]); } } } } tex.Apply(); //贴图生产 string texturePath = prefabDirName + "atlas" + atlasIndex + ".png"; BuildDirectoryToFile(texturePath); byte[] bytes = tex.EncodeToPNG(); System.IO.FileStream fs = System.IO.File.Create(texturePath); fs.Write(bytes, 0, bytes.Length); fs.Close(); Object.DestroyImmediate(tex); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); //材质生产 //CreateAssetBundle.SetTextureSetting(true, texturePath); tex = AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D)) as Texture2D; Material mat = new Material(Shader.Find("Unlit/Transparent")); mat.mainTexture = tex; string materialPath = prefabDirName + "atlas" + atlasIndex + "material.mat"; BuildDirectoryToFile(materialPath); AssetDatabase.CreateAsset(mat, materialPath); AssetDatabase.SaveAssets(); gen.atlasTextures[atlasIndex] = tex; gen.atlasMaterials[atlasIndex] = mat; } DeleteUnusedAssets(gen.atlasTextures, gen.obj.atlasTextures); DeleteUnusedAssets(gen.atlasMaterials, gen.obj.atlasMaterials); gen.CopyToTarget(gen.obj); EditorUtility.SetDirty(gen.obj); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); return(true); }
public Texture2D GetTexture(string name) { SpriteAtlasData.SpriteData spriteData = timeLineView.GetSpriteData(name); return(AssetDatabase.LoadAssetAtPath(spriteData.imagePath, typeof(Texture2D)) as Texture2D); }
private void DrawTextureSettings() { CurActionData = this.host.CurActionData; if (CurActionData == null) { return; } GUI.color = Color.white; BeginHeader("动作设置"); string changedName = EditorGUILayout.TextField("动作名", CurActionData.name).Trim(); if (changedName != CurActionData.name && changedName.Length > 0) { CurActionData.name = changedName; HandleUtility.Repaint(); } float fps = EditorGUILayout.FloatField("帧率", CurActionData.fps); if (fps > 0) { CurActionData.fps = fps; } float clipTime = CurActionData.FrameList.Length / fps; float newClipTime = EditorGUILayout.FloatField("时长", clipTime); if (newClipTime > 0 && newClipTime != clipTime) { CurActionData.fps = CurActionData.FrameList.Length / newClipTime; } EndHeader(); BeginHeader("图集设置"); if (SpriteAnimationData.SpriteAtlasData != null) { string atlasName = SpriteAnimationData.SpriteAtlasData.name; EditorGUILayout.LabelField("图集名", atlasName); int iwidth = 256; int iheight = 256; Rect rect = GUILayoutUtility.GetRect(iwidth, iheight, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)); TextureGrid.Draw(rect); GameEditorUtility.DrawOutline(rect, Color.gray); if (CurTexture != null) { iwidth = (int)rect.width; iheight = (int)rect.height; if (CurTexture.width / CurTexture.height >= iwidth / iheight) { if (CurTexture.width > iwidth) { rect.width = iwidth; rect.height = (CurTexture.height * iwidth) / CurTexture.width; } else { rect.width = CurTexture.width; rect.height = CurTexture.height; } } else { if (CurTexture.height > iheight) { rect.height = iheight; rect.width = (CurTexture.width * iheight) / CurTexture.height; } else { rect.width = CurTexture.width; rect.height = CurTexture.height; } } rect.x += (iwidth - rect.width) / 2; rect.y += (iheight - rect.height) / 2; GUI.DrawTexture(rect, CurTexture); } GUILayout.Space(1); spriteListScroll = GUILayout.BeginScrollView(spriteListScroll, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)); for (int i = 0; i < SpriteAnimationData.SpriteAtlasData.spriteDataList.Length; i++) { SpriteAtlasData.SpriteData spriteData = SpriteAnimationData.SpriteAtlasData.spriteDataList[i]; if (spriteData == null) { continue; } bool highlight = selection == spriteData; GUI.backgroundColor = highlight ? Color.green : new Color(0.8f, 0.8f, 0.8f); GUILayout.BeginHorizontal("AS TextArea", GUILayout.MinHeight(20f)); GUI.backgroundColor = Color.white; GUILayout.Label(i.ToString(), GUILayout.Width(24f)); if (GUILayout.Button(spriteData.name, "OL TextField", GUILayout.Height(20f))) { selection = spriteData; } GUI.backgroundColor = Color.green; if (GUILayout.Button("<+", GUILayout.Width(26f))) { selection = spriteData; host.timeLineView.InsertFrame(selection.name, -1); } if (GUILayout.Button("+>", GUILayout.Width(26f))) { selection = spriteData; host.timeLineView.InsertFrame(selection.name, +1); } if (selection != null) { CurTexture = this.host.GetTexture(selection.name); } else { CurTexture = null; } HandleUtility.Repaint(); GUI.backgroundColor = Color.white; GUILayout.EndHorizontal(); } GUILayout.EndScrollView(); } else { GUILayout.Label("请先创建图集,在创建动画!"); } EndHeader(); }
//左侧sprite展示区、drop拖拽、删除一系列操作 private void DrawSpriteList() { Rect rect = new Rect(0, 0, leftBarWidth, Screen.height); if (rect.Contains(Event.current.mousePosition)) { switch (Event.current.type) { case EventType.DragUpdated: if (IsValidDragPayload()) { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; } else { DragAndDrop.visualMode = DragAndDropVisualMode.None; } break; case EventType.DragPerform: ReferenceDropObjs(); Repaint(); //拖拽完成绘制 break; } } if (Event.current.type == EventType.Layout && deferredDroppedObjects != null) { HandleDroppedPayload(deferredDroppedObjects); deferredDroppedObjects = null; } if (_spriteAtlasProxy != null) { if (_spriteAtlasProxy.Empty) { GUILayout.BeginVertical(GUILayout.Width(leftBarWidth), GUILayout.ExpandHeight(true)); GUILayout.FlexibleSpace(); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label("拖动贴图到这里"); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.FlexibleSpace(); GUILayout.EndVertical(); return; } } else { return; } //line Texture2D blackTexture = new Texture2D(1, 1); blackTexture.SetPixel(0, 0, Color.black); blackTexture.Apply(); GUI.DrawTexture(new Rect(leftBarWidth, 16, 1f, Screen.height - 16), blackTexture); Handles.DrawLine(new Vector3(leftBarWidth, 0), new Vector3(leftBarWidth, 0)); spriteListScroll = GUILayout.BeginScrollView(spriteListScroll, GUILayout.Width(leftBarWidth), GUILayout.ExpandHeight(true)); bool delete = false; for (int spriteIndex = 0; spriteIndex < _spriteAtlasProxy.spriteDataList.Count; ++spriteIndex) { SpriteAtlasData.SpriteData sprite = _spriteAtlasProxy.spriteDataList[spriteIndex]; Texture2D spriteSrcTexture = GetTexture(sprite.name);; if (spriteSrcTexture == null && sprite.name.Length == 0) { continue; } bool highlight = selection == sprite.name; GUI.backgroundColor = highlight ? Color.green : new Color(0.8f, 0.8f, 0.8f); GUILayout.BeginHorizontal("AS TextArea", GUILayout.MinHeight(20f)); GUI.backgroundColor = Color.white; GUILayout.Label(spriteIndex.ToString(), GUILayout.Width(24f)); if (GUILayout.Button(sprite.name, "OL TextField", GUILayout.Height(20f))) { selection = sprite.name; textureView.CurTexture = spriteSrcTexture; } if (mDelNames.Contains(sprite.name)) { GUI.backgroundColor = Color.red; if (GUILayout.Button("删除", GUILayout.Width(60f))) { delete = true; } GUI.backgroundColor = Color.green; if (GUILayout.Button("X", GUILayout.Width(22f))) { mDelNames.Remove(sprite.name); delete = false; } GUI.backgroundColor = Color.white; } else { // 不想要的都加入删除集合,一键删除 if (GUILayout.Button("X", GUILayout.Width(22f))) { mDelNames.Add(sprite.name); } } GUILayout.EndHorizontal(); } if (delete) { for (int spriteIndex = _spriteAtlasProxy.spriteDataList.Count - 1; spriteIndex >= 0; spriteIndex--) { SpriteAtlasData.SpriteData sprite = _spriteAtlasProxy.spriteDataList[spriteIndex]; Texture2D spriteSrcTexture = GetTexture(sprite.name); if (spriteSrcTexture == null && sprite.name.Length == 0) { continue; } if (mDelNames.Contains(sprite.name)) { _spriteAtlasProxy.spriteDataList.RemoveAt(spriteIndex); if (textureView.CurTexture == spriteSrcTexture) { textureView.CurTexture = null; textureView.Draw(); } } } mDelNames.Clear(); } GUILayout.EndScrollView(); Rect viewRect = GUILayoutUtility.GetLastRect(); leftBarWidth = (int)GameEditorUtility.DragableHandle(4819283, viewRect, leftBarWidth, GameEditorUtility.DragDirection.Horizontal); }