2
0

ButtonExample.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace Valve.VR.InteractionSystem.Sample
  4. {
  5. public class ButtonExample : MonoBehaviour
  6. {
  7. public HoverButton hoverButton;
  8. public GameObject prefab;
  9. private void Start()
  10. {
  11. hoverButton.onButtonDown.AddListener(OnButtonDown);
  12. }
  13. private void OnButtonDown(Hand hand)
  14. {
  15. StartCoroutine(DoPlant());
  16. }
  17. private IEnumerator DoPlant()
  18. {
  19. GameObject planting = GameObject.Instantiate<GameObject>(prefab);
  20. planting.transform.position = this.transform.position;
  21. planting.transform.rotation = Quaternion.Euler(0, Random.value * 360f, 0);
  22. planting.GetComponentInChildren<MeshRenderer>().material.SetColor("_TintColor", Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f));
  23. Rigidbody rigidbody = planting.GetComponent<Rigidbody>();
  24. if (rigidbody != null)
  25. rigidbody.isKinematic = true;
  26. Vector3 initialScale = Vector3.one * 0.01f;
  27. Vector3 targetScale = Vector3.one * (1 + (Random.value * 0.25f));
  28. float startTime = Time.time;
  29. float overTime = 0.5f;
  30. float endTime = startTime + overTime;
  31. while (Time.time < endTime)
  32. {
  33. planting.transform.localScale = Vector3.Slerp(initialScale, targetScale, (Time.time - startTime) / overTime);
  34. yield return null;
  35. }
  36. if (rigidbody != null)
  37. rigidbody.isKinematic = false;
  38. }
  39. }
  40. }