You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
2.7 KiB
61 lines
2.7 KiB
3 years ago
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
|
||
|
|
||
|
public class TestBounds : MonoBehaviour
|
||
|
{
|
||
|
private SpriteRenderer sprRender;
|
||
|
public GameObject Button;
|
||
|
public Camera UICamera;
|
||
|
// Start is called before the first frame update
|
||
|
void Start()
|
||
|
{
|
||
|
sprRender = this.GetComponentInChildren<SpriteRenderer>();
|
||
|
//Debug.LogError(sprRender.sprite.border);
|
||
|
//Debug.LogError(sprRender.sprite.bounds);
|
||
|
//Debug.LogError(sprRender.bounds);
|
||
|
|
||
|
Bounds bounds = sprRender.sprite.bounds;
|
||
|
Vector4 border = sprRender.sprite.border;
|
||
|
Vector2 minPos = sprRender.transform.TransformPoint(bounds.center - new Vector3(bounds.extents.x - border.x / sprRender.sprite.pixelsPerUnit, bounds.extents.y - border.y / sprRender.sprite.pixelsPerUnit));
|
||
|
Vector2 maxPos = sprRender.transform.TransformPoint(bounds.center + new Vector3(bounds.extents.x - border.z / sprRender.sprite.pixelsPerUnit, bounds.extents.y - border.w / sprRender.sprite.pixelsPerUnit));
|
||
|
Vector2 midPos = (minPos + maxPos) / 2;
|
||
|
Debug.LogError("worldPos:" + minPos + "|" + maxPos);
|
||
|
|
||
|
Vector2 midLeft = new Vector2(minPos.x, midPos.y);
|
||
|
//minPos = RectTransformUtility.WorldToScreenPoint(UICamera, minPos);
|
||
|
Debug.LogError("screenPos:" + minPos + "|" + maxPos);
|
||
|
// minPos = Canvas.transform.TransformPoint(minPos);
|
||
|
Debug.LogError("uiPos:" + minPos + "|" + maxPos);
|
||
|
var trans = Button.GetComponent<RectTransform>();
|
||
|
//trans.anchoredPosition = trans.InverseTransformPoint(minPos);
|
||
|
trans.position = midLeft;
|
||
|
}
|
||
|
|
||
|
// Update is called once per frame
|
||
|
void Update()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
void OnDrawGizmos()
|
||
|
{
|
||
|
sprRender = this.GetComponent<SpriteRenderer>();
|
||
|
if (sprRender != null)
|
||
|
{
|
||
|
Bounds bounds = sprRender.sprite.bounds;
|
||
|
Vector4 border = sprRender.sprite.border;
|
||
|
Vector2 minPos = sprRender.transform.TransformPoint(bounds.center - new Vector3(bounds.extents.x - border.x/sprRender.sprite.pixelsPerUnit, bounds.extents.y - border.y / sprRender.sprite.pixelsPerUnit));
|
||
|
Vector2 maxPos = sprRender.transform.TransformPoint(bounds.center + new Vector3(bounds.extents.x - border.z / sprRender.sprite.pixelsPerUnit, bounds.extents.y - border.w / sprRender.sprite.pixelsPerUnit));
|
||
|
Debug.DrawLine(minPos, maxPos);
|
||
|
//Vector3 lt = bounds.min + new Vector3(0, bounds.size.y, 0);
|
||
|
//Vector3 rb = bounds.max + new Vector3(0, -bounds.size.y, 0);
|
||
|
//Debug.DrawLine(bounds.min, lt);
|
||
|
//Debug.DrawLine(lt, bounds.max);
|
||
|
//Debug.DrawLine(bounds.max, rb);
|
||
|
//Debug.DrawLine(bounds.min, rb);
|
||
|
}
|
||
|
}
|
||
|
}
|