Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

spawn particle at middle of recttransform via script

Discussion in 'UGUI & TextMesh Pro' started by mogwhy, Feb 16, 2016.

  1. mogwhy

    mogwhy

    Joined:
    Nov 20, 2014
    Posts:
    36
    Hi,

    I have a lot of rec-transforms and i would like to spawn a particle via script at the middle of a specific rect-transform.
    The pivots of the transforms all differ so I would need to calculate the middle of the rect based on the pivots.

    recttransform.rect.center only give me the local.
    But I would need the center position of the recttransformin in screenspace.

    I'm using "Screenspace - Camera".

    Anybody has an idea on how to do that?
     
    Last edited: Feb 16, 2016
  2. mogwhy

    mogwhy

    Joined:
    Nov 20, 2014
    Posts:
    36
    SOLUTION found, if anybody needs it.

    I'm extending the RectTransform in this code.

    Code (CSharp):
    1.     public static Vector3 GetWorldRectCenter(this RectTransform trans) {
    2.         Vector3 pos = new Vector3 ();
    3.         Vector3[] corners = new Vector3[4];
    4.         trans.GetWorldCorners (corners);
    5.         // 0 - left bottom
    6.         // 1 - left top
    7.         // 2 - right top
    8.         // 3 - right bottom
    9.         pos.x = corners [1].x + ((corners [2].x - corners[1].x)/2);
    10.         pos.y = corners [1].y + ((corners [0].y - corners[1].y)/2);
    11.         return pos;
    12.     }
     
unityunity