Search Unity

Resolved Creating a custom extension that moves the objects' position to hide it and scales with Screen Size.

Discussion in 'Editor & General Support' started by reendevelops, Jun 17, 2021.

  1. reendevelops

    reendevelops

    Joined:
    May 6, 2021
    Posts:
    15
    I have made a script where instead of setting the gameObject.SetActive to false, it will just move its position and keep track of the original position.

    Now, my small issue is that I have to reference the script first before using it -
    Code (CSharp):
    1.  
    2. public HideScript hideScript;
    3.  
    4. hideScript.Hide();
    5.  
    6. // or
    7.  
    8. FindObjectOfType<HideScript>().Hide();
    - and it is pretty tedious since I have a lot of things to move.

    I would like to call it instead with just -
    Code (CSharp):
    1.  
    2. Hide();
    EDIT: [RESOLVED]
    Here's the full code for those who want it. It's a lot better than SetActive() because it doesn't disable the GameObject. However, SetActive() can still be used; e.g. for Cameras.
    WARNINGs:
    • Do not use .Hide() on anything but UIs.
    • It will cause some weird bugs if the GameObject you want to hide is disabled by SetActive().
    • It will select in the Inspector every function call. I don't know how to substitute it.
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System;
    5. using UnityEngine;
    6. using UnityEditor;
    7.  
    8. public class HideScript : MonoBehaviour
    9. {
    10.     // Yes. Just to make it attachable in Inspector. I like keeping it there.
    11. }
    12.  
    13. public class HideDetails // Contains the details of the GameObject to hide/show.
    14. {
    15.     public string objectName;
    16.     public Vector3 origPosition;
    17.     public GameObject marker;
    18.     public bool registered;
    19.  
    20.     public HideDetails(string objectName, Vector3 origPosition, GameObject marker, bool registered)
    21.     {
    22.         this.objectName = objectName;
    23.         this.origPosition = origPosition;
    24.         this.marker = marker;
    25.         this.registered = registered;
    26.     }
    27.  
    28.     public HideDetails(HideDetails hide)
    29.     {
    30.         this.objectName = hide.objectName;
    31.         this.origPosition = hide.origPosition;
    32.         this.marker = hide.marker;
    33.         this.registered = hide.registered;
    34.     }
    35. }
    36.  
    37. public static class HideScript2 // The actual script.
    38. {
    39.     private static Vector3 hidePos = new Vector3(-10000, -20000, 0); // Where the hide happens.
    40.     public static List<HideDetails> hideList = new List<HideDetails>(); // A list made of the details.
    41.     public static GameObject Marker;
    42.  
    43.  
    44.     public static HideDetails GetDetails(string objectName) // Function to call to get all the details.
    45.     {
    46.         foreach (var hidden in hideList)
    47.         {
    48.             if (objectName == hidden.objectName)
    49.             {
    50.                 return hidden; // Returns a HiddenDetails.
    51.             }
    52.         }
    53.         return null;
    54.     }
    55.  
    56.  
    57.     public static void Hide(this GameObject gameObject, bool hide) // Function to call when hiding/showing. THE MEAT.
    58.     {
    59.         foreach (var hidden in hideList) // Checks if the GameObject's details are already registered.
    60.         {
    61.             if (gameObject.name == hidden.objectName)
    62.             {
    63.                 AfterRegister(gameObject, hide); // If registered, proceeds.
    64.                 return;
    65.             }
    66.         }
    67.  
    68.         // If not registered, registers:
    69.         string objectName = gameObject.name;
    70.         Vector3 origPosition = gameObject.GetComponent<RectTransform>().position;
    71.  
    72.         Marker = CreateMarker(gameObject);
    73.  
    74.         hideList.Add(new HideDetails(objectName, origPosition, Marker, true)); // Adds to details.
    75.  
    76.         AfterRegister(gameObject, hide); // Proceeds.
    77.     }
    78.  
    79.     public static void AfterRegister(GameObject gameObject, bool hide)
    80.     {
    81.         RectTransform Rect = gameObject.GetComponent<RectTransform>();
    82.         Transform Trans = gameObject.GetComponent<Transform>();
    83.  
    84.         if (Rect != null)
    85.         {
    86.             if (hide)
    87.             {
    88.                 Rect.position = hidePos;
    89.             }
    90.             if (!hide)
    91.             {
    92.                 var details = GetDetails(gameObject.name);
    93.                 Rect.position = details.marker.GetComponent<RectTransform>().position;
    94.             }
    95.         }
    96.  
    97.         if (Trans != null)
    98.         {
    99.             if (hide)
    100.             {
    101.                 Trans.position = hidePos;
    102.             }
    103.             if (!hide)
    104.             {
    105.                 var details = GetDetails(gameObject.name);
    106.                 Trans.position = details.marker.GetComponent<RectTransform>().position;
    107.             }
    108.         }
    109.     }
    110.     // by @reendevelops
    111.     public static GameObject CreateMarker(GameObject gameObject) // Function to create the Marker.
    112.     {
    113.         var Canvas = GameObject.Find("FX Canvas");
    114.         GameObject varMarker = null;
    115.         if (varMarker == null)
    116.         {
    117.             varMarker = new GameObject();
    118.             varMarker.name = "Marker";
    119.             varMarker.transform.SetParent(Canvas.GetComponent<RectTransform>());
    120.             varMarker.transform.position = new Vector3(0, 0, 0);
    121.             varMarker.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
    122.             varMarker.AddComponent<RectTransform>();
    123.         }
    124.         varMarker.name += "_" + gameObject.name;
    125.  
    126.         varMarker.GetComponent<RectTransform>().position = gameObject.GetComponent<RectTransform>().position;
    127.         Selection.SetActiveObjectWithContext(varMarker, null);
    128.         AnchorsToCorners();
    129.  
    130.         return varMarker;
    131.     }
    132.  
    133.     static void AnchorsToCorners() // Sets the anchors to the corner of the Marker for it to scale properly.
    134.     {
    135.         foreach (Transform transform in Selection.transforms)
    136.         {
    137.             RectTransform t = transform as RectTransform;
    138.             RectTransform pt = Selection.activeTransform.parent as RectTransform;
    139.  
    140.             if (t == null || pt == null) return;
    141.  
    142.             Vector2 newAnchorsMin = new Vector2(t.anchorMin.x + t.offsetMin.x / pt.rect.width,
    143.                                                 t.anchorMin.y + t.offsetMin.y / pt.rect.height);
    144.             Vector2 newAnchorsMax = new Vector2(t.anchorMax.x + t.offsetMax.x / pt.rect.width,
    145.                                                 t.anchorMax.y + t.offsetMax.y / pt.rect.height);
    146.  
    147.             t.anchorMin = newAnchorsMin;
    148.             t.anchorMax = newAnchorsMax;
    149.             t.offsetMin = t.offsetMax = new Vector2(0, 0);
    150.         }
    151.     }
    152. }
    153.  
     
    Last edited: Jun 18, 2021
  2. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    How many things are "a lot" of things?

    Have you considered an array/list of HideScript and just looping over them?
     
  3. reendevelops

    reendevelops

    Joined:
    May 6, 2021
    Posts:
    15
    Yes, I do do that.

    But TL;DR, what I want is for
    Code (CSharp):
    1. FindObjectOfType<HideScript>().Hide(gameObject);
    2.  
    3. // to become just
    4.  
    5. Hide(gameObject);
     
  4. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    You do use an array/list of HideScript, but are using FindObjectOfType to find one instance?

    I don't understand.
     
  5. reendevelops

    reendevelops

    Joined:
    May 6, 2021
    Posts:
    15
    I don't understand as well what you mean by using an array/list, but I don't think I do.

    I also made a mistake. It's not functions like Update() and Start() I should be comparing it to, but functions like Destroy(), ToString(), etc.

    Here's my HideScript if it'll help you understand:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEditor;
    6.  
    7. public class HideDetails // Contains the details of the GameObject to hide/show.
    8. {
    9.     public string objectName;
    10.     public Vector3 origPosition;
    11.     public GameObject marker;
    12.     public bool registered;
    13.  
    14.     public HideDetails(string objectName, Vector3 origPosition, GameObject marker, bool registered)
    15.     {
    16.         this.objectName = objectName;
    17.         this.origPosition = origPosition;
    18.         this.marker = marker;
    19.         this.registered = registered;
    20.     }
    21.  
    22.     public HideDetails(HideDetails hide)
    23.     {
    24.         this.objectName = hide.objectName;
    25.         this.origPosition = hide.origPosition;
    26.         this.marker = hide.marker;
    27.         this.registered = hide.registered;
    28.     }
    29. }
    30.  
    31. public class HideScript : MonoBehaviour // The actual script.
    32. {
    33.     private Vector3 hidePos = new Vector3(-10000, -20000, 0); // Where the hide happens.
    34.     public List<HideDetails> hideList = new List<HideDetails>(); // A list made of the details.
    35.  
    36.     public HideDetails GetDetails(string objectName) // Function to call to get all the details.
    37.     {
    38.         foreach (var hidden in hideList)
    39.         {
    40.             if (objectName == hidden.objectName)
    41.             {
    42.                 return hidden; // Returns a HiddenDetails.
    43.             }
    44.         }
    45.         return null;
    46.     }
    47.  
    48.     public void Hide(GameObject gameObject, string hide) // Function to call when hiding/showing. THE MEAT.
    49.     {
    50.         foreach (var hidden in hideList) // Checks if the GameObject's details are already registered.
    51.         {
    52.             if (gameObject.name == hidden.objectName)
    53.             {
    54.                 AfterRegister(gameObject, hide); // If registered, proceeds.
    55.                 return;
    56.             }
    57.         }
    58.  
    59.         // If not registered, registers:
    60.         string objectName = gameObject.name;
    61.         Vector3 origPosition = gameObject.GetComponent<RectTransform>().position;
    62.  
    63.         GameObject marker = CreateMarker(gameObject); // A marker Instantiated inside a canvas that scales with screen size.
    64.  
    65.         hideList.Add(new HideDetails(objectName, origPosition, marker, true)); // Adds to details.
    66.  
    67.         AfterRegister(gameObject, hide); // Proceeds.
    68.     }
    69.  
    70.     public void AfterRegister(GameObject gameObject, string hide)
    71.     {
    72.         RectTransform Rect = gameObject.GetComponent<RectTransform>();
    73.         Transform Trans = gameObject.GetComponent<Transform>();
    74.  
    75.         if (Rect != null)
    76.         {
    77.             if (hide == "hide")
    78.             {
    79.                 Rect.position = hidePos;
    80.             }
    81.             if (hide == "show")
    82.             {
    83.                 var details = GetDetails(gameObject.name);
    84.                 Rect.position = details.marker.GetComponent<RectTransform>().position;
    85.             }
    86.         }
    87.  
    88.         if (Trans != null)
    89.         {
    90.             if (hide == "hide")
    91.             {
    92.                 Trans.position = hidePos;
    93.             }
    94.             if (hide == "show")
    95.             {
    96.                 var details = GetDetails(gameObject.name);
    97.                 Trans.position = details.marker.GetComponent<RectTransform>().position;
    98.             }
    99.         }
    100.     }
    101.  
    102.     public GameObject marker;
    103.  
    104.     public GameObject CreateMarker(GameObject gameObject) // Function to create the Marker.
    105.     {
    106.         var Canvas = GameObject.Find("FX Canvas");
    107.         GameObject varMarker = Instantiate(marker, Canvas.transform);
    108.         varMarker.name += "_" + gameObject.name;
    109.  
    110.         varMarker.GetComponent<RectTransform>().position = gameObject.GetComponent<RectTransform>().position;
    111.         Selection.SetActiveObjectWithContext(varMarker, null);
    112.         AnchorsToCorners();
    113.  
    114.         return varMarker;
    115.     }
    116.  
    117.     void AnchorsToCorners() // Sets the anchors to the corner of the Marker for it to scale properly.
    118.     {
    119.         foreach (Transform transform in Selection.transforms)
    120.         {
    121.             RectTransform t = transform as RectTransform;
    122.             RectTransform pt = Selection.activeTransform.parent as RectTransform;
    123.  
    124.             if (t == null || pt == null) return;
    125.  
    126.             Vector2 newAnchorsMin = new Vector2(t.anchorMin.x + t.offsetMin.x / pt.rect.width,
    127.                                                 t.anchorMin.y + t.offsetMin.y / pt.rect.height);
    128.             Vector2 newAnchorsMax = new Vector2(t.anchorMax.x + t.offsetMax.x / pt.rect.width,
    129.                                                 t.anchorMax.y + t.offsetMax.y / pt.rect.height);
    130.  
    131.             t.anchorMin = newAnchorsMin;
    132.             t.anchorMax = newAnchorsMax;
    133.             t.offsetMin = t.offsetMax = new Vector2(0, 0);
    134.         }
    135.     }
    136. }
    137.  
    So when I want to hide an object, I go

    Code (CSharp):
    1.  
    2. FindObjectOfType<HideScript>().Hide(gameObject, "hide");
    3.  
    4. // but instead I want:
    5.  
    6. Hide(gameObject, "hide");
    7.  
     
  6. reendevelops

    reendevelops

    Joined:
    May 6, 2021
    Posts:
    15
    I've tried making it a static class and turning it into an extension like this:
    Code (CSharp):
    1. gameObject.Hide(true);
    But it doesn't work due to instances inside a static class.
     
  7. reendevelops

    reendevelops

    Joined:
    May 6, 2021
    Posts:
    15
    LOOOOOOOOL I may have found the stupidest, yet amazing solution.
    Code (CSharp):
    1.  
    2. // This is how to call it:
    3. gameObject.Hide(true); // Moves the object far away. Better than SetActive(false) because the latter disables the object, whilst the former doesn't.
    4.  
    And this is how I solved it:
    Code (CSharp):
    1.  
    2. public static class HideObject // I made a seperate static class -
    3. {
    4.      // - with parameters of the Hide function as static
    5.     public static GameObject gameObject;
    6.     public static bool hide;
    7.     public static bool hideDone;
    8.  
    9.     public static void Hide(this GameObject gameObject2, bool hide2) // This is the extension .Hide()
    10.     {
    11.         // sets the static (things?) into the parameter (things?)
    12.         hide = hide2;
    13.         gameObject = gameObject2;
    14.         hideDone = true;
    15.     }
    16. }
    17.  
    18. // And this is how it is triggered lol
    19.  
    20. public class HideScript : MonoBehaviour // The actual script.
    21. {
    22.     void Update() // with an Update()
    23.     {
    24.         // this way, there is no error with static vs instances
    25.         if (HideObject.hideDone == true)
    26.         {
    27.             HideObject.hideDone = false;
    28.             Hide(HideObject.gameObject, HideObject.hide);
    29.         }
    30.     }
     
  8. reendevelops

    reendevelops

    Joined:
    May 6, 2021
    Posts:
    15
    There is a small issue with this though. Whenever .Hide() is triggered many times as once, it'll only register the last one. This is probably due to Update.
     
  9. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    It's due to the fact that the
    gameObject
    variable in
    HideObject
    is static.
    It can only be equal to the last GameObject reference it was assigned from anywhere.
     
  10. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Are you trying to hide a specicic HideScript? Then you need to reference the specific script somehow.

    I don't see the problem with typing

    Code (CSharp):
    1. hideScript.Hide();
    2.  
    It's just not really any shorter than typing

    Code (CSharp):
    1. Hide();
    2.  
    What are you actually trying to accomplish? Are you trying to hide all HideScripts? If you are hiding specific scripts, how are you deciding which ones should be hidden? The specifics here greatly influence the solution.

    For example, if you just want to hide all HideScripts, try this:

    Code (CSharp):
    1. foreach(var hideScript in FindObjectsOfType<HideScript>))
    2.     hideScript.Hide(gameObject);
    3.  
     
  11. reendevelops

    reendevelops

    Joined:
    May 6, 2021
    Posts:
    15
    No, there is only 1 HideScript. It is a function/method. It is called when I want to hide/show an object, thus not needing multiple scripts.

    My problem with this is that I would have to reference it all the time on different scripts. That would be fine, but I don't think would be a good practice, and learning this would be.

    I have found out about extensions, but it doesn't seem to work with my particular function/method. Do you guys know others like it?
     
  12. reendevelops

    reendevelops

    Joined:
    May 6, 2021
    Posts:
    15
    I'm not sure if that's true, but the problem I see is that:
    1. Whenever I call the extension on Start(), the HideObject class takes ALL of the calls first-
    2. and then the Update() happens.
    3. Thus, each call replaces the last one and only triggering once.
    I really need help lol. This would be extremely useful in my codings if were to be figured out.
     
  13. reendevelops

    reendevelops

    Joined:
    May 6, 2021
    Posts:
    15
    I think these are plausible solutions:
    • A way to call a non-static function from a non-static class with a static function from a static class.
    • A way for my HideScript to be the extension itself (Hide function). This would mean that the HideScript class would be static and its functions too. Seems pretty hard.
    • Or maybe something in the imports? The "using blah..blah...".
     
    Last edited: Jun 18, 2021
  14. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    I just don't see a problem with referencing it from different scripts. How many different scripts are going to be accessing HideScript? Several at most, perhaps?

    It just seems to me that your solutions are way more complicated than the thing you are trying to simplify. I can't possibly imagine how adding a magic extension function to all your gameObjects that triggers an arbitrary function on a single specific MonoBehaviour could in any way be considered "good practice"
     
  15. reendevelops

    reendevelops

    Joined:
    May 6, 2021
    Posts:
    15
    I would've agreed with you right there if I wasn't able to solve it.

    Before I present the solution, I'll explain the problem. It was actually pretty simple.

    The problem was:
    • I wanted HideScript to be an extension, but Instantiate (which was essential) doesn't work in static classes without deriving from MonoBehaviour (which was also essential for it to be an extension).
    • I thought a lot more of the script couldn't handle being a static, but I was wrong. It was only the Instantiate.
    The solution:
    Instead of this:
    Code (CSharp):
    1.  
    2.         var Canvas = GameObject.Find("FX Canvas");
    3.         GameObject varMarker = Instantiate(marker, Canvas.transform);
    4.         varMarker.name += "_" + gameObject.name;
    I turned it into this:
    Code (CSharp):
    1.  
    2.         var Canvas = GameObject.Find("FX Canvas");
    3.         GameObject varMarker = null;
    4.         if (varMarker == null)
    5.         {
    6.             varMarker = new GameObject();
    7.             varMarker.name = "Marker";
    8.             varMarker.transform.SetParent(Canvas.GetComponent<RectTransform>());
    9.             varMarker.transform.position = new Vector3(0, 0, 0);
    10.             varMarker.AddComponent<RectTransform>();
    11.         }
    12.         varMarker.name += "_" + gameObject.name;
    I pretty much just remade the Instantiate lol. And with that, it is fixed.

    I can now call it like this:
    Code (CSharp):
    1.  
    2. gameObject.Hide(true);
    3. // instead of referencing it everytime
    4.  
    Reasons why I believe this to be a good practice:
    • I can make my own functions without having to reference them like HideScript here. I have a lot of ideas on extensions that will significantly ease up my workflow.
    • Extensions are very convenient.
     
  16. reendevelops

    reendevelops

    Joined:
    May 6, 2021
    Posts:
    15
    Here's the full code for those who want it. It's a lot better than SetActive() because it doesn't disable the GameObject. However, SetActive() can still be used; e.g. for Cameras.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System;
    4. using UnityEngine;
    5. using UnityEditor;
    6.  
    7. public class HideScript : MonoBehaviour
    8. {
    9.     // Yes. Just to make it attachable in Inspector. I like keeping it there.
    10. }
    11.  
    12. public class HideDetails // Contains the details of the GameObject to hide/show.
    13. {
    14.     public string objectName;
    15.     public Vector3 origPosition;
    16.     public GameObject marker;
    17.     public bool registered;
    18.  
    19.     public HideDetails(string objectName, Vector3 origPosition, GameObject marker, bool registered)
    20.     {
    21.         this.objectName = objectName;
    22.         this.origPosition = origPosition;
    23.         this.marker = marker;
    24.         this.registered = registered;
    25.     }
    26.  
    27.     public HideDetails(HideDetails hide)
    28.     {
    29.         this.objectName = hide.objectName;
    30.         this.origPosition = hide.origPosition;
    31.         this.marker = hide.marker;
    32.         this.registered = hide.registered;
    33.     }
    34. }
    35.  
    36. public static class HideScript2 // The actual script.
    37. {
    38.     private static Vector3 hidePos = new Vector3(-10000, -20000, 0); // Where the hide happens.
    39.     public static List<HideDetails> hideList = new List<HideDetails>(); // A list made of the details.
    40.     public static GameObject Marker;
    41.  
    42.  
    43.     public static HideDetails GetDetails(string objectName) // Function to call to get all the details.
    44.     {
    45.         foreach (var hidden in hideList)
    46.         {
    47.             if (objectName == hidden.objectName)
    48.             {
    49.                 return hidden; // Returns a HiddenDetails.
    50.             }
    51.         }
    52.         return null;
    53.     }
    54.  
    55.  
    56.     public static void Hide(this GameObject gameObject, bool hide) // Function to call when hiding/showing. THE MEAT.
    57.     {
    58.         foreach (var hidden in hideList) // Checks if the GameObject's details are already registered.
    59.         {
    60.             if (gameObject.name == hidden.objectName)
    61.             {
    62.                 AfterRegister(gameObject, hide); // If registered, proceeds.
    63.                 return;
    64.             }
    65.         }
    66.  
    67.         // If not registered, registers:
    68.         string objectName = gameObject.name;
    69.         Vector3 origPosition = gameObject.GetComponent<RectTransform>().position;
    70.  
    71.         Marker = CreateMarker(gameObject);
    72.  
    73.         hideList.Add(new HideDetails(objectName, origPosition, Marker, true)); // Adds to details.
    74.  
    75.         AfterRegister(gameObject, hide); // Proceeds.
    76.     }
    77.  
    78.     public static void AfterRegister(GameObject gameObject, bool hide)
    79.     {
    80.         RectTransform Rect = gameObject.GetComponent<RectTransform>();
    81.         Transform Trans = gameObject.GetComponent<Transform>();
    82.  
    83.         if (Rect != null)
    84.         {
    85.             if (hide)
    86.             {
    87.                 Rect.position = hidePos;
    88.             }
    89.             if (!hide)
    90.             {
    91.                 var details = GetDetails(gameObject.name);
    92.                 Rect.position = details.marker.GetComponent<RectTransform>().position;
    93.             }
    94.         }
    95.  
    96.         if (Trans != null)
    97.         {
    98.             if (hide)
    99.             {
    100.                 Trans.position = hidePos;
    101.             }
    102.             if (!hide)
    103.             {
    104.                 var details = GetDetails(gameObject.name);
    105.                 Trans.position = details.marker.GetComponent<RectTransform>().position;
    106.             }
    107.         }
    108.     }
    109.     // by @reendevelops
    110.     public static GameObject CreateMarker(GameObject gameObject) // Function to create the Marker.
    111.     {
    112.         var Canvas = GameObject.Find("FX Canvas");
    113.         GameObject varMarker = null;
    114.         if (varMarker == null)
    115.         {
    116.             varMarker = new GameObject();
    117.             varMarker.name = "Marker";
    118.             varMarker.transform.SetParent(Canvas.GetComponent<RectTransform>());
    119.             varMarker.transform.position = new Vector3(0, 0, 0);
    120.             varMarker.AddComponent<RectTransform>();
    121.         }
    122.         varMarker.name += "_" + gameObject.name;
    123.  
    124.         varMarker.GetComponent<RectTransform>().position = gameObject.GetComponent<RectTransform>().position;
    125.         Selection.SetActiveObjectWithContext(varMarker, null);
    126.         AnchorsToCorners();
    127.  
    128.         return varMarker;
    129.     }
    130.  
    131.     static void AnchorsToCorners() // Sets the anchors to the corner of the Marker for it to scale properly.
    132.     {
    133.         foreach (Transform transform in Selection.transforms)
    134.         {
    135.             RectTransform t = transform as RectTransform;
    136.             RectTransform pt = Selection.activeTransform.parent as RectTransform;
    137.  
    138.             if (t == null || pt == null) return;
    139.  
    140.             Vector2 newAnchorsMin = new Vector2(t.anchorMin.x + t.offsetMin.x / pt.rect.width,
    141.                                                 t.anchorMin.y + t.offsetMin.y / pt.rect.height);
    142.             Vector2 newAnchorsMax = new Vector2(t.anchorMax.x + t.offsetMax.x / pt.rect.width,
    143.                                                 t.anchorMax.y + t.offsetMax.y / pt.rect.height);
    144.  
    145.             t.anchorMin = newAnchorsMin;
    146.             t.anchorMax = newAnchorsMax;
    147.             t.offsetMin = t.offsetMax = new Vector2(0, 0);
    148.         }
    149.     }
    150. }
     
  17. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You can use Instantiate in plain C# objects by just referencing
    UnityEngine.Object
    :
    Code (CSharp):
    1. public class MyPlainClass {
    2.   public void Instantiate(GameObject gameObject) {
    3.     UnityEngine.Object.Instantiate(gameObject);
    4.   }
    5. }
    Works like normal.
     
    bobisgod234 likes this.
  18. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    But Instantiate is a static function. It can be called from anywhere...
     
  19. reendevelops

    reendevelops

    Joined:
    May 6, 2021
    Posts:
    15
    Oh right LOL did not know that

    I just tested and it would've worked that way too. However, the Marker GameObject would still need to be referenced, whereas in the other method, it creates a new one. Easy enough to figure out, but

    I'll keep it the way it is for memento's sake.

    This has been very educational. Thank you both.