|
- using System;
- using UnityEngine;
-
- namespace CIG3.ExtensionMethods
- {
- public static class Extensions
- {
- public static void AttachChildTransform(this GameObject parent, GameObject child, SpriteAlignment alignment = SpriteAlignment.Custom)
- {
- child.transform.parent = parent.transform;
- Vector3 localPosition = new Vector3(0f, 0f, -0.0001f);
- SpriteRenderer component = parent.GetComponent<SpriteRenderer>();
- if (component != null)
- {
- if (alignment == SpriteAlignment.BottomRight || alignment == SpriteAlignment.RightCenter || alignment == SpriteAlignment.TopRight)
- {
- localPosition.x = 0.5f * (float)component.sprite.texture.width;
- }
- if (alignment == SpriteAlignment.BottomLeft || alignment == SpriteAlignment.LeftCenter || alignment == SpriteAlignment.TopLeft)
- {
- localPosition.x = -0.5f * (float)component.sprite.texture.width;
- }
- if (alignment == SpriteAlignment.TopRight || alignment == SpriteAlignment.TopCenter || alignment == SpriteAlignment.TopLeft)
- {
- localPosition.y = (float)component.sprite.texture.height;
- }
- if (alignment == SpriteAlignment.LeftCenter || alignment == SpriteAlignment.Center || alignment == SpriteAlignment.RightCenter)
- {
- localPosition.y = 0.5f * (float)component.sprite.texture.height;
- }
- }
- child.transform.localPosition = localPosition;
- }
- }
- }
|