2D Toolkit Forum
2D Toolkit => Support => Topic started by: xraven13 on January 01, 2016, 11:13:23 pm
-
Hi!
I have one problem.
I use pivot to change the center of rotation/scale, but Unity works in way that it affects also the position of object - which is a problem.
I want that x,y coordinates of object are always in the top left. To make this work I need to use the value of pivot and offset the position.
But it seems 2DToolkit doesn't have this variable.
Since this is critical and I need to finish this project asap, could you please explain me how I can tweak 2DToolkit to make this possible?
Thank you!
EDIT: in return of the favor I will also share the code here for the case if someone else will need it. Thanks again. :)
-
This isn't possible in Unity, but you can get similar behaviour by creating an empty game object as a parent, then positioning that where you want to, and position the sprite under it as a child. You rotate the sprite to get the desired rotation, but move the base object based on the origin.
-
I actually have this code already working with Unity Sprites and Sprite Renderer, but I also want to able to use this when using 2DToolkit sprites.
This is the code:
width = GetComponent<SpriteRenderer> ().bounds.size.x;
height = GetComponent<SpriteRenderer> ().bounds.size.y;
origin.x = width - renderer.sprite.pivot.x;
origin.y = height - renderer.sprite.pivot.y;
public float x {
get{ return (transform.position.x-origin.x); }
set { transform.position = new Vector3 (value+origin.x, transform.position.y, transform.position.z); }
}
public float y {
get { return (transform.position.y)-origin.y; }
set { transform.position = new Vector3 (transform.position.x, value+origin.y, transform.position.z); }
}
-
Not sure you need to approach it that way...
If you do
tk2dBaseSprite baseSprite = GetComponent<tk2dBaseSprite>();
Vector3 topLeft = new Vector3(baseSprite.GetUntrimmedBounds().min.x, baseSprite.GetUntrimmedBounds().max.y, 0);
transform.position = new Vector3(x, y, 0) - topLeft;
That will position it relative to the top left. You can easily modify that to do any corner you like. Since the pivot is 0, 0, 0 all you need is the bounds to work out what the corners will be relative to it.
-
Thank you, this works! 8) So basically this is the same as the pivot for Sprite, right. At least, values are the same.
When setting y value I am subtracting from screenHeight because I am using top left coordinate system. (maybe it will be useful to someone)
If you use normal coordinate system, just remove "screenHeight -" part.
public float x {
get{
Vector3 topLeft = new Vector3(baseSprite.GetUntrimmedBounds().min.x, baseSprite.GetUntrimmedBounds().max.y,0);
return (new Vector3(transform.position.x, transform.position.y, transform.position.z) + topLeft).x;
}
set {
Vector3 topLeft = new Vector3(baseSprite.GetUntrimmedBounds().min.x, baseSprite.GetUntrimmedBounds().max.y,0);
transform.position = new Vector3(value, y, transform.position.z) - topLeft;
}
}
public float y {
get {
Vector3 topLeft = new Vector3(baseSprite.GetUntrimmedBounds().min.x, baseSprite.GetUntrimmedBounds().max.y,0);
return screenHeight - (new Vector3(transform.position.x, transform.position.y, transform.position.z) + topLeft).y;
}
set {
Vector3 topLeft = new Vector3(baseSprite.GetUntrimmedBounds().min.x, baseSprite.GetUntrimmedBounds().max.y,0);
transform.position = new Vector3(x, screenHeight - value, transform.position.z) - topLeft;
//transform.position = new Vector3 (transform.position.x, screenHeight-(value-offsetY+origin.y), transform.position.z);
}
}