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
1
Question by Wesww · Jun 06, 2012 at 07:47 PM · javascriptitweennguiplaymaker

NGUI - Simulate a button press from script

Hey all, I purchased NGUI a couple weeks ago, which is a pretty terrific package (although I was slightly bummed when two days after I bought it it went on sale for half the price, but what are you gonna do).

NGUI has useful scripts for tweening, triggering a sound, etc when you click on a button, so I was wondering how to script to have a button "click itself" by triggering it from another script?

Comment
Add comment
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

3 Replies

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

Answer by cordinc · Jun 12, 2012 at 07:32 AM

The button scripts in NGUI receive notice of a click through the OnClick() method. To trigger the click from another script call send this message to all the components on the button. For instance, if you have a script on the button that should "click" the button when the 'T' key is pressed then the code to do this would be:

 void Update() {
     if (Input.GetKeyDown(KeyCode.T)) {
         SendMessage("OnClick");
     }
 }

If the script is on a different game object to the button, then you will need to call SendMessage() on the button's game object.

Other NGUI button events can also be triggered this way. For instance: SendMessage("OnHover", true) would trigger the button to act like the mouse is hovering over it (remember to send the corresponding false message when you want the hover effect to stop).

Comment
Add comment · Show 4 · 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 Wesww · Jun 25, 2012 at 04:43 PM 0
Share

I just changed this answer to be marked as correct. Very nice.

avatar image atomicjoe · Apr 24, 2013 at 04:25 PM 0
Share

Hey thanks! I just purchased NGUI and was looking for a way to navigate the interface with a gamepad. I have tested this with playmaker and works like a charm! :D

avatar image redorav · Aug 11, 2013 at 02:11 PM 0
Share

I can't vote yet but I would upvote this answer a million times. Very nice

avatar image Ratboy601 · Jul 10, 2014 at 10:54 PM 0
Share

Even two years later, this is a sublime solution. Thank you. :D

avatar image
0

Answer by bompi88 · Jun 06, 2012 at 08:21 PM

why do you actually need that? You could solve it by:

FirstButtonScript.cs:

 using UnityEngine;
 using System.Collections;
 
 public class FirstButtonScript : MonoBehaviour {
     public GameObject buttonToTrigger; // drag your button you want to trigger here in inspector
 
     void OnClick () {
         buttonToTrigger.GetComponent<ButtonToTriggerScript>().ClickMe();
     }
 
 }

ButtonToTriggerScript.cs:

 using UnityEngine;
 using System.Collections;
 
 public class ButtonToTriggerScript : MonoBehaviour {
 
     void OnClick () {
         ClickMe();
     }
     
     void ClickMe() {
         // Do whatever you want
     }
 }
Comment
Add comment · Show 1 · 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 stanford · Sep 04, 2012 at 08:35 AM 0
Share

i m not able to do,when i press button i want to show new texture please learn me through script in NGUI

avatar image
0

Answer by Wesww · Jun 10, 2012 at 02:55 AM

Thanks for your reply Bjørn, but based on your response I feel I must have been too vague in my question. When I said:

"NGUI has useful scripts for tweening, triggering a sound, etc when you click on a button," and asked for how to trigger a button being pressed from script,

the reason this would have been useful is that I had numerous scripts attached to each button, and each script was specifying a dozen variables configured in the editor (with which I was tweening objects, playing sounds, etc).

Simply triggering the script like you suggested negated the usefulness of much of the editor customizations on those scripts.

My solution: I removed all of my NGUI iTween scripts, since they can't easily be accessed through scripting, and instead replaced their functionality by buying Playmaker and using the built in iTween commands in that package. So far so good.

Thanks anyway!

Oh and in case it helps somebody, here is a paired down version of some of my script.

Script 1:

 import System.Collections.Generic;
 
 var StateMachine : PlayMakerFSM;
 
 function Start() {
     state = "select";
 }
 
 function back(){
     if(state == "select"){
         //button not available
     }else if(state == "customize"){
         modeCharSelect();
     }else if (state == "whack"){
         var funct = (currentChar==char01)?"modeChar01Customize":(currentChar==char02)?"modeChar02Customize":"nil";
     if(funct!="nil")
         gameObject.SendMessage(funct);
     }else if (state == "stuff"){
         modeWhack();
     }else if (state == "send"){
         modeStuff();
     }
 }
 

 function modeChar01Customize () {
     state = "customize";
     StateMachine.Fsm.Event("modeChar01Customize");
     Debug.Log(state);
 }




Script 2:

  var mainCamera: GameObject;
 
 //can't use onmouseup code because I'm developing for iOS:
     function OnClick(){
         mainCamera.GetComponent(_MAIN).back(); 
     }
Comment
Add comment · Show 1 · 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 bompi88 · Jun 12, 2012 at 07:41 AM 0
Share

Another thing. Take a look at HOTween, it's better performance wise on iOS.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Changing scripts from old iTween to Path Editor 0 Answers

Joystick movement 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 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