Author Topic: GUI button to NGUI button ?  (Read 3003 times)

D0R1N

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 3
    • View Profile
GUI button to NGUI button ?
« on: July 26, 2014, 06:50:29 AM »
Hi friends, I'm using a GUI Sprite Button for JUMPING on a mobile game, with this script attached :
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(GUIElement))]
  4. public class Button : MonoBehaviour
  5. {
  6.  
  7.     public string buttonName = "Fire1";                     // The name used for the button
  8.     public bool pairedWithInputManager;
  9.     private AbstractButton m_Button;
  10.  
  11.     void OnEnable()
  12.     {
  13.         m_Button = ButtonFactory.GetPlatformSpecificButtonImplementation ();
  14.         m_Button.Enable(buttonName, pairedWithInputManager, GetComponent<GUIElement>().GetScreenRect());
  15.     }
  16.  
  17.  
  18.     void OnDisable()
  19.     {
  20.  
  21.         // remove the button from the cross platform input manager
  22.         m_Button.Disable ();
  23.     }
  24.  
  25.     void Update()
  26.     {
  27.         m_Button.Update ();
  28.     }
  29. }
I've decided to use NGUI in my main project to reduce draw calls. Before getting NGUI I want to know if it's possible to use instead NGUI Button to perform this ;) someone can help me with this ? Thanks in advance !

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: GUI button to NGUI button ?
« Reply #1 on: July 27, 2014, 03:08:20 AM »
I'm pretty sure you can answer your own question just by reading the documentation for the UIButton: http://www.tasharen.com/forum/index.php?topic=6708.0

Note the code snipplet there.

D0R1N

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: GUI button to NGUI button ?
« Reply #2 on: July 28, 2014, 10:32:35 AM »
Thanks for the suggestion ArenMook ... but no solution on my question ... I tried a lot of ideas and suggestions ... can't figure out how to make this work :( Any example....or any idea how to convert this code to work on NGUI...I really wont to move on NGUI System  :(

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: GUI button to NGUI button ?
« Reply #3 on: July 28, 2014, 02:21:40 PM »
It's fairly simple. You just need to drop your preconceptions - Out with the ButtonFactory, out with Update() checking. What is m_button anyway?

You just attach a UIButton script to your thing and then make either a OnPress(bool pressed) or OnClick() method, which will automatically get called if there's a collider on that gameobject. it's that simple.

The easiest thing for you is to look at the examples folder in NGUI or try out the free version, just to get an idea of how things work. Get the free version here: http://www.tasharen.com/get.php?file=NGUI

Do note that the free version is obsolete, and should only be used as a very basic introduction to how NGUI's general API works.

D0R1N

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: GUI button to NGUI button ?
« Reply #4 on: July 30, 2014, 09:29:20 AM »
Hi Nicki, I really appreciate your feedback, I'm new in Unity3d and especially with NGUI system that I think is one of the greatest plugin for unity3d, so before I get NGUI... I'm trying to figure out if it's possible to use NGUI for Player Controls ...will be much appreciated an quick/little example for this issue !

P.S: The
  1. m_Button
is from the Unity 2D Character Controllers provided in this turoial : http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers

Let's say I have this code :
  1.         void Update() {
  2.                                 if (Input.GetButton ("Fire") && Time.time > nextFire) {
  3.                                                 nextFire = Time.time + fireRate;
  4.                                                 Instantiate (bullet, spawnPoint.position, spawnPoint.rotation);
  5.                                                 audio.Play();
  6.                                 }
  7.                 }
How to convert my code to work with NGUI Button  :( ? This example will be great for begginers like me ...
Thanks in advance ;)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: GUI button to NGUI button ?
« Reply #5 on: July 30, 2014, 10:54:21 AM »
  1. bool mIsPressed = false;
  2.  
  3. void OnPress (bool isPressed)
  4. {
  5.     mIsPressed = isPressed;
  6. }
  7.  
  8. void Update()
  9. {
  10.     if (mIsPressed)
  11.     {
  12.         nextFire = Time.time + fireRate;
  13.         Instantiate (bullet, spawnPoint.position, spawnPoint.rotation);
  14.         audio.Play();
  15.     }
  16. }