Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using System.IO.Ports;
  4.  
  5. public class ReadUSBChangeShader : MonoBehaviour
  6. {
  7.  
  8. public Shader rojo;
  9. public Shader normal;
  10. public Renderer rend;
  11.  
  12. SerialPort sp; //whatever COM arduino uses
  13.  
  14. private void Awake()
  15. {
  16. try
  17. {
  18. sp = new SerialPort("COM3", 9600);
  19. }
  20. catch (System.Exception)
  21. {
  22.  
  23. }
  24. }
  25. void Start()
  26. {
  27. try
  28. {
  29. sp.Open();
  30. sp.ReadTimeout = 1;
  31. rend = GetComponent<Renderer>();
  32. }
  33. catch (System.Exception)
  34. {
  35.  
  36. }
  37. }
  38.  
  39. void Update()
  40. {
  41. if (sp.IsOpen)
  42. {
  43. try
  44. {
  45. int ln = sp.ReadByte();
  46. if (ln == 1)
  47. {
  48. rend.material.shader = rojo;
  49.  
  50. }
  51. else {
  52. rend.material.shader = normal;
  53. }
  54. }
  55.  
  56. catch (System.Exception)
  57. {
  58.  
  59. }
  60. }
  61. else
  62. {
  63. if (Input.GetKeyDown(KeyCode.R))
  64. {
  65. //Do something
  66. Debug.Log("lee alterno");
  67. }
  68.  
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement