PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Assets/Plugins/UnityARKitPlugin/Examples/ARKit1.5/UnityARImageAnchor/GenerateImageAnchor.cs

https://bitbucket.org/paleogma/paleogma-studentapp
C# | 83 lines | 51 code | 23 blank | 9 comment | 5 complexity | 41ca233549939aa1307fd59b1e672893 MD5 | raw file
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.XR.iOS;
  5. public class GenerateImageAnchor : MonoBehaviour {
  6. [SerializeField]
  7. private ARReferenceImage referenceImage;
  8. [SerializeField]
  9. private GameObject prefabToGenerate;
  10. private GameObject imageAnchorGO;
  11. //public AudioClip mammouthAwake;
  12. //AudioSource audioSource;
  13. // Use this for initialization
  14. void Start () {
  15. UnityARSessionNativeInterface.ARImageAnchorAddedEvent += AddImageAnchor;
  16. UnityARSessionNativeInterface.ARImageAnchorUpdatedEvent += UpdateImageAnchor;
  17. UnityARSessionNativeInterface.ARImageAnchorRemovedEvent += RemoveImageAnchor;
  18. //audioSource = GetComponent<AudioSource>();
  19. }
  20. void AddImageAnchor(ARImageAnchor arImageAnchor)
  21. {
  22. Debug.Log ("image anchor added");
  23. if (arImageAnchor.referenceImageName == referenceImage.imageName) {
  24. GlobalTarget.setStep3();
  25. GlobalARSceneState.userIsInFossilChunk = true;
  26. Matrix4x4 wantedPosition = prefabToGenerate.transform.localToWorldMatrix * arImageAnchor.transform;
  27. //Matrix4x4 wantedPosition = arImageAnchor.transform;
  28. Vector3 position = UnityARMatrixOps.GetPosition (wantedPosition);
  29. //Quaternion rotation = UnityARMatrixOps.GetRotation(wantedPosition);
  30. Quaternion rotation = Quaternion.Euler(0,0,0);
  31. imageAnchorGO = Instantiate<GameObject> (prefabToGenerate, position, rotation);
  32. //audioSource.PlayOneShot(mammouthAwake, 0.7F);
  33. }
  34. }
  35. void UpdateImageAnchor(ARImageAnchor arImageAnchor)
  36. {
  37. Debug.Log ("image anchor updated");
  38. if (arImageAnchor.referenceImageName == referenceImage.imageName) {
  39. Matrix4x4 wantedPosition = prefabToGenerate.transform.localToWorldMatrix * arImageAnchor.transform;
  40. imageAnchorGO.transform.position = UnityARMatrixOps.GetPosition (wantedPosition);
  41. //imageAnchorGO.transform.rotation = UnityARMatrixOps.GetRotation (wantedPosition);
  42. }
  43. }
  44. void RemoveImageAnchor(ARImageAnchor arImageAnchor)
  45. {
  46. Debug.Log ("image anchor removed");
  47. if (imageAnchorGO) {
  48. GameObject.Destroy (imageAnchorGO);
  49. }
  50. }
  51. void OnDestroy()
  52. {
  53. UnityARSessionNativeInterface.ARImageAnchorAddedEvent -= AddImageAnchor;
  54. UnityARSessionNativeInterface.ARImageAnchorUpdatedEvent -= UpdateImageAnchor;
  55. UnityARSessionNativeInterface.ARImageAnchorRemovedEvent -= RemoveImageAnchor;
  56. }
  57. // Update is called once per frame
  58. void Update () {
  59. }
  60. }