Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 breno1080 · Oct 10, 2015 at 12:39 AM · c#androiduibuttons

How convert this Door Script to work with Ui Buttons!

I made a door script to work with the key "E" but i need convert this to a Ui Button(for android) The Script:

     public bool EstaTrancada,PrecisaDeChave;
     public AudioClip PortaNormal,PortaTrancada,SomDeChave;
     public float distanciaParaAbrir = 3;
     public Font Fonte;
     public float VelocidadeDeGiro = 60;
     public int IDdaPorta;
     public static List<int> ListaDeIDs = new List<int>();
     private bool EstaAberta,PodeAbrir,AvisoTrancada,temAChave;
     private float CronometroDoAviso,CronometroMovimento;
     private float RotacaoFechada,RotacaoAberta;
     private GameObject Jogador;
     private bool MovimentarPorta;
 
     void Start (){
         EstaAberta = false;
         AvisoTrancada = false;
         temAChave = false;
         RotacaoFechada = transform.eulerAngles.y;
         RotacaoAberta = transform.eulerAngles.y + 90;
         if (RotacaoAberta > 360) {
             RotacaoAberta = transform.eulerAngles.y + 90 -360;
         }
         Jogador = GameObject.FindWithTag ("Player");
         if (PrecisaDeChave == true) {
             EstaTrancada = true;
         }
     }
     void Update (){
         // CHECAHDO SE ESTA PERTO OU NAO
         if (Vector3.Distance (transform.position, Jogador.transform.position) <= distanciaParaAbrir) {
             PodeAbrir = true;
         } else if (Vector3.Distance (transform.position, Jogador.transform.position) > distanciaParaAbrir) {
             PodeAbrir = false;
         }
         //CHECANDO SE ESTA TRANCADA OU NAO... SE NAO ESTIVER, PODE ABRIR
         if (EstaTrancada == false) {
             if(Input.GetKeyDown("e") && MovimentarPorta == true && PodeAbrir == true){
                 CronometroMovimento = 0;
                 EstaAberta = !EstaAberta;
                 GetComponent<AudioSource>().Stop ();
                 GetComponent<AudioSource>().PlayOneShot(PortaNormal);
             }
             else if(Input.GetKeyDown("e") && PodeAbrir == true && MovimentarPorta == false){
                 GetComponent<AudioSource>().PlayOneShot(PortaNormal);
                 MovimentarPorta = true;
             }
         }
         // SE A PORTA ESTIVER TRANCADA 
         if (Input.GetKeyDown ("e") && PodeAbrir == true && EstaTrancada == true) {
             //CHECA SE O PALYER TEM A CHAVE OU NAO
             for(int x = 0; x < ListaDeIDs.Count; x++){
                 if(IDdaPorta == ListaDeIDs[x]){
                     temAChave = true;
                 }else{
                     temAChave = false;
                 }
             }
             // SE O PALYER TEM A CHAVE
             if(temAChave == true && PrecisaDeChave == true){
                 EstaTrancada = false;
                 if(!GetComponent<AudioSource>().isPlaying){
                     GetComponent<AudioSource>().PlayOneShot(SomDeChave);
                 }
             }
             // SE O PALYER NAO TEM A CHAVE
             else {
                 AvisoTrancada = true;
                 if(!GetComponent<AudioSource>().isPlaying){
                     GetComponent<AudioSource>().PlayOneShot(PortaTrancada);
                 }
             }
         }
         // CRONOMETRO DO AVISO DA PORTA TRANCADA
         if (AvisoTrancada == true) {
             CronometroDoAviso += Time.deltaTime;
         }
         if (CronometroDoAviso >= 3) {
             AvisoTrancada = false;
             CronometroDoAviso = 0;
         }
         // CRONOMETRO DO MOVIMENTO DA PORTA
         if (MovimentarPorta == true) {
             CronometroMovimento += Time.deltaTime;
         }
         if(CronometroMovimento >= 2 + 75/VelocidadeDeGiro){
             MovimentarPorta = false;
             CronometroMovimento = 0;
             EstaAberta = !EstaAberta;
         }
     }
     void FixedUpdate (){
         // MOVIMENTO DE ABRIR A PORTA
         if (MovimentarPorta == true && EstaAberta == false) {
             Vector3 rotacaoFinal = new Vector3(0,RotacaoAberta,0);
             transform.eulerAngles = Vector3.Lerp (transform.eulerAngles,rotacaoFinal,Time.deltaTime*(VelocidadeDeGiro/50));
         }
         // MOVIMENTO DE FECHAR A PORTA
         else if (MovimentarPorta == true && EstaAberta == true) {
             Vector3 rotacaoFinal = new Vector3(0,RotacaoFechada,0);
             transform.eulerAngles = Vector3.Lerp (transform.eulerAngles,rotacaoFinal,Time.deltaTime*(VelocidadeDeGiro/50));
         }
     }
     void OnGUI (){
         // AVISO SOBRE PORTA TRANCADA
         GUI.skin.font = Fonte;
         GUI.skin.label.fontSize = Screen.height / 20;
         if (AvisoTrancada == true) {
             if(PrecisaDeChave == true){
                 GUI.Label(new Rect(Screen.width/2-Screen.width/5,Screen.height/2-Screen.height/16,Screen.width/2.5f,Screen.height/8),"Voce precisa de uma chave"); 
             }
             else if(PrecisaDeChave == false){
                 GUI.Label(new Rect(Screen.width/2-Screen.width/5,Screen.height/2-Screen.height/16,Screen.width/2.5f,Screen.height/8),"Nunca ira abrir"); 
             }
         }
     }
 } 

code>

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ali_Jaffer · Oct 10, 2015 at 08:19 AM

First Add this code in your script

//Variable bool isDoorOpen = false;

//For UI Button Public void DoorOpenButtonClick() { isDoorOpen = true; }

//in Update() //In line if (Input.GetKeyDown ("e") && PodeAbrir == true && EstaTrancada == true) //Replace it withif (isDoorOpen && PodeAbrir == true && EstaTrancada == true)

  1. create A UI Button (Create>UI>Button)

  2. Select the Button and goInto inspectorWindow and find OnClickEvent in Button Component Attached to it.

  3. Click "+"

  4. Drag the GameObject that contains your Code.

  5. click dropDownMenu and (Select your Script And Select DoorOpenButtonClick I hope this will solve your problem

for more visit this https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button?playlist=17111

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

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

Please help! After build on Android UI buttons can't enable/disable script but works when using Unity remote. 0 Answers

UI Buttons sometimes not detecting touch 1 Answer

Distribute terrain in zones 3 Answers

How can you make images appear on (UI) buttons in a scroll box/scrollable list/scrollview? 0 Answers

Double Jump With UI BUTTONS; 0 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