97 lines
3.0 KiB
C#
97 lines
3.0 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic; // WAJIB TAMBAH INI
|
|
|
|
public class ARInteractionManager : MonoBehaviour
|
|
{
|
|
[Header("Scripts References")]
|
|
public Manager gameManager;
|
|
// Sekarang menggunakan List agar bisa menampung banyak ImageTarget Buku
|
|
public List<BookPageManager> bookManagers;
|
|
|
|
[Header("Settings")]
|
|
public float zoomSpeed = 2.0f;
|
|
public float rotateSpeed = 150f;
|
|
public float minScale = 0.1f;
|
|
public float maxScale = 15.0f;
|
|
|
|
private bool isZoomingIn = false;
|
|
private bool isZoomingOut = false;
|
|
private bool isRotating = false;
|
|
|
|
void Update()
|
|
{
|
|
if (isZoomingIn) PerformZoom(true);
|
|
if (isZoomingOut) PerformZoom(false);
|
|
if (isRotating) PerformRotate();
|
|
}
|
|
|
|
public bool IsUserInteracting() => isRotating || isZoomingIn || isZoomingOut;
|
|
|
|
private GameObject GetTargetModel()
|
|
{
|
|
// 1. Cek SEMUA Buku: Melakukan perulangan pada List bookManagers
|
|
if (bookManagers != null)
|
|
{
|
|
foreach (var bManager in bookManagers)
|
|
{
|
|
// Cek apakah bManager ada dan apakah ImageTarget tersebut sedang terlihat di kamera
|
|
if (bManager != null && bManager.IsTracked())
|
|
{
|
|
// Ambil molekul yang sedang difokuskan pada halaman tersebut
|
|
GameObject focused = bManager.GetFocusedMolecule();
|
|
if (focused != null) return focused;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 2. Jika tidak ada buku yang terdeteksi, pindah ke sistem Kartu (Manager)
|
|
if (gameManager != null)
|
|
{
|
|
return gameManager.GetActiveModel();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
void PerformZoom(bool bigger)
|
|
{
|
|
GameObject obj = GetTargetModel();
|
|
if (obj == null) return;
|
|
|
|
float zoomFactor = 1.0f + (zoomSpeed * Time.deltaTime);
|
|
if (!bigger) zoomFactor = 1.0f / zoomFactor;
|
|
|
|
Vector3 nextScale = obj.transform.localScale * zoomFactor;
|
|
|
|
if (nextScale.x >= minScale && nextScale.x <= maxScale)
|
|
{
|
|
obj.transform.localScale = nextScale;
|
|
}
|
|
}
|
|
|
|
void PerformRotate()
|
|
{
|
|
GameObject obj = GetTargetModel();
|
|
if (obj == null) return;
|
|
obj.transform.Rotate(0, -rotateSpeed * Time.deltaTime, 0, Space.Self);
|
|
}
|
|
|
|
public void ToggleAnimasi()
|
|
{
|
|
GameObject obj = GetTargetModel();
|
|
if (obj == null) return;
|
|
|
|
RotateOrbit[] orbits = obj.GetComponentsInChildren<RotateOrbit>();
|
|
foreach (var orbit in orbits)
|
|
{
|
|
orbit.ToggleAnimation();
|
|
}
|
|
}
|
|
|
|
public void OnPressZoomIn() => isZoomingIn = true;
|
|
public void OnReleaseZoomIn() => isZoomingIn = false;
|
|
public void OnPressZoomOut() => isZoomingOut = true;
|
|
public void OnReleaseZoomOut() => isZoomingOut = false;
|
|
public void OnPressRotate() => isRotating = true;
|
|
public void OnReleaseRotate() => isRotating = false;
|
|
} |