Doubts about Canvas & Panels in Unity3d

Discussion in 'Game Development (Technical)' started by Kradsbo, Feb 14, 2016.

  1. Kradsbo

    Kradsbo New Member

    Joined:
    Feb 2, 2016
    Messages:
    2
    Likes Received:
    0
    Hello

    I'm trying to take some components inside a prefav who is a Canvas, the prefav inside have two panels and, on each one of them, inside them, have 4 buttons, all of this is just a single prefab, like this:

    Canvas
    -Panel1
    --Button1
    --Button2
    --Button3
    --Button4
    -Panel2
    --Button5
    --Button6
    --Button7
    --Button8

    i want to access this buttons to try -somehow- to add the script to the OnClick() area of the button and then, select a function inside it, and add a variable if the function requiere it, so the questions are simple:

    How exactly i can access the panels and the buttons from C#?

    And then, how can i access the OnClick() selected script area of the buttons through C#?

    How can i from then get the function of that script through C#?

    And how i can, after that, bring a variable value if the function ask for it through C#?

    And also:

    What are exaclty at component level the panels?, i know they (i guess/heard) can't be selected like "Panels", so the question stands, whats exactly the type of component that a panel is?, it doesn't seem in the GameObject list than there is actualy a Panel type at all.

    This is what i have so far:

    Code:
    ...
    Canvas canvasPrefab =LoadSprite();
    canvasPrefab.name ="canvasPrefab";
    Instantiate(canvasPrefab,Camera.main.transform.position,Camera.main.transform.rotation);
    canvasPrefab.GetComponent(Canvas);//Not sure if this is right
    ...
    

    Any comment, question or request for clarification will also be much apreciated.

    Thanks in advance
     
  2. Frozen Cube

    Frozen Cube New Member

    Joined:
    Feb 2, 2016
    Messages:
    27
    Likes Received:
    22
    Forgive me if I haven't fully understood your question, but it looks like you are horribly over complicating things for yourself. Firstly to use the new UI buttons, all you have to do is make sure that your script is attached to a gameobject, then on the button itself, press the little "+" to add a function under the "On Click()" area. Then you drag-and-drop the gameobject with the script into the provided slot and select the function you want the button to perform from the desired script attached to the gameobject.

    In terms of accessing UI through C#, all you need to do is add this to the top of your script:

    using UnityEngine.UI;

    Panels can be accessed as Images, for example (as a variable):

    public UI.Image panel;

    I hope this clarifies things for you. :)
     
  3. MilosMike

    MilosMike New Member

    Joined:
    Jan 28, 2016
    Messages:
    3
    Likes Received:
    0
    Does it need to be a prefab? Put canvas in hierarchy, let me try to explain how I did it:

    Create empty GameObject in hierarchy. Name it 'GManager' (or something like that). Create new C# script, name it GameManager.cs and attach it to new 'GManager' GameObject.

    In GameManager.cs declare public variables:

    Code:
    public GameObject Panel1;
    
    public GameObject Panel2;
    
    Save it.

    From unity, select your 'GManager', drag from hierarchy panel1 and panel2 in to the respected fields (public variables you've created earlier) in inspector.
    Your Panle1 and Panel2 should be inactive.

    Now, you have references for Panel1 and Panel2, when you need Panel1 you can easily show it, like in this example:

    Code:
      void Start()
        {
         Panel1.SetActive(true);
    }
    
    If you want to switch it off just use:
    Code:
    Panel1.SetActive(false);
    
    Same GameManager.cs script can handle all buttons OnClick. Only if you have created UI buttons (Unity 5).

    Create 8 public void functions for each button, choose names that fits you. Here is an example:
    Code:
     public void Panel1Button1OnClick()
        {
         // do something
        }
    
    
    Save all of this.

    In Unity select, lets say button1 (panel1).
    In the inspector look for the 'button (script)'. There is 'On Click()', it is empty. Now click on +
    Locate box : 'None (Object)'. Drag there your 'GManager' GameObject.
    At this point you can select from drop down menu under 'On Click()', functions from GameManager.cs and select function Panel1Button1OnClick()

    You have references to Panel1 and Panel2, you have OnClick event handles for each of button.
    I hope some of this will work for you.
     

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer