Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to copy a recttransform?

Discussion in 'Scripting' started by rainbow_design, Sep 27, 2017.

Thread Status:
Not open for further replies.
  1. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    Hello, i have an object with a recttransform and i like to copy it to a different gameobject so it will have the exact rect variables how can i do this the best?
     
  2. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    Ehm, in editor or in game?
    In editor you need to copy the rect transform component and past only the values on another.
    In game you need to create a script that do the exact same thing. Remember to add "using UnityEngine.UI" for use the Rect Transform component
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  4. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    RoMax with a script...

    i have a gameobject i want to copy the rect stuff to it. just instantiate the object the rect comes from does not work since the new object is an instance of something with some stuff on it. I "solved" it by make a own function which just copys the basic values it worked so far...
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    RectTransform is not a part of the UnityEngine.UI namespace.
     
  6. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    Oh, your right :eek:
     
  7. andreivictor

    andreivictor

    Joined:
    Jun 26, 2018
    Posts:
    3
    Code (CSharp):
    1. // Create a new UI GameObject like this, you can just add RectTransform only if you want.
    2. obj = new GameObject(layerName, new Type[] { typeof(RectTransform), typeof(CanvasRenderer) } );
    3.  
    4. // optionally, you can set the Parent using the transform (not the RectTransform)
    5. obj.transform.SetParent(transform, false);
    6.  
    7. // Get the source and target RectTransform components
    8. RectTransform rectTransform = GetComponent<RectTransform>();
    9. RectTransform objRectTransform = obj.GetComponent<RectTransform>();
    10.  
    11. // These four properties are to be copied
    12. objRectTransform.anchorMin = rectTransform.anchorMin;
    13. objRectTransform.anchorMax = rectTransform.anchorMax;
    14. objRectTransform.anchoredPosition = rectTransform.anchoredPosition;
    15. objRectTransform.sizeDelta = rectTransform.sizeDelta;
    16.  
    17.  
     
    Last edited: Jan 24, 2019
  8. LucasGarciaMateu

    LucasGarciaMateu

    Joined:
    Mar 1, 2021
    Posts:
    1
    Hi, I've been dealing with this issue today and just wanted to point out that within the properties to be copied you should also account for the pivot:

    objRectTransform.pivot= rectTransform.pivot;
     
  9. dijkstrab

    dijkstrab

    Joined:
    May 3, 2023
    Posts:
    1
Thread Status:
Not open for further replies.