Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unityplease · Jan 02, 2015 at 11:18 AM · c#uibutton

How to create a 4.6 UI button NOT from prefab NOT in unity editor only from C#

I've read a lot of questions and answers and watched endless youtube videos but I can't find the answer to this anywhere.

Frequently people suggest instatiating a prefab button or worse ignore the question conditions and just say "drag and drop the button and function".

I really would like to create an entire 4.6 UI button ONLY using C#. complete with mouse over and on click functions.

I have found that the following doesn't work:

   MyButton.onClick.AddListener(() => { MyFunction();})


I create the following example c# code that shows I cannot reach the handleButton method and it does not print "pressed" when I click the invisible button.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 public class CreateCanvasButton : MonoBehaviour {
 
 
 
 
     void Start () {
         // create event system
         GameObject eventsystem = new GameObject ();
         eventsystem.AddComponent<EventSystem> ();
         eventsystem.AddComponent<StandaloneInputModule> ();
         eventsystem.AddComponent<TouchInputModule> ();
 
 
         string name = "Canvas";
 
         GameObject newCanvasGO = new GameObject ();
         newCanvasGO.name = name;
         newCanvasGO.transform.SetParent (this.transform);
         newCanvasGO.AddComponent <Canvas>();
         newCanvasGO.AddComponent<CanvasScaler> ();
         newCanvasGO.AddComponent<GraphicRaycaster> ();
 
         RectTransform theCanvasRectTransform = newCanvasGO.GetComponent<RectTransform> ();
         theCanvasRectTransform.anchoredPosition3D = new Vector3 (0f, 0f, 0f);
         theCanvasRectTransform.sizeDelta = new Vector2 (2400f,3800f);
         theCanvasRectTransform.anchorMin = new Vector2 (0f,0f);
         theCanvasRectTransform.anchorMax = new Vector2 (1f,1f);
         theCanvasRectTransform.localScale = new Vector3 (0.0005f,0.0005f,1f);
         theCanvasRectTransform.localPosition = new Vector3 (0f, 2.9f, 0f);
         Canvas theCanvas = newCanvasGO.GetComponent<Canvas> ();
         theCanvas.worldCamera = Camera.main;
         newCanvasGO.SetActive (true);
         newCanvasGO.AddComponent<Image> ();
         Image canvasImage = newCanvasGO.GetComponent<Image> ();
         Sprite mySprite = Resources.Load<Sprite> ("Background");
         canvasImage.sprite = mySprite;
         canvasImage.type = Image.Type.Sliced;
         // create name title text
         GameObject titleTextGO = new GameObject ();
         titleTextGO.name = "Name_text";
         titleTextGO.transform.parent = newCanvasGO.transform;
         Text titleText = titleTextGO.AddComponent<Text> ();
         RectTransform titleTextRT = titleTextGO.GetComponent<RectTransform> ();
         titleTextRT.anchoredPosition3D = new Vector3 (0f,0f,0f);
         titleTextRT.sizeDelta = new Vector2 (877,199);
         titleTextRT.localPosition = new Vector3 (-22f,1722f,0f);
         titleTextRT.localScale = new Vector3 (1f, 1f, 1f);
         titleText.text = "Title";
         Font theFont = Resources.GetBuiltinResource<Font> ("Arial.ttf");
         titleText.font = theFont;
         titleText.fontSize = 140;
         titleText.color = Color.black;
         titleText.alignment = TextAnchor.MiddleCenter;
         titleText.resizeTextForBestFit = true;
         titleText.resizeTextMinSize = 10;
         titleText.resizeTextMaxSize = 160;
         // create nickname title text
         GameObject TextGO = new GameObject ();
         TextGO.name = "InputField";
         TextGO.transform.parent = newCanvasGO.transform;
         TextGO.AddComponent<RectTransform> ();
         RectTransform TextRT = TextGO.GetComponent<RectTransform> ();
         TextGO.AddComponent<InputField> ();
         InputField Input = TextGO.GetComponent<InputField> ();
         TextRT.anchoredPosition3D = new Vector3 (0f,0f,0f);
         TextRT.sizeDelta = new Vector2 (1669,3370);
         TextRT.localPosition = new Vector3 (-35f,-188f,0f);
         TextRT.localScale = new Vector3 (1f, 1f, 1f);
         Input.transition = Selectable.Transition.None;
         Input.text = "SomeText that never appears";
         
         
         GameObject textForInputGO = new GameObject ();
         textForInputGO.name = "Text_ForInputField";
         textForInputGO.transform.SetParent(TextGO.transform);
         RectTransform textRT = textForInputGO.AddComponent<RectTransform> ();
         Text textscript = textForInputGO.AddComponent<Text>();
         textRT.sizeDelta = new Vector2 (1356f,300f);
         textRT.localPosition = new Vector3 (0f,-0f,0f);
         textRT.localRotation = new Quaternion (0f, 0f, 0f, 0f);
         textRT.localScale = new Vector3 (1f,1f,1f);
         Input.textComponent = textscript;
         textscript.supportRichText = false;
         textscript.resizeTextForBestFit = true;
         textscript.resizeTextMaxSize = 300;
         textscript.resizeTextMinSize = 10;
         textscript.font = theFont;
         textscript.text = "This is the main body text.";
         textscript.color = Color.black;
         textscript.alignment = TextAnchor.MiddleCenter;
         
 
         // create button
         GameObject invisibleButtonGO = new GameObject ();
         invisibleButtonGO.name = "invisibleButton";
         invisibleButtonGO.transform.parent = newCanvasGO.transform;
         invisibleButtonGO.AddComponent<RectTransform> ();
         RectTransform invisibleButtonRT = invisibleButtonGO.GetComponent<RectTransform> ();
         invisibleButtonRT.anchoredPosition3D = new Vector3 (0f,0f,0f);
         invisibleButtonRT.sizeDelta = new Vector2 (358,354);
         invisibleButtonRT.localPosition = new Vector3 (1021f,-1723f,0f);
         invisibleButtonRT.localScale = new Vector3 (1f, 1f, 1f);
         invisibleButtonGO.AddComponent<Button> ();
         Button invisibleButton = invisibleButtonGO.GetComponent<Button> ();
         invisibleButton.transition = Selectable.Transition.None;
         invisibleButton.onClick.AddListener (() => handleButton());
 
     }
 
     void handleButton (){
         print ("pressed!");
         }
 
 }

Please help I've been trying this for days and have only found unanswered questions such as this one:

http://answers.unity3d.com/questions/855412/adding-a-function-to-a-ui-46-button-called-on-anot.html

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unityplease · Jan 02, 2015 at 11:14 AM 1
Share

well they are created using the new keyword so I assume they are still in memory. The game objects appear in the scene hierachy and the input field seems to work correctly, but the button does not.

avatar image Landern · Jan 02, 2015 at 12:32 PM 0
Share

@unityplease, i missed this line:

 newCanvasGO.transform.SetParent (this.transform);

And thought there wasn't a reference applied to a GameObject in the scene, my bad.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by jenci1990 · Jan 02, 2015 at 11:37 AM

Add image component to your button:

 GameObject invisibleButtonGO = new GameObject();
 invisibleButtonGO.name = "invisibleButton";
 invisibleButtonGO.transform.parent = transform;
 invisibleButtonGO.AddComponent<RectTransform>();
 RectTransform invisibleButtonRT = invisibleButtonGO.GetComponent<RectTransform>();
 invisibleButtonRT.anchoredPosition3D = new Vector3(0f, 0f, 0f);
 invisibleButtonRT.sizeDelta = new Vector2(358, 354);
 invisibleButtonRT.localPosition = new Vector3(1021f, -1723f, 0f);
 invisibleButtonRT.localScale = new Vector3(1f, 1f, 1f);
 invisibleButtonGO.AddComponent<Button>();
 Button invisibleButton = invisibleButtonGO.GetComponent<Button>();
 invisibleButton.transition = Selectable.Transition.None;
 invisibleButton.onClick.AddListener(() => handleButton());
 
 //change:
 Image image = invisibleButtonGO.AddComponent<Image>();
 image.color = new Color(0, 0, 0, 0);

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unityplease · Jan 02, 2015 at 12:18 PM 0
Share

Thanks this works, is this requirement for the event to be registered requiring an image component documented somewhere because it wasn't obvious?

avatar image unityplease · Jan 02, 2015 at 12:29 PM 0
Share

Another question about my code if you can help me, how to I stop the input field from picking up key presses after I press return?

avatar image Scribe · Jan 02, 2015 at 12:38 PM 0
Share

The canvas object has a component called 'GraphicsRaycaster' so for it to register, you must have a graphic component attached 'GraphicRaycaster : A BaseRaycaster to raycast against Graphic elements.'

It's not that well documented, but it makes sense that invisible buttons don't function (you can get round this by adding a transparent texture to the image component I believe)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

UI 4.6 menu "Start" button script c# 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity UI 4.6 Canvas Enable/Disable make Accessible 2 Answers

Pause Menu? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges