How do I can use Arduino push button trigger unity UI button (on click ()) , no direct active effect? code can function but it had duplicate code and not the result that I want.
**code in arduino
const int buttonPin = 2;
const int ledPin = 13;
void setup() { Serial.begin(9600);
pinMode (buttonPin, INPUT); pinMode(ledPin, OUTPUT);
digitalWrite(buttonPin, HIGH); }
void loop() { if(digitalRead(buttonPin)==LOW) { //Serial.println("LEFT"); Serial.write(1); Serial.flush(); delay(20); digitalWrite(ledPin, HIGH); }else { digitalWrite(ledPin, LOW); } }
code in unity ( arduino function with unity)
using UnityEngine; using System.Collections; using System.IO.Ports;
public class TulScript : MonoBehaviour { public ParticleSystem ParticleSystemFirePoint1; SerialPort sp = new SerialPort("COM4", 9600);
void Start()
{
sp.Open();
sp.ReadTimeout = 1;
}
void Update()
{
//amountToMove = speed * Time.deltaTime;
if (sp.IsOpen)
{
try
{
MoveObject(sp.ReadByte());
print(sp.ReadByte());
}
catch (System.Exception)
{
}
}
}
public void MoveObject(int Direction)
{
if (Direction == 1)
{
if (ParticleSystemFirePoint1.isPlaying)
{
ParticleSystemFirePoint1.Stop();
}
else
{
ParticleSystemFirePoint1.Play();
}
}
}
}
code in unity ( action with effect) using System.Collections; using System.Collections.Generic; using UnityEngine;
public class FirePoint1 : MonoBehaviour { public ParticleSystem ParticleSystemFirePoint1;
void Start () {
}
void Update () {
}
public void ToggleFirePoint1()
{
if (ParticleSystemFirePoint1.isPlaying)
{
ParticleSystemFirePoint1.Stop();
}
else
{
ParticleSystemFirePoint1.Play();
}
}
}
code in unity ( change button color)**
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
[RequireComponent(typeof(Button))] public class OpenFirePoint1 : MonoBehaviour
{
public Button FirePoint1Button;
public Sprite FirePoint1;
public Sprite FirePoint1_OpenFirePoint1;
private int counter = 0;
void Start () {
FirePoint1Button = GetComponent<Button>();
}
public void changeButton()
{
counter++;
if (counter % 2==0)
{
FirePoint1Button.image.overrideSprite = FirePoint1;
}
else
{
FirePoint1Button.image.overrideSprite = FirePoint1_OpenFirePoint1;
}
}
}
Your answer
Follow this Question
Related Questions
Can't access functions on my UI button 1 Answer
Does multiple spamming a button break it? 2 Answers
How to fix button clones that aren't working? 0 Answers
Cube rotation on Unity makes rotates servo on Arduino 0 Answers
why my static timer variable print 0 everytime i call the getcolor function?! 0 Answers