2D Toolkit Forum
2D Toolkit => Support => Topic started by: codejoy on April 16, 2015, 06:23:10 pm
-
I am not sure but it seems like this is something it could do with how the fonts can be text meshes. Wouldn't know where to start though. I would love a speech bubble that looks like this (attached) it was created at this link here:
(its sort of an 8bit retro lo res speech bubble with a font to ultimately match)
The size of the bubble would be dynamic to match the text in it...
Here is how i created the bubble...
http://wigflip.com/ds/
I guess worst case scenario I would create one for all my text and just put it up as a sprite. *shrug*
-
You can create the speech bubble using a sliced sprite, which you'd be able to resize correctly to match the bounds of the text. You'll need a script to get the EstimatedMeshBounds of the text and feed that to your sliced sprite.
-
Excellent I will research sliced sprite and EstimatedMeshBounds thank you!
-
okay does the source image matter? I think mine is not correct as when it scales it goes very muddy. The source image is attached here (I am thinking its the drop shadow causing the sliced sprite to not handle correctly)
i was scaling it both by scale and pixels to units.
-
Also to save time here is the code so far. I have the sliced sprite, with the SpeechBubble script on it and a text mesh under it. (will get the exact font I want later) but for now ...it sort of works aside from the ugly borders mentioned earlier... and the text not being in the center (more it is centered on the left border of the speech bubble). Image attached as well.
// Use this for initialization
tk2dTextMesh msh;
TextMesh y;
tk2dSlicedSprite spr;
void Start () {
msh = transform.Find ("TextMesh").GetComponent<tk2dTextMesh>();
if(!msh)
Debug.LogError ("Coulnd't get it!");
Bounds b = msh.GetEstimatedMeshBoundsForString(msh.text);
spr= GetComponent<tk2dSlicedSprite>();
spr.ReshapeBounds(b.min, b.max);
}
// Update is called once per frame
void Update () {
}
}
-
Just was curious if i was on the right path for this?
-
Yes it looks like you are, just the pivots are in the wrong places.
-
realized the source image and the resulting image was not attached, so I couldn't show how the muddied texture looked. I wanted to have it it expand like a block oval not a true oval. So found the pivot and set that on the sprite to top left and then set the sliced sprite anchor point to lower center. Attached is that is close. So really just the border and how its too oval not a nice rectangle with round corners. The code above does try to calculate the right size but it grows the sliced sprite funny.
-
Image attached.
-
Is it pixelated because the scale is inorrect? You should be able to track this down by seeing what changes. If you can tell me what changes I should be able to work out why.
-
Really dumb question: The scale in which context? And what changes how? Like when the bubble gets bigger to hold the text, see how that changes?
-
The scale & dimensions on the sliced sprite.
-
Ahh okay, I think I follow ya. If I run the game then my code thats on the sliced sprite:
public class SpeechBubble : MonoBehaviour {
// Use this for initialization
tk2dTextMesh msh;
TextMesh y;
tk2dSlicedSprite spr;
void Start () {
msh = transform.Find ("TextMesh").GetComponent<tk2dTextMesh>();
if(!msh)
Debug.LogError ("Coulnd't get it!");
Bounds b = msh.GetEstimatedMeshBoundsForString(msh.text);
spr= GetComponent<tk2dSlicedSprite>();
spr.ReshapeBounds(b.min, b.max);
}
}
Changes the dimensions from 32x32 (Which is what it is if you were to just open the file in paint for example) to 356x92 with a scale of 1. (looks like it scales with a bilinear not a point filter mode). Though the sprites filter mode if i just click the PNG from unity (so in the inspector) is set to point.
So thats what makes it look whacked, sounds like it is my source image but if that is the case I am not sure how to have a source image that "Grows" correctly?
-
The point is it shouldn't be scaling it up like that - it should just expand it when resized. Try resizing the sliced sprite in the editor to 356x92 and see what it looks like.
-
okay did that looked the same in the editor. screenshot is attached.
-
It looks like your sprite isn't the same size as the text meshes - If you create the sprite of that bubble it needs to be the correct size, looks like it might be from a different collection / have different pixels per meter settings etc. You need to get the correct effect in the viewport first before your code will work.
-
ahhhh okay. I did some tweaking turns out I double checked my font AND the sprite collection SETTINGS (I didn't realize I could set filter there too). So it is crisper but still encompassing the text it grows on the borders. I have a posted here a image with on top what its doing (with point filtering!) and on bottom something I did in gimp for what I am aiming for.
-
Its the settings on the sliced sprite you need to modify. I think the borders are too small - you're scaling the borders rather than leaving them unscaled.
-
This seemed to work put the borders at 10. Center middle anchored everything and wham takes any size text and grows around it and dose it well.
I attach the sliced sprite speech bubble as a child of my main player and it all works great, except when i walk the opposite direction. To get the player sprite to face the other way i set his scale to -1 on the x. and it works, but the sliced sprite speech bubble now turns too and looks like text was in a mirror :( not sure a way around this.
-
Solution is not to attach the speech bubble, but in a script position it relative to the player.
-
yep! :) thanks for all your help. Going to have a script that tracks player just like the camera basically.
-
I had one more question as I used this more. The bounds keeps resizing my sliced sprite larger and larger. Here is my use case:
I have different lines of text goto the speech bubble. It displays the line of text, then the next is sent to the speech bubble. I *WAS* in the Start method sizing the bubble, but since its now part of my speaking code, it sizes itself when a new line of text comes in so I have a function called SizeBubble on the sliced sprite:
public void SizeBubble()
{
if(!msh)
msh = transform.Find ("TextMesh").GetComponent<tk2dTextMesh>();
Debug.Log ("Mesh: "+ msh.text + " ****" );
Bounds b = msh.GetEstimatedMeshBoundsForString(msh.text);
spr= GetComponent<tk2dSlicedSprite>();
spr.ReshapeBounds(b.min, b.max);
}
The start code looks like this:
void Start () {
msh = transform.Find ("TextMesh").GetComponent<tk2dTextMesh>();
if(!msh)
Debug.LogError ("Coulnd't get it!");
spr= GetComponent<tk2dSlicedSprite>();
Bounds old = spr.GetBounds();
t=target.transform;
}
The thing I am seeing is that everytime i throw a new line of text at the bubble, even if the line is shorter than the last the sliced sprite grows and never shrinks. Above you notice I try to save the bounds in a variable called old which i called reshapbounds on in the SizeBubble on top (took it out as it did nothing) but I think I am not understanding how ReshapeBounds works as to why my sliced sprite keeps growing and never shrinks again to match a new line of text once my talky system sends it to the speechbubble.
So any pointers as to why the sliced sprite grows and never shrinks?
-
I don't get it either. It should work. If you can't get it to work post a simple repro in private support and I'll take a look at it.