Search Unity

ex2D - the best 2D sprite solution for Unity [RELEASED]

Discussion in 'Assets and Asset Store' started by johnny_karas, Aug 24, 2011.

  1. soulburner

    soulburner

    Joined:
    Mar 25, 2012
    Posts:
    169
    That will only work for monospace font.

    For fonts with variable char width, you need to calculate line width according to each letter's pixel width.
     
  2. soulburner

    soulburner

    Joined:
    Mar 25, 2012
    Posts:
    169
    Anyway, I've made wrapping by myself :)
     
  3. nia1701

    nia1701

    Joined:
    Jun 8, 2012
    Posts:
    74
    you mean manually or you came up with a coded solution? Would you mind sharing if you came up with a coded solution?
    Thanks!
     
  4. soulburner

    soulburner

    Joined:
    Mar 25, 2012
    Posts:
    169
    spr - is exSpriteFont variable
    el - my own text-definition structure

    Didn't test it deeply, but in 1st look it works ok

    Code (csharp):
    1.     private function WrapText(s: String): String {
    2.        
    3.         var res: String = "";
    4.         var cur_word: String = "";
    5.         var cur_word_len: int = 0;
    6.         var cur_len: int = 0;
    7.        
    8.         for (var i: int = 0; i < s.Length; i++) {
    9.             var c: char = s[i];
    10.             if (c == "\n") {
    11.                 res += cur_word + c;
    12.                 cur_word = "";
    13.                 cur_word_len = cur_len = 0;
    14.                 continue;
    15.             }
    16.             var char_info: exBitmapFont.CharInfo = spr.fontInfo.GetCharInfo(c);
    17.             if (char_info == null) continue;
    18.             cur_word_len += char_info.xadvance + spr.tracking;
    19.             if (spr.useKerning  (i < s.Length-1)) { // kerning and not last char
    20.                 for (var k: exBitmapFont.KerningInfo in spr.fontInfo.kernings) {
    21.                     if ((k.first == c)  (k.second == s[i + 1])) {
    22.                         cur_word_len += k.amount;
    23.                         break;
    24.                     }
    25.                 }
    26.             }
    27.            
    28.             if (cur_len + cur_word_len > el.width) { // need wrap
    29.                 if (c == " ") {
    30.                     res += cur_word + "\n";
    31.                     cur_word = "";
    32.                     cur_len = cur_word_len = 0;
    33.                     continue;
    34.                 }
    35.                 if (cur_len > 0) { // else the word is too long to be wrapped
    36.                     res += "\n";
    37.                     cur_len = 0;
    38.                 }
    39.                 continue;
    40.             } else { // no wrap yet
    41.                 cur_word += c;
    42.                 if (c == " ") {
    43.                     res += cur_word;
    44.                     cur_word = "";
    45.                     cur_len += cur_word_len;
    46.                     cur_word_len = 0;
    47.                 }
    48.             }
    49.         }
    50.        
    51.         if (cur_word.Length > 0) {
    52.             res += cur_word;
    53.         }
    54.        
    55.         return res;
    56.        
    57.     }
     
    Last edited: Jul 21, 2012
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Wu,

    You mentioned you are replacing soft clip with Clip() in shader. This would be pretty bad for mobile if it works like http://http.developer.nvidia.com/Cg/clip.html

    Because that is a wrapper for Discard, which is really slow on mobile. Do you have any thoughts about that?
     
  6. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Also undocumented in 3.5.4:

    .MarkDynamic
    .Colors32

    Which will speed up dynamic mesh modification ie vertex color and changing vertex positions... I am not able to find any documentation but apparently they are in.
     
  7. vvander

    vvander

    Joined:
    Jan 18, 2011
    Posts:
    72
    Hey there, I'm having an issue running an animation.

    My movement animation has its wrapmode set to "loop", and it does work correctly when doing movement in the main thread of execution. The problem happens when I'm trying to play the same movement animation at a different time with a coroutine...

    Code (csharp):
    1. ...
    2. abilityUser.GetComponent<exSpriteAnimation>().Play("Walk Side");
    3. Debug.Log(abilityUser.GetComponent<exSpriteAnimation>().IsPlaying("Walk Side"));
    4. yield return new WaitForSeconds(.5f);
    5. Debug.Log(abilityUser.GetComponent<exSpriteAnimation>().IsPlaying("Walk Side"));
    6. ...
    The first Debug.Log statement returns "true", and the second one returns "false". It seems that the animation is being cut off when the yield statement is called, and this is evident when playing the game, too. Does this mean that looping animations don't work inside coroutines like this? There aren't any errors showing up. Any help would be appreciated!
     
  8. Niokizou

    Niokizou

    Joined:
    Jul 3, 2012
    Posts:
    3
    Hello ! I just want to know if with Ex2D I can repeat a texture. Currently when I try to scale my SpriteObject, the texture is scaled to, I want to know if the sprite can kept the original texture and repeat it on the object :)

    Thanks in advance !
     
  9. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hi Coldsteel,

    Did you mean the FSM in exUnity? Though this is not part of ex2D, I can reply you here:

    Code (csharp):
    1.  
    2. public class Actor : FSMBase {
    3.  
    4.     void Start () {
    5.         if (stateMachine != null) {
    6.             stateMachine.Start();
    7.         }
    8.     }
    9.    
    10.     void Update () {
    11.         stateMachine.Update();
    12.     }
    13.  
    14.     protected override void InitStateMachine () {
    15.  
    16.             fsm.State calmDown = new fsm.State( "calmDown", stateMachine );
    17.             calmDown.onEnter += EnterIdleState;
    18.             calmDown.onAction += UpdateIdleState;
    19.  
    20.             fsm.State fever = new fsm.State( "fever", stateMachine );
    21.             fever.onEnter += EnterFeverState;
    22.             fever.onAction += UpdateFeverState;
    23.  
    24.             fsm.State danger = new fsm.State( "danger", stateMachine );
    25.             danger.onEnter += EnterDangerState;
    26.             danger.onAction += UpdateDangerState;
    27.  
    28.             fsm.State lose = new fsm.State( "lose", stateMachine );
    29.             lose.onEnter += EnterLoseState;
    30.  
    31.             fsm.State sleep = new fsm.State( "sleep", stateMachine );
    32.     }
    33.  
    34.     // define your functions UpdateIdleState, EnterIdleState, ....................
    35.     // ------------------------------------------------------------------
    36.     // Desc:
    37.     // ------------------------------------------------------------------
    38.  
    39.     protected void EnterIdleState ( fsm.State _from, fsm.State _to, fsm.Event _event ) {
    40.         faceAnim.Play("idle");
    41.     }
    42.  
    43.     // ------------------------------------------------------------------
    44.     // Desc:
    45.     // ------------------------------------------------------------------
    46.  
    47.     protected void UpdateIdleState ( fsm.State _curState ) {
    48.         UpdateMyMood ();
    49.     }
    50.  
    51. .......................
    52. }
    53.  
    54.  
     
  10. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hi Soulburner,

    There is no text wrap in current version. I've add this in the to do list of ex2D GUI.

    Regards,
    Wu
     
  11. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hi hippocoder,

    Yes, you are right. I found the clip very slow in iOS device. And I change it to:

    Code (csharp):
    1.  
    2.                 if ( 1.0 - max ( factor.x, factor.y ) <= 0.0f )
    3.                     outColor.a = 0.0f;
    4.  
    I'm not sure if this is faster in mobile device. Any suggestion?
     
  12. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hi hippocoder,

    Can you explain more about this? Thanks in advance.
     
  13. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hi Wander,

    I can't judge the problem by the script, can you send me an example project of this issue? Thanks in advance.
     
  14. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hi Niokizou,

    If you want to repeat a texture, that means you will not use Atlas, but the single texture and set its filter mode to repeat. In current version of ex2D, this is not well supported. You have to put a texture to sprite without making it in the atlas, then set the tilling of the sprite's material to the value you wish.

    This feature will be improved in v2.0.

    Regards,
    Wu
     
  15. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Hi Wu,

    Regarding shader performance, there really isn't much we can do on mobile. Discard is slow because it's a branch, the same as your code (essentially) however in newer hardware, this could dramatically speed up. But for existing devices, it's always going to be slow...

    http://docs.unity3d.com/Documentation/ScriptReference/GL.html -- the GL class is now available to 4.0 indie users (I am not sure about 3.5.4) and it will work fine on directx (the calls are wrapped) - perhaps scissor is available ( http://www.opengl.org/sdk/docs/man/xhtml/glScissor.xml ) ?

    It looks as though it is just a slow thing to deal with regardless and if you are dealing with quads (no shear or rotation) then altering uv and mesh verts seems the quickest way for older devices, but you did that before right? it was also difficult to use.

    Regarding the new commands, I do not know much more information, it is on the beta test notes for 3.5.4 but no further information was given. If you are not on beta yet, I am sure unity staff will be happy to add such a well respected middleware author like you to the list.
     
  16. jackmarolop

    jackmarolop

    Joined:
    Jul 23, 2012
    Posts:
    1
    Hi Wu...


    How import a texture file "*.png" in a folder asset (exp:"SreamingAsset") to a gameobjeck ex2D by scripting?

    IEnumerator LoadTexture(string Name)
    {

    WWW LocationTexture= new WWW("file://" + Application.dataPath + "/SreamingAsset/" + name);
    yield return LocationTexture;

    }
    GameObject Objek = new GameObject("Tree");
    Objek.AddComponent("exSprite");
    Objek.AddComponent("BoxCollider");
    Objek.transform.position = new Vector3(0, 0, 0);
    Objek.renderer.material.mainTexture = LocationTexture.texture;
    Objek.renderer.material.shader = Shader.Find("ex2D/Alpha Blended");

    }

    void Start()
    {
    StartCoroutine("Tree");
    }
     
    Last edited: Jul 23, 2012
  17. harlock1975

    harlock1975

    Joined:
    Feb 27, 2012
    Posts:
    46
    Hi Wu,
    this is not strictly ex2d related but maybe you can save me some time.
    I am trying to use the SetPivot script here http://unifycommunity.com/wiki/index.php?title=SetPivot with ex2d, but it does not work as expected (well, for me anyway).
    I am trying to set the pivot point for the sprites I use to create a complex character, but the pivot is not correctly positioned while moving the position sliders.
    Can the texture offset or any other sprite manipulation interfere with the pivot modification? Is there anyone that has succeeded in using ex2d woth the SetPivot script?
    Thanks!
     
  18. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hi hippocoder,

    I'm looking forward to test Unity 4 if it support scissor. The soft clip is currently been deprecated and will be re-opened in ex2D v2.0 since it has conflict with current code. I test this in iOS device and I found, most of the game deal UI(with clipping) in a light environment, which means it doesn't care frame rate too much. I need to do more research for a conclusion, thanks for your advice.
     
  19. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hi Jackmarolop,

    What you are trying to do is load a texture not in atlas and set it on exSprite. This is not a recommend way in using ex2D. You should put this texture in an atlas, and instead of download the texture, download the atlas and set the texture in it by index (the atlas should in an AssetBundle). So the script will be:

    Code (csharp):
    1.  
    2.  
    3. sprite.SetSprite( download_atlas, index );
    4.  
    5.  
    If you insisting in using a single texture for sprite. You need to make a sprite as prefab and make sure there will be a mesh on it first (no empty texture). Then change its material like what you've write above.

    The current version of ex2D is not well support for setup single texture. But this problem will be solve in ex2D v2.0 based on the new structure and working pipeline.
     
  20. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hi harlock1975,

    I never use this script before, so I don't think ex2D works fine with pivot. Let me research on it first and give you the answer.
     
  21. Niokizou

    Niokizou

    Joined:
    Jul 3, 2012
    Posts:
    3
    Many thanks for your answer ! I have also posted the answer on Unity Answer for ex2D.

    I have another question (I'm sorry but I am a real beginner :) )

    I am currently working in two resolutions (960*640 480*320) for my game. By default, my camera is in 960*640 and my Target in the player settings is Native. But the textures are too big for my iphone 3GS. So I have made a script to test on which device is the user and I resize my camera if the player is in 480*320. But I want to know if with ex2D I have an easy way to associate my Sprite object with another texture and Atlas (with my 3GS assets)?

    Thanks in advance !
     
  22. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hi Niokizou,

    The current version of ex2D didn't well support multi-resolution. So you can not switch texture depends on different resolution smartly. However, you can use the pixel-perfect option in exSprite inspector, this option allow the sprite re-size automatically to fit the different size of screen.

    Regards,
    Wu
     
  23. soulburner

    soulburner

    Joined:
    Mar 25, 2012
    Posts:
    169
    Dear Wu Jie, I've purchased indie version, thank you again for your job!

    I've found one little but very dissapointing bug with fonts.

    In a few words - you need to Mathf.Round() x-coordinate of a string when making center-adjusted font field.

    Take a look. Here is a text (placed at integer coords) that is adjusted to left. It is ok:

    $font1.png

    But when I make font center-adjusted, some lines become blurry:

    $font2.png

    I think that it happens when you're calculating the middle coordinate and you should simply add Round() there.

    Thank you again and I'm hoping for a soon fix ;)

    PS: Is there any way to turn off font filling with color and just take the symbols just as they are from a font atlas? If no, can you make it?
     
    Last edited: Jul 26, 2012
  24. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hi Soulburner,

    Thanks for the advice, I'm not sure if Round() calculate will cause any problem, so I will test this first.

    If you wish to use white color fonts, you can fill the color in your font resource(texture).
     
  25. soulburner

    soulburner

    Joined:
    Mar 25, 2012
    Posts:
    169
    I thought about rounding because as I can see some lines look sharper then others. The only reason that I could imagine is that some are positioned at integer coordinates and some - are at float coords. The odd line length can easily cause this problem.

    Sorry, I didn't understand your answer. For example I have a font atlas with all effects and colors drawn there and I don't want ex2D engine to add any effects and color gradients. How can I do that? I can see where to turn off shadow and outline, but how do I turn off color filling?
     
  26. bec_bunsen

    bec_bunsen

    Joined:
    Mar 2, 2012
    Posts:
    40
    Hi Wu, hi Nantas !

    I just wanna say thank you !
    I love the last update :)

    My project progress slowly but surely.

    Keep on doing this great job with ex2D.
     
  27. lastprogrammer

    lastprogrammer

    Joined:
    Apr 30, 2011
    Posts:
    166
    I'm having problem with draw calls still. I have seven objects that all use the same atlas but they are all scaled differently. When I run the game I am getting an extra seven draw calls. Isn't the atlas supposed to reduce draw calls, or is the scaling messing that up? Thanks.
     
  28. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Some of us work at very small resolutions with ex2D (<0.1) so make sure rounding doesn't break anything.
     
  29. harlock1975

    harlock1975

    Joined:
    Feb 27, 2012
    Posts:
    46
    Hi Wu, any chance you had a look at this?
    Thanks!
     
  30. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hi soulburner,

    What I mean is you should make a font resource that take no effects but just white color. If a font resource already have color in it, you still can use Photoshop or other image process tool remove those color in the font texture, and use it. Because this is very easy to achieved by 3rd tools, I don't think it is necessary for ex2D add another function to remove effects in the font ( otherwise, people will need more but forget they have Photoshop or Gimp).
     
  31. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hi lastprogrammer,

    Based on Unity's document http://docs.unity3d.com/Documentation/Manual/iphone-DrawCall-Batching.html, the different scale will break up batches. So remember use exSprite's scale instead of Transform's scale.
     
  32. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Not exactly, if user in 3D environment, round will cause problem in raster.

    Agree, I need more test to confirm if this should be done in ex2D, otherwise it is a Unity rendering problem.
     
  33. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hi harlock,

    The site been Hacked I doubt. But based on the last time I read the script, I don't think it will be compatible with ex2D. Since ex2D also provide anchor that help user define their Pivot position in the mesh.
     
  34. decoy98

    decoy98

    Joined:
    Aug 30, 2011
    Posts:
    36
    Hello Wu Jie,

    Just a small question: is it better(in terms of optimization) to have separate atlases for each game objects even if they are in the same category? So for example, right now, I have all my atlases separate for powerup1, powerup2, powerup3...

    Or
    Should I put all my images in one powerup atlas for better optimization?
     
  35. soulburner

    soulburner

    Joined:
    Mar 25, 2012
    Posts:
    169
    And maybe you will make an option to turn off all effect so that engine will take the symbols just as they are from the font atlas without adding anything?

    I'm asking because GlyphDesigner can make more beautiful effects than ex2D makes. So I'd like to make effects in GlyphDesigner and use them in the game.
     
  36. soulburner

    soulburner

    Joined:
    Mar 25, 2012
    Posts:
    169
    Maybe you should add rounding only in pixel-perfect mode.

    All I'm asking is to make Mathf.Round(linewidth/2) where you make something like this to calculate center point.
     
  37. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hi decoy88,

    It is better to put all powerup image in one atlas.
     
  38. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hello soulburner,

    If you turn off outline, shadow and set the sprite font's top and bottom color to white, it will not apply any additional effect to the font. We are using GlyphDesigner in our game exactly what you describe and nothing wrong in setting up sprite font in this way.

    Regards,
    Wu
     
  39. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Let me do more test and give you a conclusion.
     
  40. soulburner

    soulburner

    Joined:
    Mar 25, 2012
    Posts:
    169
    Ah, thanks! Got it!
     
  41. soulburner

    soulburner

    Joined:
    Mar 25, 2012
    Posts:
    169
    Got a strange bug.

    I've used exClipping in my game. It works fine when run in Unity, but crases with error when running as standalone (.exe) file.

    PHP:
    NullReferenceException
      at 
    (wrapper managed-to-nativeUnityEngine.Material:Internal_CreateWithShader (UnityEngine.Material,UnityEngine.Shader)
      
    at UnityEngine.Material..ctor (UnityEngine.Shader shader) [0x00000in <filename unknown>:
      at exClipping
    .ApplyClipMaterial (.exPlane _plane) [0x00000in <filename unknown>:
      at exClipping
    .AddPlane (.exPlane _plane) [0x00000in <filename unknown>:
      at _MY_LINE_OF_CODE_
    _MY_LINE_OF_CODE_ is:

    exclipping.AddPlane(xxxxx);

    I don't know if I use exClipping component correctly. Let me explain what I did.

    Well, I needed to clip a group of planes on the screen. So, I'm creating a new exClipping object:

    PHP:
            go = new GameObject();
            
    exclipping go.AddComponent("exClipping") as exClipping;
            if (
    exclipping == null) {
                
    Debug.LogError("Error: exclipping is null (couldn't fint component?)");
                return;
            }
    And then adding exSprites to it using the line that crashes the build:

    PHP:
    exclipping.AddPlane(xxxxx);
    The same code works fine when run in Unity.
     
    Last edited: Jul 31, 2012
  42. soulburner

    soulburner

    Joined:
    Mar 25, 2012
    Posts:
    169
    I've taken your sources from github and compiled them. So, I've tested version with Round() and without.

    Take a look at lines marked with a red dot:

    $ex2d1.png

    They are blurry at the left picture and sharp at the right.

    I've added Mathf.Round() at lines 606 and 626 of "exSpriteFont.cs"
     
  43. nia1701

    nia1701

    Joined:
    Jun 8, 2012
    Posts:
    74
    I'm sorry, I am really confused on how to play an animation in reverse? I tried to implement the code in javascript:
    Code (csharp):
    1.  
    2. var spriteAnim : exSpriteAnimState;
    3. spriteAnim = AO.player.GetComponent(exSpriteAnimState);
    4.  
    5. spAnim.Play("BikThrowFront");
    6. spriteAnim.GetCurrentAnimation().speed = -1.0;
    7.  
    I get GetCurrentAnimation is not a member of exSpriteAnimState.....
     
  44. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hi soulburner,

    This is because the clipping shader didn't included when you build the game. Since the clipping code instantiate material by Shader.Find("..."), it will not have reference automatically. So a way to solve it is put the Alpha Blended (Clipping) shader in the Resources folder. I will seek a better way to make it automatically referenced in the scene.
     
  45. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hi trothmaster,

    GetCurrentAnimation() is a member of exSpriteAnimation NOT exSpriteAnimState, exSpriteAnimState is some instance of anim-clip used in exSpriteAnimation. So your code should be:

    Code (csharp):
    1.  
    2. var spriteAnim : exSpriteAnimation;
    3. spriteAnim = AO.player.GetComponent(exSpriteAnimation);
    4. spriteAnim.GetCurrentAnimation().speed = -1.0;
    5. spriteAnim .Play("BikThrowFront");
    6.  
     
  46. johnny_karas

    johnny_karas

    Joined:
    Sep 20, 2010
    Posts:
    704
    Hi soulburner,

    Thanks for your advice and the test, I've apply the code in sprite font, I also change some of the code in sprite and some other place. I'm still testing if this change will not affect other things, so a fix build will come out later (probably this weekend.)
     
  47. nia1701

    nia1701

    Joined:
    Jun 8, 2012
    Posts:
    74
    Thank you!
     
  48. soulburner

    soulburner

    Joined:
    Mar 25, 2012
    Posts:
    169
    Thanks a lot.

    I think the most easy way to fix it is just moving shaders into resources folder. Don't see any reasons why not to make it the default place for storing these files :)
     
  49. BTStone

    BTStone

    Joined:
    Mar 10, 2012
    Posts:
    1,422
    Hey there,

    when do you plan to release your own TileMap-Editor? I am asking because i am about to buy this here: http://u3d.as/content/mudloop/uni-tile/1R0
    but on the Homepage the Developers just mention it works with SpriteManager and SpriteManager 2. Ex2D is not mentioned, but it still could work, i think.
    Currently i am using your indie license v1.2.5, should i wait for your own TileMap-Editor or would it take to long?
     
  50. Dragonhill

    Dragonhill

    Joined:
    Jun 2, 2010
    Posts:
    22
    Hi Wu, Thanx for the heads up. When can we expect 1.2.6 to be released?
    ~D