Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SM2 - 2D made easy and efficient!

Discussion in 'iOS and tvOS' started by Brady, Nov 4, 2009.

  1. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    I actually haven't heard any specifics about it yet. I'm looking forward to it though as whatever it winds up being, it will help me take the next iteration of SpriteManager where I've been wanting to take it but couldn't as yet since I've been having to work around Unity's limitations in that area. I still have lots of ideas of things I want to implement that can make the process of working with sprites even easier, and make Unity a much more powerful tool for 2D games. But since Unity doesn't comprehend the idea of a "sprite" currently, it's been difficult to pursue these kinds of solutions. Hopefully with this new feature will give me the flexibility to finally make these ideas a reality for you guys! :D

    While I doubt the new 3.0 stuff duplicates all the SM2 functionality, I'm excited for what it will make possible for the next SM2/3!

    BTW, if anyone is at GDC and can find out any details about this from the guys there, drop me a PM as I'd love to get a jump start on laying the groundwork for the next SpriteManager!
     
  2. Andrej-Vojtas

    Andrej-Vojtas

    Joined:
    Jan 12, 2009
    Posts:
    67
    The new auto-trimming feature in SM2 (image-based offset) and packed atlas building is alone worth a SM2 purchase for me and there is so much more and still new things coming. Great job :)

    The native 2D sprite support is something I was really really hoping for to be in Unity 3.0. The official press releases mentions the 2D sprite support in connection with the Unity iPhone 3.0. Do you guys expect it to be also in the standalone Unity 3.0 for PC, OS X and web?

    This question was asked in the official Unity 3.0 announcement thread, but wasn't clarified yet (just a hang in there the answers are coming type of answer) I know it's just speculation, but what do you guys think?
     
  3. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    I detected a hint in what Higgy said that made me think that the sprite engine might be iPhone-only. But surely that wouldn't be the case since I can't think of any good reason why it wouldn't be supported in "regular" Unity. It could be Pro-only, perhaps. But I guess we'll just have to wait and see. If anyone is at GDC, I'd love to see if anyone could get some clarification on this as well as the feature set.

    As for SM2, etc, thanks for the kind comments. Whatever the feature set winds up being, I fully intend to keep pushing the envelope and adding more capabilities and ease-of-use. :)
     
  4. blastone

    blastone

    Joined:
    Apr 7, 2009
    Posts:
    168
    I've just recently bought your SM2 stuff, previously I used SM1 and built all my gameObjects dynamically in code, then attached sprites to them.

    I dont want to do it using prefabs as I have all my values and UVS currently setup, I mainly want to upgrade to use the dynamic batching.

    As such is it possible for you to show me a snippet of code that takes a gameObject in code and then attaches a new Sprite to it. That would be awesome.

    Until then Ill revert back to SM1 for my project as I don't need all the visual stuff and generate all my sprites in code.

    Many thanks.
     
  5. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    Sure. You should be able to do the following using the latest beta (just recently updated):
    Code (csharp):
    1.  
    2. GameObject go = new GameObject();
    3. Sprite sp = (Sprite) go.AddComponent(typeof(Sprite));
    4. sp.Setup(width, height, lowerLeftPixel, pixelDimensions, materialToUse);
    5.  
    Where materialToUse is a reference to the material you want to use for the sprite. The other arguments should be familiar.
     
  6. blastone

    blastone

    Joined:
    Apr 7, 2009
    Posts:
    168
    Thanks for that.

    Another question for you now. Iv'e double clicked the 2_1 package and imported all the items, but now get a glaringly red error

    Assets/Editor/BuildAtlases.cs(92,33): error CS0117: `Selection' does not contain a definition for `activeGameObject'

    along with a swathe of warnings.

    I can run the demo fine when I have it as a separate project, but bringing into my existing project enters me into a whole world of trouble.
     
  7. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    That's very strange. What version of Unity are you using? The line in question is just calling a standard Unity class (Selection) so I'm not sure how that could be going wrong since Selection does, indeed, have a member called "activeGameObject" in all current Unity versions I know of.

    Also, I recommend using the latest beta as it includes several important fixes and improvements that will soon be committed to a 1.0.1 release.
     
  8. blastone

    blastone

    Joined:
    Apr 7, 2009
    Posts:
    168
    the latest Unity Iphone (1.6) professional.
    Ill pull down the latest Beta and also try with that (I downloaded whatever was there yesterday)
     
  9. jitsua

    jitsua

    Joined:
    Apr 1, 2009
    Posts:
    20
    more noob questions :( sorry. What is the best technique for upgrading a RC5 unity project to 1.0. If I import over the top. All the PackedSprite components become corrupted. (either reverting to MonoBehaviour or having numbers only in the anchor combo box.) Rebuilding the atlases caused alpha issues. Do the materials need reconstructing? Or do I have to recreate the entire scene again? I also miss not having .staticUVs property as I had been using it to extract all the UVs to XML from within Unity Editor. Or to swap textures from the same atlas at run time. Should the GetUVs function do the trick? for runtime swaping? Or do I also need to modify the mesh to account for translation. Thanks.....
     
  10. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    @blastone
    Did you get it working?

    @jitsua
    This sounds like you might be deleting the old scripts before importing the new? This would cause Unity to lose the references to anything in those scripts.

    Can you describe the issues? There shouldn't be any issues, other than empty alpha regions of the sprite frame will get cropped as shown in the image-based offset video, but that shouldn't cause problems as the sprite still gets offset to match the location it would be in if the empty spare were still there (this saves a ton of texture space).

    You shouldn't have to recreate anything.

    If you're pulling that out at edit-time, use ._ser_uvs instead. If you're pulling it out at runtime, you can use .staticFrameInfo (which contains a 'uv' Rect).

    GetUVs will get the current UVs - not just the static ones. So if an animation is playing, you'll get the current UVs for the current frame. No need to modify the mesh.
     
  11. jitsua

    jitsua

    Joined:
    Apr 1, 2009
    Posts:
    20
    If I load up a scene, that contains RC5 PackedSprites.
    I select Assets->Import package
    Select SpriteManager2_1.0.unityPackage
    and open.
    Select ok to replace all SM2 items.
    Go to code and fix all missing references to .staticUVs
    After hitting run, all the sprites turn to a flat untextured slightly alpha'd quad. (see attached).
     

    Attached Files:

  12. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    Have you rebuilt your atlases? Also, I recommend using the latest beta available on the group as it includes some fixes. Also be sure when rebuilding your atlases to note the info in the docs under the section labeled "Scan Project Folder".
     
  13. Lokken

    Lokken

    Joined:
    Apr 23, 2009
    Posts:
    436
    So I have a simple GameObject with a PackedSprite attached and a static texture assigned.

    I instantiate it 33 times to lay out my game board. I was under the impression they should only be one draw call, but there are 33 separate.

    I have attached the CombineChildren script but it stops the pixelperfecet/auto-resize from taking effect.

    For some reason I thought that if the sprites shared a material they would be a single draw call?

    L
     
  14. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    All things being equal, they will get batched (in Unity iPhone). But there are caveats to batching with blended shaders. See the troubleshooting item in the SM2 docs titled, "I am getting more than 1 draw call for each material I'm using". It's at the end. Though to be clear, this is not an SM2 issue, it is just a fact of the state of rendering technology. But there are workarounds, as suggested in the SM2 troubleshooting.

    If this isn't a blending-related issue, the other thing to check is to make sure you aren't making any changes directly to the materials of your sprites, which will cause Unity to duplicate them, causing multiple draw calls.

    Let me know if you need any further assistance.
     
  15. Lokken

    Lokken

    Joined:
    Apr 23, 2009
    Posts:
    436
    I believe you nailed it. I am working in Unity Desktop and didn't realize that dynamic batching wasn't functional.
     
  16. Lokken

    Lokken

    Joined:
    Apr 23, 2009
    Posts:
    436
    Actually I have a follow-up question that may be of help to others if they run into the same thing.

    The SM2 Documentation suggests that you use the SetColor() method instead of directly changing a sprite's material properties.

    So if I have 3 objets that use the same material I can set their colors to 3 different colors and remain at 1 draw call?

    My gut reaction says no, but I want to make sure I am not missing something simple.

    Thanks!
     
  17. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    Yes, you can. SM2 uses vertex coloring to accomplish this. This lets you do tinting, as well as fading of a sprite (by changing the alpha). Just be sure you're using a shader that supports vertex coloring, such as the ones provided with SM2. But yes, with SM2, changing the color/alpha of multiple sprites is possible with a single draw call. Just be sure to use SetColor() to do it.
     
  18. Lokken

    Lokken

    Joined:
    Apr 23, 2009
    Posts:
    436
    You tha man
     
  19. aerende

    aerende

    Joined:
    Apr 27, 2009
    Posts:
    316
    I'm getting some strange sizing behavior with Sprites w/SM2 and Prefabs and have a few questions.

    A) Simple Prefabs with Sprite Script

    1) I created an atlas with six PNGs that were each 200x200 pixels using a GameObject and Packed Sprite.
    2) Then I make a GameObject containing a Mesh, Mesh Renderer and the Sprite Script set to Pixel Perfect. The Mesh Renderer uses the Material from 1) above as its Element 0. I set the Lower Left Pixel of the Sprite Script to (0, 1024) and the Pixel Dimensions to (200, 200).
    3) The Sprite Script says that the (Width, Height) = (104, 104).

    Q1) Any ideas why the (Width, Height) is not (200, 200)?

    B) Complex Prefabs with Packed Sprite

    1) I created a GameObject with Mesh, Mesh Renderer and PackedSprite. The PackedSprite contains animations and is set to Pixel Perfect. The GameObject's scale is set to (2.5, 2.5, 2.5). When I play the scene and the animation is played, the Width and Height of the Packed Sprite oscillates around (1.80, 2.80).

    2) Then I make a PreFab from the GameObject in 1) above and place the PreFab into an empty GameObject with Scale = (1, 1, 1) in another scene. Now when the animatinon plays, the Width and Height of the PackedSprite is around (29, 47).

    Q2) Any ideas why the size of the prefab is almost 10 times larger than the original GameObject even though the PackedSprite in the prefab has Pixel Perfect set?
     
  20. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    Q1) Make sure your camera is set to be orthographic. Once you've done that, the orthographic size will determine what the pixel-to-world-coordinate ratio will be. 200x200 are the pixel dimensions of your sprite, but the "width" and "height" members of the sprite are the worldspace dimensions. They are not the same thing. The only way these two would match is if your camera's orthographic size was such that there was a 1:1 correlation between world and screen space. This can be accomplished by setting the camera's orthographic size to half the height of your viewport. So for example, for an iPhone in landscape mode, you'd choose an orthographic size of 160. For portrait, 240. This means for every pixel on-screen, there is 1 unit of world space. But there's no need to do it this way unless you prefer the numeric correlation.

    Q2) It's tough to say without knowing other variables. It could be a camera issue. If you could export these to a package I could see and test, it would help. If that's possible, e-mail me privately (use the support e-mail address you received) and I'll have a look at it.
     
  21. aerende

    aerende

    Joined:
    Apr 27, 2009
    Posts:
    316
    Setting the camera to orthographic fixed the problem. Thanks!
     
  22. x4000

    x4000

    Joined:
    Mar 17, 2010
    Posts:
    353
    Just bought SM2 (for desktop purposes at the moment, not iPhone). It seems great for sprite animation and display for sure. I do have a couple of questions, though:

    1. Is it possible to do uv animations with this? For example, if I had a tileable rain PNG that I loaded into a sprite, could I size the sprite to fill the viewport and then use texture offsetting to basically make it look like the rain is falling? This might be better suited for outside SM2, but then I'm back to issues with the crispness and lack of pixel-precision.

    2. Is it possible to use additional shaders on an additional sprite? For example, if I want to work with the blend mode to set up additive blending for just a specific set of sprites, is that possible?

    3. Is it possible to have the game load a variety of images and build a sprite atlas at runtime? That way, for instance, I can set up my scripts to simply look for images in a certain folder, then load all of the images into an atlas, then set properties on the atlas through script, and then use the atlas on other objects like anything else? This would be good for making it so that players can "mod" the game and add custom sprite sets, etc, without loading Unity which is a goal of mine.

    Thanks for any help!

    Chris
     
  23. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    There are ways to do it. You could manually adjust the UVs over time using one of the SetUVs() methods. But to avoid precision issues, you'll need to make sure you use just enough of the image to fill the viewport.

    It is possible, but the other sprite(s) would have to use a different material, resulting in an additional draw call. There is no way in Unity to use different shaders within the same draw call. Also realize that automatic drawcall batching currently only works on Unity iPhone.

    Not with the current implementation. You could modify the sourcecode to do something like that perhaps, but it would be pretty extensive since things would be somewhat different doing it at runtime. Also, you wouldn't want to do it while any gameplay is running as it is very resource intensive to compile the atlases.
     
  24. x4000

    x4000

    Joined:
    Mar 17, 2010
    Posts:
    353
    Okay, fair enough.

    Regarding #1, sounds like that will be the ticket.

    Regarding #2, that is also not a problem since all of the sprites of a given sort will be using the same blend mode anyway. And I am aware of the draw batching limitation on non-iPhone implementations. We'll see how much of a limiter that turns out to be.

    Regarding #3, that's fair enough. I've started just importing my textures and am working with them as intended in Unity. That's a bit counter to my normal way of doing things, but so it goes.

    Thanks for the notes!
     
  25. x4000

    x4000

    Joined:
    Mar 17, 2010
    Posts:
    353
    Another question -- do you have any tutorials or tips anywhere about how to place sprites from SM2 in screen coordinates? Essentially, say I'm using an immobile camera for a puzzle game, how would I go about tiling some background textures (that are really Sprite objects)? Given that the size of the camera is a variant thing on the desktop, based on the player's chosen resolution, this seems to have a few problems.

    I suppose I could do ScreenToWorldCoordinates, but I'm not sure if that is going to lead to further problems with tiling things appropriately with the pixel perfect setting on. What sort of camera settings are supposed to be used in this sort of situation with SM2?
     
  26. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    There isn't really a generalized solution for something like this since every game will want to handle it a bit differently. But here are the basic considerations:

    Pixel-perfect will always display the sprite in question at a 1:1 pixel-to-pixel ratio, regardless of screen resolution. So at high resolution, the sprite will occupy a smaller percentage of the screen than at low resolution. If you want the graphical element in question to always occupy the same proportion of screen area regardless of resolution, then do not use pixel perfect (if you still need resizing for animations, keep autoResize checked). If, on the other hand, you want it to be pixel-perfect no matter what, then check pixel perfect. These two scenarios are necessarily mutually exclusive.

    For a bi-directionally tiling background, you're going to have to use an entire texture/material for that since there's no way to fit anything else onto it, or else it will show up in the tile sequence. So in that case, you don't necessarily have to use a sprite. You certainly can though, and just adjust the UVs so that it tiles properly. Probably the easiest way to do this is to use the SetUVsFromPixelCoords() and just pass it the screen resolution. Make sure your anchor is set to MIDDLE_CENTER, and position the sprite's GameObject in the center of the screen. It should expand, pixel-perfect, to exactly fill the viewable area. Also make sure your texture's wrap mode is not set to clamp.
     
  27. x4000

    x4000

    Joined:
    Mar 17, 2010
    Posts:
    353
    Okay -- that is good information, and actually answers some other questions I have. I'm used to using IDirect3DX sprites, which are far simpler to handle by contrast (they are more of an immediate-mode writing thing), but this is more flexible in terms of allowing uv changes and other effects.

    I guess my question is what is the best way to determine the "center of the screen" based on your notes there? Of course, that is more relevant for my rain example, above. In the case of what I was asking about here, I basically have a set of multiple 256x256 tiles that I want to set all next to one another starting at the top left of the screen. I'll worry with keeping screen dimensions to specific max values a bit later on (basically the max, pixel-perfect, would be 1280x768, and then it would scale down from there automatically cropping as it goes).
     
  28. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    The center of the screen can either be found taking the center screen pixel and translating it to a world point, or if you know your camera is at, say, (0,0,Z), then just make sure to position your background sprite at (0,0,Z+n), where n is the distance from the camera's Z position.

    If your camera will move around, think about making the camera the parent of the background sprite.

    As for a series of 256x256 tiles, you'll want to use ScreenToWorldPoint again to find the upper-left, and then just use appropriate logic in a loop to instantiate tiles from there, from left-to-right, top-to-bottom as needed. To find out how far to "step" each time, you'll need to figure your world-to-pixel ratio. If you're using an orthographic camera, that's pretty easy since the ortho size is 1/2 the visible world height. So an ortho size of 10 means your frustum is 20 world units high and the width would be found by looking at your viewport aspect ratio. If it is not orthographic, however, you'll probably just need to project two points from the screen into space and compare them to see the distance in world units per pixel at a given Z depth.
     
  29. x4000

    x4000

    Joined:
    Mar 17, 2010
    Posts:
    353
    Okay -- thanks, that pretty much answers my question. There is more math involved in the conversions than I would prefer, which hopefully will not be a performance drain with a lot of stuff onscreen at once. I guess I'll find out!
     
  30. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    This should only need to be calculated when the viewport size changes. So it shouldn't be happening at runtime. Though on the average desktop, even if they ran every frame, these calculations would be miniscule.
     
  31. x4000

    x4000

    Joined:
    Mar 17, 2010
    Posts:
    353
    Okay, thanks for the prior help -- I've got it working based on Sprite, and it works really well.

    One question I am now running into, though, is how to rotate sprites along the Z axis when I am looking at them on the XY axis. This should be simple, and in fact it largely looks to be, but I am getting unexpected results: the sprites seem float in a big circle around their intended position as they rotate, rather than rotating around their own center.

    In directX in the past I was used to working in radians, but apparently the unit here is degrees (though, I have one source saying that RotateAroundLocal is radians!?). So I'm passing in a value between 0 and 360, and expecting to see the sprite rotate with something like this:

    Code (csharp):
    1. obj.Transform.eulerAngles = new Vector3( 0, 0, 0 );
    2. if ( Rotation > 0 )
    3. {
    4.    obj.Transform.Rotate( new Vector3( obj.Transform.position.x + obj.SingleSprite.fixedHalfSize,
    5.                 obj.Transform.position.y + obj.SingleSprite.fixedHalfSize, obj.Transform.position.z ), Rotation );
    6. }
    And oddly, the above does the same exact thing if I take out the fixedHalfSize additions (which are the value of half the sprite's width/height).

    And, even more oddly, it does the same if I pass 0, 0, 1 instead of x, y, z.

    In the case of RotateAroundLocal, with everything I've tried it has just gone nuts and blinks all over the place (so, perhaps that is a case of it being radians instead of degrees), but it does so in a big circle nonetheless. The circle is exactly centered around the sprite's original position, never touching the intended position at all.

    My sprites are all parented to the main camera, which is located at 0,0,0. Then the sprites have varying x/y positions of course, set by Translate.position, and they have z values varying from about 20,000 on down (I am using the Z depth for draw order with a pool of sprites that are used "on demand" per frame -- it works really well).

    I am rather puzzled by this, to say the least. If this were some sort of "you must apply the rotation before the position transform" problem, then I'd expect to see the rotation become less and less skewed as I moved to the bottom left. But it's consistent no matter where I position it on the screen.

    Quite odd indeed. Any help is much appreciated.
     
  32. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    This looks to be because Rotate() accepts as its first argument the euler angles of the desired rotation. The second and optional argument is a point in space that the rotation is relative to. It would seem the arguments in your sample code are in reverse order. But perhaps I am misunderstanding.
     
  33. x4000

    x4000

    Joined:
    Mar 17, 2010
    Posts:
    353
    Oh, good grief -- sorry to bother you with something like that! That did indeed fix it, thanks.
     
  34. x4000

    x4000

    Joined:
    Mar 17, 2010
    Posts:
    353
    Actually, that wasn't quite it, though it did help me find that. Just in case anyone else is looking for this, here's the solution, in the end:

    Code (csharp):
    1. obj.Transform.eulerAngles = Vector3.zero;
    2.         if ( Rotation > 0 )
    3.             obj.Transform.RotateAround( new Vector3( obj.Transform.position.x + obj.SingleSprite.fixedHalfSize,
    4.                 obj.Transform.position.y + obj.SingleSprite.fixedHalfSize, obj.Transform.position.x ),
    5.                 new Vector3( 0, 0, 1 ), Rotation );
    The first vector is the point around which to rotate, the second is the axis on which to rotate, and then the rotation is the last parameter.
     
  35. aerende

    aerende

    Joined:
    Apr 27, 2009
    Posts:
    316
    I have a character animation that is pinned to a corner of the animation. When I flip the horizontal direction the character is going, I get a big horizontal offset in the character's game object because the whole animation is flipping about the pinned corner. I tried horizontally offsetting the character's game object when I change the character's direction, but that causes the camera to bounce since the camera is following the character.

    I viewed the video for the new SM2 regarding pinning animations using TEXTURE_OFFSET. Would using TEXTURE_OFFSET solve my problem? Or is there another approach?
     
  36. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    Yes, I think using TEXTURE_OFFSET probably would. Just make sure your source texture(s) take into account the fact that the center of the GameObject will be positioned at the center of the source texture.

    Using this feature, you can achieve the same benefits of keeping the sprite from moving in undesired directions as it changes shape, while not forcing the image to be oriented in an odd manner relative to the GameObject's center.
     
  37. aerende

    aerende

    Joined:
    Apr 27, 2009
    Posts:
    316
    I created a prefab of a door containing a Packed Sprite with two animations: 1) for opening the door and 2) for closing the door. I made two GameObjects with this door prefab,: 1) Door1 and Door2. Then I play animations for the opening and closing of Door1 followed by opening and closing of Door2.

    Code (csharp):
    1.   Door1GO    = GameObject.Find("Door1");
    2.   Door1GOPF  = Door1GO.Find("PreFabDoor");
    3.   Door1GOPS  = Door1GOPF.GetComponent("PackedSprite");
    4.  
    5.   Door2GO    = GameObject.Find("Door2");
    6.   Door2GOPF  = Door2GO.Find("PreFabDoor");
    7.   Door2GOPS  = Door2GOPF.GetComponent("PackedSprite");
    8.  
    9.      Door1GOPS.PlayAnim(2);
    10.      yield WaitForSeconds(1.0);
    11.      Door1GOPS.PlayAnim(1);
    12.      yield WaitForSeconds(1.0);
    13.      Door2GOPS.PlayAnim(2);
    14.      yield WaitForSeconds(1.0);
    15.      Door2GOPS.PlayAnim(1);
    16.      yield WaitForSeconds(1.0);

    What happens is that only one door will open and close but twice. Sometimes it is the first door and sometimes it is the second door. Leaving Unity and restarting Unity can sometimes change which door is the one that opens and closes.

    Any idea what could be causing this?
     
  38. Lokken

    Lokken

    Joined:
    Apr 23, 2009
    Posts:
    436
    Code (csharp):
    1. Door1GOPF  = Door1GO.Find("PreFabDoor");
    2.  
    3. Door2GOPF  = Door2GO.Find("PreFabDoor");
    4.  
    5.  
    I am not 100% sure but I believe that these two will return the same object to Door1GOPF and Door2GOPF.

    The Find() function I believe searches all game objects and not just children which i think is your intention.

    Have you tried setting these variables up and assigning them in the editor and not at runtime?

    Reference: http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html

    So maybe try

    Code (csharp):
    1.  
    2. Door1GOPF  = GameObject.Find("/Door1GO/PreFabDoor");
    3.  
    4. Door2GOPF  = GameObject.Find("/Door2GO/PreFabDoor");
    5.  
     
  39. aerende

    aerende

    Joined:
    Apr 27, 2009
    Posts:
    316
    Lokken:

    That did the trick! Thank you very much!
     
  40. aerende

    aerende

    Joined:
    Apr 27, 2009
    Posts:
    316
    I erased the material and Sprite Atlas I was using for a Packed Sprite. But when I try to re-generate it using Build Sprite Atlas with a new empty Material, nothing happens. SM2 says in the Console that "Atlas generation complete", but the Material is still blank. It's as if SM2 thinks the Sprite Atlas has been generated, but the Material attached to the Packed Sprite's Mesh Renderer is blank.

    Does anyone have any ideas why SM2 won't generate the Sprite Atlas?
     
  41. aerende

    aerende

    Joined:
    Apr 27, 2009
    Posts:
    316
    I have a prefab that consists of:

    1) Mesh Renderer with one material = PreFabMaterial
    2) Packed Sprite with animations in the PreFabMaterial

    The PreFabMaterial is using Shader = Transparent/Vertex Colored.

    I instantiate 3 of these prefabs and each one is a separate draw call. I thought there was only one draw call per material.

    Does anyone know why there is a separate draw call per instance of the prefab?
     
  42. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    due to transparent

    transparent objects can not always be batched as they otherwise fail to correctly depth sort
     
  43. aerende

    aerende

    Joined:
    Apr 27, 2009
    Posts:
    316
    dreamora:

    I tried using other Shaders instead of Transparent and still I got a separate draw call per instance.

    Can you suggest a Shader that I can use that won't give me a separate draw call per instance?
     
  44. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    are you sure you use the same material (no changed material properties and alike as any change on the values creates a new material!)
     
  45. aerende

    aerende

    Joined:
    Apr 27, 2009
    Posts:
    316
    dreamora:

    I'm not sure what you are asking? Where the Shader is listed in the Material, I just selected a different Shader and then hit play and looked at the stats as I made instances of the prefabs inactive, to determine that there was still a draw call per instance.
     
  46. Lokken

    Lokken

    Joined:
    Apr 23, 2009
    Posts:
    436
    Are you in Unity 2.6 or Unity iPhone? 2.6 does not have the dynamic batching that iPhone does (I believe).

    What dreamora is wanting to make sure you're not doing is altering any material properties manually.

    for example doing something like

    Code (csharp):
    1.  
    2. myMaterial.color = Color.red;
    3.  
    will cause unity to duplicate the material thus resulting in more draw calls.
     
  47. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    On your atlas building issue, make sure the references are still setup to your source textures in your animations. That's the only reason I can think of that it wouldn't build the atlas (also, be sure to download the latest 1.0.1 version from the group).

    On the draw call issue, be sure to see the SM2 docs in the troubleshooting section. There's a bunch of info in there about things to check related to draw calls. Let me know if you need further help.
     
  48. aerende

    aerende

    Joined:
    Apr 27, 2009
    Posts:
    316
    Lokken:

    I'm using Unity iPhone. The Materials I am using have been created by SM2. I'm not altering any of the material properties manually.

    Brady:

    Downloading version 1.0.1 fixed the atlas build issue.

    I'm reading the latest SM2 docs about draw calls. Thanks!
     
  49. YueFeng

    YueFeng

    Joined:
    Feb 14, 2010
    Posts:
    24
    hi,I have a problem.
    I based the tutorial steps to rebuild sprite atlases, but it tells me " 0 atlases generated", what's the problem?
    Thanks!
     
  50. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    This is likely due to the setting of "Scan Project Folder". See the SM2 docs under the section "Scan Project Folder". Basically, because Unity iPhone lacks some of the prefab management features of 2.6, when building atlases with Unity iPhone, you need to either uncheck "Scan Project Folder" if you are building atlases for sprites in the current scene that are not connected to prefabs, or else keep it checked, and have all your sprites connected to prefabs. Having it checked only scans prefabs in the project folder, and updates sprites in your scenes based on the prefabs. Unchecking it means only the sprites in your scene will be used to build atlases and prefabs in the project folder will not be scanned.

    You'll find all the details in the above mentioned section of the SM2 docs.