Skip to content

Useful Code Snippets

adenine edited this page Jun 21, 2019 · 9 revisions

General Coding

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class CapsuleInteractions : MonoBehaviour {

	public Renderer rend; 
	public float speed = 10.0f;
	public Color altColor = Color.red;


	// Use this for initialization
	void Start () {
		rend = GetComponent<Renderer>();
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown ("s")) {
			toggleCapsule();
		}

		// change rotation
		transform.Rotate(Vector3.left, speed * Time.deltaTime);	

		// change the color of the object
		if (Input.GetKeyDown (KeyCode.R)){
			altColor.r -= 0.1f;
			rend.material.color = altColor;
		}
		if (Input.GetKeyDown (KeyCode.G)){
			altColor.g += 0.1f;
			rend.material.color = altColor;
		}
	}

	public void toggleCapsule() {
		rend.enabled = !rend.enabled;
	}
}

Subscriber

// Subscriber (Modify SpacebrewEvents.cs)

// add this line near the top
sbClient.addEventListener (this.gameObject, "keystroke");

public void OnSpacebrewEvent(SpacebrewClient.SpacebrewMessage _msg) {
		print ("Received Spacebrew Message");
		print (_msg);

		if (_msg.name == "keystroke") {
			// do something cool
// GameObject go = GameObject.Find("BaseBoARd/YourObjectsGoHere/CenterOfUniverse");
				//OrbitManager om = go.GetComponent <OrbitManager> ();
				//om.makeSatellite();
		}
}

Publisher

// Publishing Example
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class YOURCLASSNAMEHERE : MonoBehaviour {

	// Use this for initialization
	SpacebrewEvents sbEvents;

	void Start () {
		GameObject go = GameObject.Find ("SpacebrewObject");
		sbEvents = go.GetComponent <SpacebrewEvents> ();
	}	

	void Update() {
		// Name, Type, Value
		// Values could be “Boolean”, “String”, “Range”
		sbClient.sendMessage("buttonPress", "boolean", "true");
	}
}
Clone this wiki locally