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 make custom UI controls/components using C#?

Discussion in 'UGUI & TextMesh Pro' started by topaz7, Mar 24, 2015.

  1. topaz7

    topaz7

    Joined:
    Dec 15, 2012
    Posts:
    76
    I want to make tab window control for the UI and some other control but I don't know how to begin. Any help would be greatly appreciated.
     
  2. crushhh

    crushhh

    Joined:
    Mar 22, 2015
    Posts:
    12
    Think about the elements of a typical Tab control.

    You have the tabs themselves, which can be represented as a button or image with a click event bound to it.
    You have a pane that will hold the content of each tab, or you could generate a pane for each tab and only show the currently selected tabs pane.

    If you want your Tabs aligned across the top of the tab, you'd likely give them a common GameObject parent and apply a Horizontal Layout component to it. Don't forget to add the accompanying Layout Item component to each Tab.

    You could apply a Vertical Layout component to the TabControl GameObject which would layout your Tabs on top of your content pane.

    Tie the functionality together in your TabControl script.

    Hope that helps you get started.
     
    topaz7 likes this.
  3. topaz7

    topaz7

    Joined:
    Dec 15, 2012
    Posts:
    76
    Good advice there crushhh. I made a control, now how do I reuse it as a control and add it to another UI Element?
     
  4. crushhh

    crushhh

    Joined:
    Mar 22, 2015
    Posts:
    12
    You can drag it to your Project to turn it into a prefab.

    Once you have a prefab, you can use the Instantiate function to create an instance of it by passing a reference of the prefab to the function.

    Then, you just need to call setParent() on the instance, and pass the transform of the ui element you want to add the instance to.

    Hope that helps!