//-------------------------------------- // PUBLIC METHODS //-------------------------------------- public static Rect RenderNode(int index, TPAtlas antlas, string name) { Vector2 itemPos = new Vector2 (); itemPos.y = 25f; if(index + 1 > colItemsCount) { itemPos.y += Mathf.FloorToInt(index / colItemsCount) * TexturePackerStyles.TEXTURE_RECT_SIZE * 1.7f; index = index % colItemsCount; } itemPos.x = itemsSpace / 2f + (index) * (itemsSpace + box_size); GUILayout.BeginArea (new Rect (itemPos.x, itemPos.y , box_size, box_size * 2f), ""); { if(TexturePackerAtlasEditor.selection.Contains(name)) { drawColordeBox (box_size, box_size, Color.green); /* GUILayout.BeginArea (new Rect (box_padding, box_padding , TexturePackerStyles.TEXTURE_RECT_SIZE, TexturePackerStyles.TEXTURE_RECT_SIZE), ""); { drawColordeBox (TexturePackerStyles.TEXTURE_RECT_SIZE, TexturePackerStyles.TEXTURE_RECT_SIZE); } GUILayout.EndArea (); */ } else { drawColordeBox (box_size, box_size); } TPAtlasTexture tx = antlas.getTexture (name); tx.draw (getSize(tx), true); string texNmae = string.Empty; if(TPEditorData.isExtensionsEnabled) { texNmae = tx.name; } else { texNmae = tx.nameNoExtention; } string imageInfo = texNmae + "\n(" + tx.width + "x" + tx.height + ")"; GUILayout.Label (imageInfo, TexturePackerStyles.imageLableStyle, TexturePackerStyles.FixedWidthHeight(110f, 40f)); } GUILayout.EndArea (); return new Rect (itemPos.x, itemPos.y, box_size, box_size); }
public TerrainChunk(OmniTerrain parent,int x, int y,int size) { OmniTerrain.loadedChunks.Add(this); OmniTerrain.chunks[x + y * size] = this; if (OmniTerrain.biomes.ContainsKey(x + y * size)) biome = OmniTerrain.biomes[x + y * size]; objects = new List<OmniObject>(); pendingSlantUpdates = new List<int>(); atlas = TPackManager.getAtlas ("World"); pos = new Vector3 ((int)(x*size), (int)(y*size), 0); mesh = new Mesh[] { new Mesh(), new Mesh() }; chunksize = size; slant = new byte[chunksize * chunksize]; for (int i = 0; i < slant.Length; i++) { slant[i] = 0; } this.parent = parent; if (Network.isClient) { RChunkEvent e = new RChunkEvent(OmniWorld.tick); e.x = (int)x; e.y = (int)y; e.player = Network.player; OmniEvents.AddEvent(e); } else { loaded = true; GenDamageMap(size); GenBiomes(size); GenSlant(); GenMesh(1); } }
void OnValidate() { atlas = TPackManager.getAtlas (atlasName); textureList = atlas.frameNames; }
public void load() { atlas = TPackManager.getAtlas(atlasName); }
private void renderAtlasPreview() { string[] atlasesNames = TPAssetPostprocessor.atlasesNames; EditorGUIUtility.LookLikeInspector(); GUILayout.BeginHorizontal(TexturePackerStyles.toolBarBoxStyle); GUILayout.FlexibleSpace(); EditorGUI.BeginChangeCheck(); if(atlasIndex == -1) { int i = 0; string n = TPEditorData.selectedAtlasName; foreach(string atName in atlasesNames) { if(atName.Equals(n)) { atlasIndex = i; break; } i++; } if(atlasIndex == -1) { atlasIndex = 0; } } atlasIndex = EditorGUI.Popup( new Rect(-10, 0, TexturePackerStyles.ATLAS_POPUP_WIDTH, 20), "", atlasIndex, atlasesNames, TexturePackerStyles.toolBarDropDwonStyle); if(EditorGUI.EndChangeCheck()) { images.Clear (); selection.Clear(); TPEditorData.selectedAtlasName = atlasesNames [atlasIndex]; } GUIStyle btnStyle = EditorStyles.toolbarButton; if(GUILayout.Button("Add To Animation", btnStyle)) { addSelectedFrameToAnimation(); } if(TPEditorData.isExtensionsEnabled) { btnStyle = TexturePackerStyles.toobarEnabledButton; } if(GUILayout.Button("Extensions", btnStyle)) { TPEditorData.isExtensionsEnabled = !TPEditorData.isExtensionsEnabled; } search = GUILayout.TextField(search, GUI.skin.FindStyle("ToolbarSeachTextField"), TexturePackerStyles.FixedWidth(200f)); if (GUILayout.Button("", GUI.skin.FindStyle("ToolbarSeachCancelButton"))) { // Remove focus if cleared search = ""; GUI.FocusControl(null); } GUILayout.EndHorizontal(); scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Width (editor.position.width), GUILayout.Height (editor.position.height - 17f)); GUILayout.Box("", GUIStyle.none, new GUILayoutOption[]{GUILayout.ExpandWidth(true), GUILayout.Height(_maxY)}); _maxY = 0f; TextureNodeRenderer.calculateVars (editor.position.width - 10f); currentAtlas = getAtlas (atlasesNames[atlasIndex]); Rect r; int index = 0; images.Clear (); foreach(string name in currentAtlas.frameNames) { if(search != string.Empty) { if(!name.ToLower().Contains(search.ToLower())) { continue; } } r = TextureNodeRenderer.RenderNode (index, currentAtlas, name); rememberImageRect (name, r); setNodeY(r.y + r.height * 1.5f); index++; } EditorGUILayout.EndScrollView(); }
static void ConvertSprite() { //EditorUtility.DisplayDialog("MyTool", "Do It in C# !", "OK", ""); //Object selobj = Selection.activeObject; string selectionPath = AssetDatabase.GetAssetPath(Selection.activeObject); if (!selectionPath.EndsWith(".plist")) { EditorUtility.DisplayDialog("Error", "Please select a plist file!", "OK", ""); return; } Debug.LogWarning("#PLisToSprites start:" + selectionPath); string fileContent = string.Empty; using (FileStream file = new FileStream(selectionPath, FileMode.Open)) { byte[] str = new byte[(int)file.Length]; file.Read(str, 0, str.Length); fileContent = GetUTF8String(str); Debug.Log(fileContent); file.Close(); file.Dispose(); } //去掉<!DOCTYPE>,不然异常 int delStart = fileContent.IndexOf("<!DOCTYPE"); int delEnd = fileContent.IndexOf("\n", delStart); fileContent = fileContent.Remove(delStart, delEnd - delStart); Debug.Log(fileContent); //解析文件 PList plist = new PList(); plist.LoadText(fileContent);//Load(selectionPath); TPAtlas at = new TPAtlas(); at.LoadX(plist); //重写meta string texPath = Path.GetDirectoryName(selectionPath) + "/" + at.realTextureFileName; Texture2D selTex = AssetDatabase.LoadAssetAtPath(texPath, typeof(Texture2D)) as Texture2D; Debug.Log("texture:" + texPath); Debug.Log("write texture:" + selTex.name + " size:" + selTex.texelSize); TextureImporter textureImporter = AssetImporter.GetAtPath(texPath) as TextureImporter; if (textureImporter.textureType != TextureImporterType.Sprite && textureImporter.textureType != TextureImporterType.Advanced) { EditorUtility.DisplayDialog("Error", "Texture'type must be sprite or Advanced!", "OK", ""); return; } if (textureImporter.spriteImportMode != SpriteImportMode.Multiple) { EditorUtility.DisplayDialog("Error", "spriteImportMode must be Multiple!", "OK", ""); return; } SpriteMetaData[] sheetMetas = new SpriteMetaData[at.sheets.Count]; for (int i = 0; i < at.sheets.Count; i++) { var frameData = at.sheets[i]; sheetMetas[i].alignment = 0; sheetMetas[i].border = new Vector4(0, 0, 0, 0); sheetMetas[i].name = frameData.name; sheetMetas[i].pivot = new Vector2(0.5f, 0.5f); sheetMetas[i].rect = new Rect(frameData.frame.x, at.size.y - frameData.frame.y - frameData.frame.height, frameData.frame.width, frameData.frame.height);//这里原点在左下角,y相反 //Debug.Log("do sprite:" + frameData.name); } //textureImporter.spriteImportMode = SpriteImportMode.Multiple; textureImporter.spritesheet = sheetMetas; //save textureImporter.textureType = TextureImporterType.Sprite; //bug? textureImporter.spriteImportMode = SpriteImportMode.Multiple; //不加这两句会导致无法保存meta AssetDatabase.ImportAsset(texPath, ImportAssetOptions.ForceUpdate); Debug.LogWarning("#PLisToSprites end:" + texPath); }
//-------------------------------------- // INITIALIZE //-------------------------------------- public TPAtlasTexture(string _name, IDictionary <string, System.Object> frameData, TPAtlas _atlas) { name = _name; if (name.Length > 4 && name.Contains(".")) { nameNoExtention = name.Substring(0, name.Length - 4); } foreach (string cKey in frameData.Keys) { switch (cKey) { case "frame": _frame = new TFrame(frameData[cKey] as IDictionary <string, System.Object>); break; case "sourceSize": _sourceSize = new TSourceSize(frameData[cKey] as IDictionary <string, System.Object>); break; case "spriteSourceSize": _spriteSourceSize = new TFrame(frameData[cKey] as IDictionary <string, System.Object>); break; case "trimmed": _trimmed = System.Boolean.Parse(frameData[cKey].ToString()); break; case "rotated": _rotated = System.Boolean.Parse(frameData[cKey].ToString()); break; } } _scale = _atlas.meta.scale;; _width = (float)_sourceSize.w / _scale; _height = (float)_sourceSize.h / _scale; atlas = _atlas; calculateVars(); }
void OnEnable() { atlas = TPackManager.getAtlas ("World"); aTexture = atlas.getTexture(atlas.frameNames [index]); //aTexture.calculateVars(); boundsTexture = atlas.getTexture("MAT_Dirt"); }
static void OutputSpriteWithPlist() { //getdic Object selobj = Selection.activeObject; string selectionPath = AssetDatabase.GetAssetPath(Selection.activeObject); if (!selectionPath.EndsWith(".plist") && !selectionPath.EndsWith(".txt")) { EditorUtility.DisplayDialog("Error", "Please select a plist file!", "OK", ""); return; } Debug.LogWarning("#PLisToSprites start:" + selectionPath); string fileContent = string.Empty; using (FileStream file = new FileStream(selectionPath, FileMode.Open)) { byte[] str = new byte[(int)file.Length]; file.Read(str, 0, str.Length); fileContent = GetUTF8String(str); Debug.Log(fileContent); file.Close(); file.Dispose(); } //去掉<!DOCTYPE>,不然异常 int delStart = fileContent.IndexOf("<!DOCTYPE"); if (delStart >= 0) { int delEnd = fileContent.IndexOf("\n", delStart); fileContent = fileContent.Remove(delStart, delEnd - delStart); } Debug.Log(fileContent); //解析文件 PList plist = new PList(); plist.LoadText(fileContent);//Load(selectionPath); TPAtlas at = new TPAtlas(); at.LoadX(plist); Dictionary <string, bool> rotatoDic = new Dictionary <string, bool>(); foreach (var item in at.sheets) { if (item.rotated) { rotatoDic[item.name] = true; } } //output string texPath = Path.GetDirectoryName(selectionPath) + "/" + at.realTextureFileName; Texture2D selTex = AssetDatabase.LoadAssetAtPath <Texture2D>(texPath); string rootPath = Path.GetDirectoryName(selectionPath) + "/" + Selection.activeObject.name; TextureImporter textureImporter = AssetImporter.GetAtPath(texPath) as TextureImporter; int i = 0; //EditorUtility.DisplayProgressBar("unpack sprites", "start", 0); foreach (var spmeta in textureImporter.spritesheet) { i++; //if (i > 10) //{ // break; //} bool rotate = false; if (rotatoDic.ContainsKey(spmeta.name)) { rotate = true; Debug.LogWarning("conv ret:" + spmeta.name); } EditorUtility.DisplayProgressBar("unpack sprites", spmeta.name, (float)i / (float)textureImporter.spritesheet.Length); string path = rootPath + "/" + spmeta.name; string selectionExt = System.IO.Path.GetExtension(spmeta.name); if (selectionExt != ".png" && selectionExt != ".PNG") { path += ".png"; } string subDir = Path.GetDirectoryName(path); if (!Directory.Exists(subDir)) { Directory.CreateDirectory(subDir); } Debug.Log("output :" + path); SavePriteToPNG_Meta(selTex, spmeta, path, rotate); } EditorUtility.ClearProgressBar(); AssetDatabase.Refresh(); }
private void renderAtlasPreview() { string[] atlasesNames = TPAssetPostprocessor.atlasesNames; EditorGUIUtility.LookLikeInspector(); GUILayout.BeginHorizontal(TexturePackerStyles.toolBarBoxStyle); GUILayout.FlexibleSpace(); EditorGUI.BeginChangeCheck(); if (atlasIndex == -1) { int i = 0; string n = TPEditorData.selectedAtlasName; foreach (string atName in atlasesNames) { if (atName.Equals(n)) { atlasIndex = i; break; } i++; } if (atlasIndex == -1) { atlasIndex = 0; } } atlasIndex = EditorGUI.Popup( new Rect(-10, 0, TexturePackerStyles.ATLAS_POPUP_WIDTH, 20), "", atlasIndex, atlasesNames, TexturePackerStyles.toolBarDropDwonStyle); if (EditorGUI.EndChangeCheck()) { images.Clear(); selection.Clear(); TPEditorData.selectedAtlasName = atlasesNames [atlasIndex]; } GUIStyle btnStyle = EditorStyles.toolbarButton; if (GUILayout.Button("Add To Animation", btnStyle)) { addSelectedFrameToAnimation(); } if (TPEditorData.isExtensionsEnabled) { btnStyle = TexturePackerStyles.toobarEnabledButton; } if (GUILayout.Button("Extensions", btnStyle)) { TPEditorData.isExtensionsEnabled = !TPEditorData.isExtensionsEnabled; } search = GUILayout.TextField(search, GUI.skin.FindStyle("ToolbarSeachTextField"), TexturePackerStyles.FixedWidth(200f)); if (GUILayout.Button("", GUI.skin.FindStyle("ToolbarSeachCancelButton"))) { // Remove focus if cleared search = ""; GUI.FocusControl(null); } GUILayout.EndHorizontal(); scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Width(editor.position.width), GUILayout.Height(editor.position.height - 17f)); GUILayout.Box("", GUIStyle.none, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(_maxY) }); _maxY = 0f; TextureNodeRenderer.calculateVars(editor.position.width - 10f); currentAtlas = getAtlas(atlasesNames[atlasIndex]); Rect r; int index = 0; images.Clear(); foreach (string name in currentAtlas.frameNames) { if (search != string.Empty) { if (!name.ToLower().Contains(search.ToLower())) { continue; } } r = TextureNodeRenderer.RenderNode(index, currentAtlas, name); rememberImageRect(name, r); setNodeY(r.y + r.height * 1.5f); index++; } EditorGUILayout.EndScrollView(); }