Hello Guest

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Messages - theremin

Pages: [1] 2
Support / Re: Dynamically set tk2dCamera resolutionOverride in code
« on: November 25, 2012, 11:02:40 pm »
Yeah, I'm clamping to power of 2 scales ? it's the only way to do the scaling since I want uniform pixel perfect sprites. But I have very specific scale thresholds in mind so that my game looks the way I want it to on phones, tablets, and monitors. I imagine not everyone would want to use the same scaling ranges that I'm using, so it might not be as easy as adding a simple power of 2 scaling option. If the power of 2 scaling option was automatic, how would you determine the scale thresholds?

Ultimately, I think the Resolution Override system might be a bit more powerful if it used resolution ranges instead of specific resolutions. That way we could do stuff like I'm doing in my code, e.g., less than 640px height, then scale=1; less than 1280px height, then scale=2, etc. But that might just be best left to individuals to add whatever code they want, so they can scale their games however they want. For me, as long as code snippets like the one I'm using above continue to work, I'm totally happy :)

Thanks for your help!

Support / Re: Dynamically set tk2dCamera resolutionOverride in code
« on: November 24, 2012, 08:16:22 pm »
Update: I just discovered that if I enable the Resolution Overrides checkbox in the Inspector, then I can use the code above and my dynamic scaling seems to work as intended. I think that solves it, but let me know if there's anything else I should know. Thanks!

Support / Dynamically set tk2dCamera resolutionOverride in code
« on: November 24, 2012, 06:16:28 pm »
I haven't updated 2D Toolkit in a while, and I recently decided to update to 1.80 patch 3. I noticed that camera scaling (resolution override) seems to work quite differently now.

I used to handle camera scaling dynamically in code, and that worked really well for me, but my old script doesn't work anymore. This is the script that I used to put on my tk2dCamera object, in the Start() method:

Code: [Select]
tk2dCamera.inst.resolutionOverride = new tk2dCameraResolutionOverride[1];
tk2dCamera.inst.resolutionOverride[0] = new tk2dCameraResolutionOverride();
tk2dCamera.inst.resolutionOverride[0].width = Screen.width;
tk2dCamera.inst.resolutionOverride[0].height = Screen.height;

if (Screen.height >= 1280)
    tk2dCamera.inst.resolutionOverride[0].scale = 4f;
else if (Screen.height >= 640)
    tk2dCamera.inst.resolutionOverride[0].scale = 2f;
    tk2dCamera.inst.resolutionOverride[0].scale = 1f;


I'm not interested in using 2D Toolkit's new automatic scaling ? it doesn't allow me to fine tune my scaling enough, and I want pixel perfect scaling that's always a factor of 2 ? but, again, my old script isn't working anymore.

How can I manually set the resolutionOverride scale value with this new version of 2D Toolkit?

Support / Unity 4.0 and 2D Toolkit
« on: August 22, 2012, 05:08:52 pm »
I just got an e-mail from Unity Technologies today saying that the Unity 4.0 pre-order beta is officially available for download today. I'm not going to download it yet; I'm in no particular rush. But it did remind me that I've been curious about Unity 4.0 and 2D Toolkit. Will 2D Toolkit likely support Unity 4.0 at some point? And will Unity 3.x support continue?

Releases / Re: 2D Toolkit 1.75 final
« on: June 25, 2012, 08:04:04 pm »
Is the animation Wrap Mode "Loop Section" functionality broken in this release? I just started using Loop Section for the first time this morning -- so this could be my fault -- but I could have sworn this working right before I upgraded from 1.7 to 1.75.

I have a 9-frame animation, with Wrap Mode set to "Loop Section" and Loop Start set to 1. The animation plays frames 0-8 correctly, but as soon as it's supposed to reset to frame 1, I start getting a barrage of errors on this line:

Code: [Select]
IndexOutOfRangeException: Array index is out of range.
tk2dAnimatedSprite.SetFrameInternal (Int32 currFrame) (at Assets/TK2DROOT/tk2d/Code/Sprites/tk2dAnimatedSprite.cs:351)
tk2dAnimatedSprite.Update () (at Assets/TK2DROOT/tk2d/Code/Sprites/tk2dAnimatedSprite.cs:406)

Looks like "currFrame" (passed into SetFrameInternal) just keeps on going -- frame 9, frame 10, frame 11, etc.

Am I doing something wrong, or is this broken in 2D Toolkit?

Just tried this out and it works brilliantly! Thanks unikron.

Support / Re: Help me correct my thinking on resolutions.
« on: June 13, 2012, 05:28:29 am »
If you're not very comfortable with resolution stuff yet, you may want to stay away from that little script I provided for the time being. It may cause more harm than good until you have a good grasp on resolutions.

The best way to figure out what resolution you're running at is to throw the line Debug.Log(Screen.width+","+Screen.height); somewhere into your code, and watch the output in the console. It's important to play around with different resolutions to get a sense of how your game will look on each. Go to Edit > Project Settings > Player to set the default screen width and screen height, and then, in the Game window, make sure that "Standalone (Width x Height)" is selected. Also make sure "Maximize on Play" is active.

As far as I can tell, as long as your monitor's resolution is larger than the default screen width and screen height you chose, you'll see your game at your preferred resolution. I haven't been able to figure out how to make Unity play a game in-editor at my monitor's full native resolution yet though. (The game doesn't fit with Unity's menus, toolbars, and status bars, so Unity seems to shrink it down to a resolution that fits.)

Support / Re: Help me correct my thinking on resolutions.
« on: June 12, 2012, 02:58:32 am »
Preface: I'm still new to Unity and 2D Toolkit, so take my advice with a grain of salt. Definitely pay more attention to unikron; he's the smart guy here :)

But I'll throw in my $0.02:

If it's not too late to switch to a tk2dCamera, I highly recommend that. It will make your life so much easier. tk2dCamera makes sprite positioning simple. Want to move a sprite 1 pixel to the right? Just add 1 the transform.position.x value. You'll never get any of those "off" pixels.

Also, depending on how flexible you want to be with your resolution support (sounds like you're OK with forcing 1280x720, and if you want to stick with that, that's fine --- just ignore this): With blocky retro style pixel graphics, using the tk2dCamera's Resolution Override to scale your graphics works really well for keeping your looking fairly consistent at a variety of native resolutions. You might have a scale of 1:1 for really old displays (e.g., 640x480), and then a scale of 2:1 for 1280x720 (and 1280x800), and a scale of 3:1 for 1920x1080 (and 1290x1200)... and perhaps even beyond e.g., for the new MacBook Pro retina display announced today. That will guarantee nice sharp retro pixel graphics at every resolution. But that may not work for you, especially if you're using 1280x720 for 1:1 pixels. Just wanted to throw the idea out there.

I'm using a similar concept for a retro style pixel game I'm making, with support for a wide variety of resolutions:
- 480x320 on the iPhone 3GS
- 960x640 on the iPhone 4/4S
- 1024x768 on the iPad
- 2048x1536 on the iPad 3
- 1280x720, 1280x800, 1920x1080, and 1920x1200 (and beyond) on PC and Mac.

And I throw this script on my tk2dCamera, and 2D Toolkit makes a whole lot of magic happen:

Code: [Select]
void Awake()
tk2dCamera.inst.resolutionOverride = new tk2dCameraResolutionOverride[1];
tk2dCamera.inst.resolutionOverride[0] = new tk2dCameraResolutionOverride();
tk2dCamera.inst.resolutionOverride[0].width = Screen.width;
tk2dCamera.inst.resolutionOverride[0].height = Screen.height;

if (Screen.height >= 1280)
tk2dCamera.inst.resolutionOverride[0].scale = 4f;
else if (Screen.height >= 960)
tk2dCamera.inst.resolutionOverride[0].scale = 3f;
else if (Screen.height >= 640)
tk2dCamera.inst.resolutionOverride[0].scale = 2f;
tk2dCamera.inst.resolutionOverride[0].scale = 1f;

Note: The scale values above are just ballpark, obviously you'd have to use whatever works for your particular circumstances.

RE: #1:

Brilliant! That's the solution I've been searching for... almost. Unfortunately, the material is still flickering -- it's bouncing back and forth between border and no border very quickly whenever an animation is playing -- but at least I have one sprite isolated now with its own material/shader.

I suspect this would be solved by the "ignore material" option that you're thinking about adding. Any chance that option just affects a small piece of td2k code that I could just edit myself in the meantime? My texture atlases are pretty small, so I don't intend to use multi-atlases anyway. If it's just a few lines of code I could comment out, that would be awesome.

Thanks unikron. This is a great library, and your support is top notch. I'll definitely be recommending 2D Toolkit to my game programming friends.

Thanks for your response! This is a good start, but I still have some questions.

Question 1:

In the most basic terms, I don't understand how to change the material, even after each frame change. I just want to make a copy of the material at runtime, change its shader to something other than the default, and selectively use that on one specific sprite (but not all sprites that share that material).

Here's some code I've been playing with:

Code: [Select]
protected tk2dAnimatedSprite anim;
protected Material material;

void Start()
anim = (tk2dAnimatedSprite)GetComponent<tk2dAnimatedSprite>();

var material = (Material)Instantiate(anim.GetCurrentSpriteDef().material);
material.shader = Shader.Find("Custom/Sprite Border");

void Update()
anim.GetCurrentSpriteDef().material = material;

Unfortunately, this doesn't seem to work at all. First of all, it changes the material for all sprites that are using that material, instead of just one of the sprites. And secondly, it permanently changes the material, even after I stop running the program. The connection between the SpriteCollection and material/data gets broken, and I'm left with just a pink box for all of my sprites in the Unity Editor. The only way to fix it is to re-open the SpriteCollection and Commit again.

What am I doing wrong?

Question 2:

If I tried something like you're talking about in your last paragraph (the one that "sounds like a LOT of work"), that would require me to keep two sets of GameObjects in my scene, right? (Two copies of each sprite; one without a border, and one with.) How would I keep the sprite pairs in sync at runtime? Would I have to constantly update both sprites whenever anything changes in one? Or is there a way to copy a sprite's state (especially the current animation frame and how far the sprite is into it) at a specific moment?

Let me provide a little more detail, and ask a slightly different (but related) question:

How can I change the material of just one sprite dynamically at runtime (without changing the material for every other sprite that's using the same atlas texture)?

Unikron mentioned that you can use a different shader (and thus force a new draw call) on a specific sprite in this question:

What I'm trying to figure out is: How?

I'm OK with having a new draw call, but I have no idea how to do it. How do I apply a new material/texture/shader without having to create two copies of every Sprite/SpriteCollection/SpriteAnimation in the editor?

To give the full backstory, I'm trying to create a border around my sprites under certain conditions (whichever sprite was clicked most recently). I would like to avoid manually creating every sprite twice in the editor if possible, and I especially don't want to create all of the animations twice.

I have a few different methods that work, I'm just not sure how to integrate them into my 2D Toolkit workflow. I have a custom shader that draws a 1px border around a sprite, and I also have code that copies a Texture2D and modifies its colors with GetPixels/SetPixels. I suppose a third alternative would be to somehow duplicate the 2D Toolkit SpriteCollectionData in the editor and replace the texture with a bordered version.

I just can't figure out how to switch between the two textures/materials dynamically in code. The only way I know that would definitely work is to create two completely different sets of SpriteCollections, SpriteAnimations, and sprite GameObjects, and then manually keep them in sync during gameplay and selectively choose which one is visible --- which is a lot of extra setup work, and probably a real performance hit too!

How can I avoid doing that?

I would like to be able to make a copy of my animated sprite's atlas texture and dynamically change the pixels in-memory at runtime, but I'm having trouble figuring out which of the sprite instance's texture(s) I have to change in code.

For example, take the following code:

Code: [Select]
var texture = (Texture2D)Instantiate(renderer.material.mainTexture);
renderer.material.mainTexture = texture;

var cols = texture.GetPixels();
for (var x = 0; x < cols.Length; x++)
if (cols[x].a > 0f)
cols[x] = Color.red;


This code takes the texture and changes every visible pixel (alpha>0) to red. I put this code into a new MonoBehaviour script and attached it to my animated sprite GameObject.

If I put the code in the Start() method, it seems to do absolutely nothing (the sprite animates normally with its original texture, as defined in the editor). But if I put that code in the Update() method, I can see that the code partially works -- the sprite frames turn red, and flash quickly between red and their original colors. It seems as the code is "battling" against the 2D Toolkit code, which is constantly swapping back to the original texture.

How can I completely swap out the sprite atlas texture for a specific animated sprite without fighting against the 2D Toolkit code?

I've tried changing the texture as seen in the code above, and I've also tried changing the tk2dAnimatedSprite anim.collection.textures. Is there any way to tell a tk2dAnimatedSprite to use a completely different material/texture at runtime?

Cool, that sounds good. Thanks for the response! :)

I was asking an unrelated question on the Unity Answers site, and I posted a snippet of the 2D Toolkit "BlendVertexColor" shader, and one of the commenters said the following:

You should tell whoever made 2D Toolkit that fixed function shaders aren't appropriate for mobile devices anymore! Fixed function emulation is a poor choice for mobile devices; it eats performance and battery.


I'm a shader novice, so I'm not sure exactly what he means by this. Can anyone explain what this means? What would be a better alternative for a shader?

Support / Get clip name in CompleteDelegate?
« on: June 02, 2012, 07:57:04 pm »
Is it possible to get the clip name (for the clip that's ending) in animationCompleteDelegate? Or, to ask it a different way: Is there an easy way to get a clip name from a clipId?

The method signature for the complete delegate is:

Code: [Select]
void CompleteDelegate(tk2dAnimatedSprite sprite, int clipId)
It would be cool if it was more powerful, like the event delegate:

Code: [Select]
void EventDelegate(tk2dAnimatedSprite sprite, tk2dSpriteAnimationClip clip, tk2dSpriteAnimationFrame frame, int frameNum)
i.e., I would love to have the tk2dSpriteAnimationClip clip instead of int clipId.

Pages: [1] 2