Skip to content

Instantly share code, notes, and snippets.

@jweimann
Last active May 7, 2017 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jweimann/3606571755c5ed8f43b07138e6cb7a7b to your computer and use it in GitHub Desktop.
Save jweimann/3606571755c5ed8f43b07138e6cb7a7b to your computer and use it in GitHub Desktop.
using UnityEngine;
public class PrimitiveCreator : MonoBehaviour
{
private SteamVR_TrackedController _controller;
private PrimitiveType _currentPrimitiveType = PrimitiveType.Sphere;
private void OnEnable()
{
_controller = GetComponent<SteamVR_TrackedController>();
_controller.TriggerClicked += HandleTriggerClicked;
_controller.PadClicked += HandlePadClicked;
}
private void OnDisable()
{
_controller.TriggerClicked -= HandleTriggerClicked;
_controller.PadClicked -= HandlePadClicked;
}
#region Primitive Spawning
private void HandleTriggerClicked(object sender, ClickedEventArgs e)
{
SpawnCurrentPrimitiveAtController();
}
private void SpawnCurrentPrimitiveAtController()
{
var spawnedPrimitive = GameObject.CreatePrimitive(_currentPrimitiveType);
spawnedPrimitive.transform.position = transform.position;
spawnedPrimitive.transform.rotation = transform.rotation;
spawnedPrimitive.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
if (_currentPrimitiveType == PrimitiveType.Plane)
spawnedPrimitive.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
}
#endregion
#region Primitive Selection
private void HandlePadClicked(object sender, ClickedEventArgs e)
{
if (e.padY < 0)
SelectPreviousPrimitive();
else
SelectNextPrimitive();
}
private void SelectNextPrimitive()
{
_currentPrimitiveType++;
if (_currentPrimitiveType > PrimitiveType.Quad)
_currentPrimitiveType = PrimitiveType.Sphere;
}
private void SelectPreviousPrimitive()
{
_currentPrimitiveType--;
if (_currentPrimitiveType < PrimitiveType.Sphere)
_currentPrimitiveType = PrimitiveType.Quad;
}
#endregion
}
@ynechaev
Copy link

ynechaev commented May 7, 2017

Looks like this gist should be updated to work with Unity 5.6:

NullReferenceException: Object reference not set to an instance of an object
PrimitiveCreator.OnEnable () (at Assets/Code/PrimitiveCreator.cs:11)

NullReferenceException: Object reference not set to an instance of an object
PrimitiveCreator.OnEnable () (at Assets/Code/PrimitiveCreator.cs:11)

NullReferenceException: Object reference not set to an instance of an object
PrimitiveCreator.OnDisable () (at Assets/Code/PrimitiveCreator.cs:17)
UnityEngine.GameObject:SetActive(Boolean)
SteamVR_ControllerManager:OnEnable() (at Assets/SteamVR/Scripts/SteamVR_ControllerManager.cs:60)

NullReferenceException: Object reference not set to an instance of an object
PrimitiveCreator.OnDisable () (at Assets/Code/PrimitiveCreator.cs:17)
UnityEngine.GameObject:SetActive(Boolean)
SteamVR_ControllerManager:OnEnable() (at Assets/SteamVR/Scripts/SteamVR_ControllerManager.cs:60)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment