Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

List of gameobject components

Discussion in 'Scripting' started by Arakjin, May 17, 2019.

  1. Arakjin

    Arakjin

    Joined:
    Nov 12, 2017
    Posts:
    10
    Hello all

    I'm trying to make a Augmentative and alternative communication program which creates multiple different objects through script

    I'm trying to create new gameobjects through method "CreateGameObject" and pass components through a list since doing huge lists of this .AddComponent for every different gameobject gets kinda messy.

    Here is one such messy bit and I need to create many different kinds:
    Code (CSharp):
    1.  
    2.     private void CreateImages(Texture texture, string name, int total, byte[] uwr, string uid)
    3.     {
    4.         //Initialize new gameobject
    5.         GameObject btn = new GameObject(name);
    6.         //Add components for new gameobject
    7.         btn.AddComponent<RectTransform>();
    8.         btn.AddComponent<CanvasRenderer>();
    9.         btn.AddComponent<VerticalLayoutGroup>();
    10.         btn.AddComponent<Image>();
    11.         btn.AddComponent<Button>();
    12.         //Set Component Image settings
    13.         btn.GetComponent<Image>().sprite = background;
    14.         btn.GetComponent<Image>().color = ConvertColor(225, 225, 225, 255);
    15.         btn.GetComponent<Image>().preserveAspect = true;
    16.         btn.GetComponent<Image>().type = Image.Type.Sliced;
    17.         btn.GetComponent<Image>().fillCenter = true;
    18.         btn.GetComponent<Image>().raycastTarget = true;
    19.         //Set Component Button settings
    20.         btn.GetComponent<Button>().transition = Selectable.Transition.ColorTint;
    21.         ColorBlock colors = btn.GetComponent<Button>().colors;
    22.         colors.normalColor = ConvertColor(255, 255, 255, 255);
    23.         colors.highlightedColor = ConvertColor(245, 245, 245, 255);
    24.         colors.pressedColor = ConvertColor(200, 200, 200, 255);
    25.         colors.disabledColor = ConvertColor(200, 200, 200, 128);
    26.         btn.GetComponent<Button>().colors = colors;
    27.         //Set Component VerticalLayoutGroup settings
    28.         btn.GetComponent<VerticalLayoutGroup>().padding.left = 15;
    29.         btn.GetComponent<VerticalLayoutGroup>().padding.right = 15;
    30.         btn.GetComponent<VerticalLayoutGroup>().padding.top = 15;
    31.         btn.GetComponent<VerticalLayoutGroup>().padding.bottom = 15;
    32.         btn.GetComponent<VerticalLayoutGroup>().childAlignment = TextAnchor.MiddleCenter;
    33.         btn.GetComponent<VerticalLayoutGroup>().childControlHeight = true;
    34.         btn.GetComponent<VerticalLayoutGroup>().childControlWidth = true;
    35.         btn.GetComponent<VerticalLayoutGroup>().childForceExpandHeight = true;
    36.         btn.GetComponent<VerticalLayoutGroup>().childForceExpandWidth = true;
    37.         //Initialize img child object
    38.         GameObject img = new GameObject("Img");
    39.         //Add components for child object
    40.         img.AddComponent<RectTransform>();
    41.         img.AddComponent<CanvasRenderer>();
    42.         if (total != 0)
    43.         {
    44.             img.AddComponent<RawImage>();
    45.             //set texture
    46.             img.GetComponent<RawImage>().texture = texture;
    47.             // Add script to button
    48.             btn.GetComponent<Button>().onClick.AddListener(() => SelectImage(uid, uwr, btn.GetComponent<Image>()));
    49.         }
    50.         else
    51.         {
    52.             img.AddComponent<Text>();
    53.             img.GetComponent<Text>().resizeTextForBestFit = true;
    54.             img.GetComponent<Text>().color = Color.black;
    55.             img.GetComponent<Text>().alignment = TextAnchor.MiddleCenter;
    56.             img.GetComponent<Text>().font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
    57.             img.GetComponent<Text>().text = "Empty search result";
    58.         }
    59.         //Create gameobject in panel
    60.         btn.transform.SetParent(SearchPanel.transform, false);
    61.         //Create child image for button
    62.         img.transform.SetParent(btn.transform, false);
    63.     }
    64.  
    I tried something like this, but it doesn't work:
    Code (CSharp):
    1.  
    2.     private GameObject CreateGameObject(string name, List<Component> components)
    3.     {
    4.         GameObject o = new GameObject(name);
    5.         foreach (Component item in components)
    6.         {
    7.             o.AddComponent<item>();
    8.         }
    9.         return o;
    10.     }
    11.  
     
    Last edited: May 17, 2019
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    You really should use code tags. And if you like for people to suggest somethng, you really should ask a question.

    I'm guessing that you are looking for a general way of adding components to your game objects, so based on the fact that all components are descendants of the <component> type, your CreateGameObject method should work. Can you tell us why that did not work?
     
  3. Arakjin

    Arakjin

    Joined:
    Nov 12, 2017
    Posts:
    10
    Sorry, figured those code tags a little late.

    I figured it out, Component was wrong class for this, I'll leave this if someone else needs it

    Code (CSharp):
    1. private GameObject CreateGameObject(string name, Type[] components)
    2.     {
    3.         GameObject o = new GameObject(name);
    4.         foreach (Type item in components)
    5.         {
    6.             o.AddComponent(item);
    7.         }
    8.         return o;
    9.     }
    And for calling it

    Code (CSharp):
    1. CreateGameObject("Object", new Type[] { typeof(Text), typeof(Image), typeof(VerticalLayoutGroup)});
    2.  
    Now is there a way to pass settings like this?
     
    Last edited: May 17, 2019