Search Unity

Setting an image to be drawn between two world positions through scripts

Discussion in 'General Graphics' started by CoffeeConundrum, Mar 3, 2016.

  1. CoffeeConundrum

    CoffeeConundrum

    Joined:
    Dec 30, 2013
    Posts:
    46
    Wondering if anyone can help me out here, I'm wanting to draw an image between two positions on the screen. I know I can change the scale of the image in the X axis to scale it between the two but I'm requiring that it's accurate as well!

    Many thanks!
     
  2. raphael-ernaelsten-heaj

    raphael-ernaelsten-heaj

    Joined:
    Mar 9, 2016
    Posts:
    78
    I made something similar a while ago...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [ExecuteInEditMode]
    5. public class DisplayTextureBetweenTwoScreenPositions : MonoBehaviour
    6. {
    7.     public Texture2D texture;
    8.     [Range(0.0f, 1.0f)]
    9.     public float xSize = 0.5f;
    10.     [Range(0.0f, 1.0f)]
    11.     public float ySize = 0.5f;
    12.     public bool maintainAspectRatio = false;
    13.     public bool pixelPerfect = false;
    14.     public Vector2 normalizedScreenPosA = Vector2.zero;
    15.     public Vector2 normalizedScreenPosB = Vector2.one;
    16.     public bool rotate = true;
    17.  
    18.     private Vector3 screenPos;
    19.     private Rect rect;
    20.     private float angle;
    21.  
    22.     private Vector2 GetSize()
    23.     {
    24.         int sizeX = Mathf.RoundToInt(xSize * (float)Screen.width);
    25.         int sizeY = maintainAspectRatio ? Mathf.RoundToInt((float)texture.height / (float)texture.width * (float)sizeX) : Mathf.RoundToInt(ySize * (float)Screen.height);
    26.         if (pixelPerfect)
    27.         {
    28.             sizeX = texture.width;
    29.             sizeY = texture.height;
    30.         }
    31.  
    32.         return new Vector2(sizeX, sizeY);
    33.     }
    34.  
    35.     void Update()
    36.     {
    37. #if UNITY_EDITOR
    38.         if (texture != null)
    39.         {
    40. #endif
    41.             Vector2 screenPosA = new Vector2(normalizedScreenPosA.x * Screen.width, normalizedScreenPosA.y * Screen.height);
    42.             Vector2 screenPosB = new Vector2(normalizedScreenPosB.x * Screen.width, normalizedScreenPosB.y * Screen.height);
    43.             Vector2 size = GetSize();
    44.  
    45.             if (rotate)
    46.             {
    47.                 Vector2 tmp = screenPosA - screenPosB;
    48.                 angle = Mathf.Atan2(tmp.y, tmp.x) * Mathf.Rad2Deg;
    49.             }
    50.  
    51.             Vector3 tmpScreenPos = Vector3.zero;
    52.             tmpScreenPos.x = (screenPosA.x + screenPosB.x) * 0.5f;
    53.             tmpScreenPos.y = (screenPosA.y + screenPosB.y) * 0.5f;
    54.             screenPos = tmpScreenPos;
    55.  
    56.             Rect tmpRect = rect;
    57.             tmpRect.center = new Vector2(screenPos.x, Screen.height - screenPos.y);
    58.             tmpRect.width = size.x;
    59.             tmpRect.height = size.y;
    60.             rect = tmpRect;
    61.  
    62. #if UNITY_EDITOR
    63.         }
    64. #endif
    65.     }
    66.  
    67.     void OnGUI()
    68.     {
    69. #if UNITY_EDITOR
    70.         if (texture != null)
    71.         {
    72. #endif
    73.             if (rotate)
    74.             {
    75.                 Matrix4x4 tmp = GUI.matrix;
    76.                 GUIUtility.RotateAroundPivot(angle, rect.center);
    77.                 GUI.DrawTexture(rect, texture);
    78.                 GUI.matrix = tmp;
    79.             }
    80.             else
    81.             {
    82.                 GUI.DrawTexture(rect, texture);
    83.             }
    84. #if UNITY_EDITOR
    85.         }
    86. #endif
    87.     }
    88. }
    89.