atlas tool like ngui

hi i was just wandering if there was an asset on unity that can make an atlas like ngui, and will have a list of the textures that can be used on object. so i can click the object and say use image 1 in atlas out of 10 images

EDIT :

So I have been working on this, and thanks to a script supplied by @heaversm , I have made a solution that applies a texture by name to a plane created with heaversm’s script. The result is automatically assigned UVs for the textures and all in 1 drawcall =]

Download the full package here, it contains all scripts and a sample texture sheet to show proof-of-concept : http://www.alucardj.net16.net/unityanswers/TexturePackerParser.unitypackage

To make Atlas : Create your atlas and text file with Texture Packer (free version available http://www.codeandweb.com/texturepacker ). Remember to change the information file to .txt format. Note - you can save your texture packer project, and as long as you don’t move the images on your computer, the project can be opened and other images then can be added or removed.
In Unity, set the texture import settings, then create a material with your Atlas (also assign shader).

To use the Reader : Place the script on an empty gameObject. In the script, set up a variable and typecast it to GameObject (e.g. public var object1 : GameObject;). Then to assign an image, In the Start function call the method AssignMaterials with the object variable name and the Image in the Atlas you wish to use.
Drag-and-drop the Material and the image information Text File into the inspector. Drag-and-drop the object into the Inspector. That’s it ! You can call AssignMaterials in any function to change the image (with some scripting it would be possible to play a sprite-sheet animation).

To use the Plane Creator : This is a modified version of heaversm’s script. I have locked it to a single quad, and changed the Menu location. In Unity, there should now be a new option - Custom Item. Click on this, then click on Custom Simple Plane … Set the Width, Length, Orientation (Vertical), optional Collider and the name for the object, then hit create. Done.

And that’s about it. Please give it a try and leave feedback on what you think, I am very excited at how this works. I have uploaded a package because it’s much easier than upping each script and explaining where it goes, and with an example project it should be easy to work out how to use it for yourself. Happy Coding =]

http://www.alucardj.net16.net/unityanswers/TexturePackerParser.unitypackage


After a long night (yes, I’m watching the sun come up!) I think I’ve cracked it. Actually had to ask a couple of questions myself to get it done, but I have the first stage of a working Texture Packer Reader completed. Woo-Hoo =]

I say first stage because there are a few considerations. Firstly this is a dirty script i.e. the values are hard-coded, but I have tested this on several Texture Packer atlases, and the text format stays the same so it always works (for me so far).

Also this script uses SetTextureOffset and mainTextureScale to change the UVs on the object. Using these commands the benefit of an atlas is lost, as SetTextureOffset increases the drawcall by one. So instead of 4 objects sharing an atlas having 1 drawcall, this has 4.

The Proper way to do this is to swap the UVs on the vertices. As I know you are interested in performance (from your other questions) I had to mention it. I can make Quad-Meshes and swap the UVs on the verts, but that will take alot more work (that’s why they charge for NGUI, FlyingText3D, etc). Also I am still trying to perfect procedural meshes in ExecuteInEditMode.

Anyway, here is a working reader. You can download Texture Packer and make your own atlases to test it for yourself (or I can upload some sample textures and text files).

I have left in some commented lines, these can be used to read a texture instead of a material. To use a texture instead of a material, comment out any line with textureAtlasMaterial, and un-comment any line with textureAtlas. Just make sure you have a suitable material with shader on your objects if you do this. The script will assign the texture by itself.

To test and learn to use this :

  • Create an atlas and a text file, make the atlas as a material, drag and drop the text file and the material in the Inspector.

  • Then Create 4 cubes, and drop them in the Inspector.

  • In the script, look for the line with the comment Change the image name HERE, type the name of the IMAGE you want to display here. (the name of the separate image in the atlas, Not the atlas name)

  • Hit Play =]

The 4 cubes should then be individually rendered to the name of the image you gave.

Hopefully this explanation and the screenshot shows what I mean.

Here’s the script !

#pragma strict

public var textureAtlasInfo : TextAsset;

//public var textureAtlas : Texture;
public var textureAtlasMaterial : Material;

private var textureInfo : Vector4;
private var textureWidth : float;
private var textureHeight : float;

private var tempInfo : String;
private var _words : String[];

// -- user objects --
public var cube1 : GameObject;
public var cube2 : GameObject;
public var cube3 : GameObject;
public var cube4 : GameObject;
// -- END --

function Start() 
{
 LoadTextureInfo();
 AssignMaterials();
}

function AssignMaterials() 
{
 // -- Code to assign material and set the Tiling and Offset -- 
 
 // cube1.renderer.material.mainTexture = textureAtlas;
 cube1.renderer.material = textureAtlasMaterial;
 var cube1_offsets : Vector4 = GetTexInfo( "box_checked.png" ); // ** Change the image name HERE
 cube1.renderer.material.SetTextureOffset ( "_MainTex", Vector2( cube1_offsets.x, 0 - cube1_offsets.y - cube1_offsets.w ) );
 cube1.renderer.material.mainTextureScale = Vector2( cube1_offsets.z, cube1_offsets.w );
 
 
 // cube2.renderer.material.mainTexture = textureAtlas;
 cube2.renderer.material = textureAtlasMaterial;
 var cube2_offsets : Vector4 = GetTexInfo( "joystick.png" ); // ** Change the image name HERE
 cube2.renderer.material.SetTextureOffset ( "_MainTex", Vector2( cube2_offsets.x, 0 - cube2_offsets.y - cube2_offsets.w ) );
 cube2.renderer.material.mainTextureScale = Vector2( cube2_offsets.z, cube2_offsets.w );
 
 
 // cube3.renderer.material.mainTexture = textureAtlas;
 cube3.renderer.material = textureAtlasMaterial;
 var cube3_offsets : Vector4 = GetTexInfo( "knob.png" ); // ** Change the image name HERE
 cube3.renderer.material.SetTextureOffset ( "_MainTex", Vector2( cube3_offsets.x, 0 - cube3_offsets.y - cube3_offsets.w ) );
 cube3.renderer.material.mainTextureScale = Vector2( cube3_offsets.z, cube3_offsets.w );
 
 
 // cube4.renderer.material.mainTexture = textureAtlas;
 cube4.renderer.material = textureAtlasMaterial;
 var cube4_offsets : Vector4 = GetTexInfo( "button_pause.png" ); // ** Change the image name HERE
 cube4.renderer.material.SetTextureOffset ( "_MainTex", Vector2( cube4_offsets.x, 0 - cube4_offsets.y - cube4_offsets.w ) );
 cube4.renderer.material.mainTextureScale = Vector2( cube4_offsets.z, cube4_offsets.w );
 
 // -- END --
}

function LoadTextureInfo() // Read the Text File
{
 //if (textureAtlasInfo == null || textureAtlas == null)
 if (textureAtlasInfo == null || textureAtlasMaterial == null)
 {
 Debug.Log("TEXT INFO FILE or TEXTURE is NOT FOUND");
 return;
 }
 
 //textureWidth = textureAtlas.width;
 //textureHeight = textureAtlas.height;
 textureWidth = textureAtlasMaterial.mainTexture.width;
 textureHeight = textureAtlasMaterial.mainTexture.height;
 
 tempInfo = textureAtlasInfo.text;
 
 tempInfo = tempInfo.Replace("\r", "");
 tempInfo = tempInfo.Replace("

“, “”);
tempInfo = tempInfo.Replace(” “, “”);
tempInfo = tempInfo.Replace(”{“, “”);
tempInfo = tempInfo.Replace(”}“, “”);
tempInfo = tempInfo.Replace(”:“, “”);
tempInfo = tempInfo.Replace(”,“, “”);
tempInfo = tempInfo.Replace(” ", “”);

 _words = tempInfo.Split("\""[0]);
 
}

function GetTexInfo( imageName : String ) : Vector4 // Call this to assign material UVs
{
 for (var i : int = 0; i < _words.length; i ++)
 {
 if ( String.Equals( _words*, imageName ) )*

{
textureInfo.x = parseFloat( _words[i + 5] ) / textureWidth;
textureInfo.y = parseFloat( _words[i + 7] ) / textureHeight;
textureInfo.z = parseFloat( _words[i + 9] ) / textureWidth;
textureInfo.w = parseFloat( _words[i + 11] ) / textureHeight;
}
}

return textureInfo;
}
* Only change things between the // ---- and // -- END --
* Calling the function GetTexInfo( “box_checked.png” ); returns the UV position and size for the image in the quotation marks.
Then the material is set to the correct UVs with cube1.renderer.material.SetTextureOffset and cube1.renderer.material.mainTextureScale . Like I said before , the proper way to reduce drawcalls is to swap the UVs on the verts with the Vector4 info.
Let me know what you think =]
![alt text][1]
[1]: http://www.alucardj.net16.net/unityanswers/TexPakEg.png

Hi! I’ve been trying TexturePacker and noticed that if you have no license, the exported texture has some “adds” remaining you have no license…is there any other free option? Thanks!