using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.EventSystems; public class CameraAround : MonoBehaviour { [SerializeField] float _rotateAmountX = 5f; public float RotationAmountX { get { return _rotateAmountX; } set { _rotateAmountX = value; } } [SerializeField] float _rotateAmountY = 1f; public float RotationAmountY { get { return _rotateAmountY; } set { _rotateAmountY = value; } } [SerializeField] float _touchRotateAmountX = 32f; public float TouchRotationAmountX { get { return _touchRotateAmountX; } set { _touchRotateAmountX = value; } } [SerializeField] float _touchRotateAmountY = 15f; public float TouchRotationAmountY { get { return _touchRotateAmountY; } set { _touchRotateAmountY = value; } } public Texture2D cursorTextureAround; Vector2 _hotspot; bool isCurrentMouseIn; bool isPreviousMouseIn; //Wheel Button private Vector2 lastMousePosition; private Vector2 simulatedDeltaPosition; private bool isMiddleMouseButtonDown; private TouchPhase simulatedTouchPhase; //int _fingerID = -1; private Vector2 _touchDeltaMovement; private void Awake() { _hotspot = new Vector2(cursorTextureAround.width * 0.5f, cursorTextureAround.height * 0.5f); } private void Update() { if(_G.CAMSET == CamSettings.Focus || !SceneModeManager.CompareSceneMode(SceneModes.Navigate)) return; if(Input.touchCount > 0 ) { if (EventSystem.current.IsPointerOverGameObject()) return; if (Input.touchCount > 0 && EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) return; var touch = Input.GetTouch(0); Vector3 target = _G.CC; float deltaTime = Time.deltaTime; if (touch.phase == TouchPhase.Moved ) { // SceneModeManager.Instance.SetSceneMode("Fix 4"); _touchDeltaMovement = touch.deltaPosition; Vector2 delta = _touchDeltaMovement; delta.Scale(_touchDeltaMovement * new Vector2(_touchRotateAmountX * deltaTime, _touchRotateAmountY * deltaTime)); //SceneModeManager.Instance.SetSceneMode("Input X " + _touchDeltaMovement.x + "\nInput Y " + _touchDeltaMovement.y); OrbitCam(target, delta.y, delta.x); } } //Wheel Button------------------------------------------------------------------------------------------Wheel Button if (!SceneModeManager.CompareSceneMode(SceneModes.Calcule)) { WheelUpdate(); Hide(); } } void LateUpdate() { isCurrentMouseIn = MouseCheck.InOut(); if (SceneModeManager.CompareSceneMode(SceneModes.Navigate)/* || SceneModeManager.CompareSceneMode(SceneModes.ReadyMove) */&& _G.CAMSET != CamSettings.Ortho && _G.CAMSET != CamSettings.Plan2D) { OrbitCamera(); } else if (_G.CAMSET != CamSettings.Ortho && _G.CAMSET != CamSettings.Plan2D) { Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto); } isPreviousMouseIn = isCurrentMouseIn; } public void OrbitCamera() { if (_G.CAMSET == CamSettings.Focus) {/*Debug.Log("Not in focus");*/ return; } if (EventSystem.current.IsPointerOverGameObject()){ return;} if (Input.touchCount > 0 && EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) return; Vector3 target = _G.CC;//Vector3.zero; //this is the center of the scene, you can use any point here\ if (Input.touchCount > 0 ) { if (Input.GetTouch(0).phase == TouchPhase.Moved) { float maxSpeed = 10f; SceneModeManager.Instance.SetSceneMode("No Fix"); Vector2 touchDeltaPositionBase = Input.GetTouch(0).deltaPosition; Vector2 direction = touchDeltaPositionBase.normalized; float magnitude = Mathf.Min(touchDeltaPositionBase.magnitude, maxSpeed); direction.Scale(magnitude * new Vector2(_touchRotateAmountX , _touchRotateAmountY)); OrbitCam(target, direction.y, direction.x); } } else if (isPreviousMouseIn && isCurrentMouseIn &&( Input.GetMouseButton(0) /*|| Input.GetMouseButton(1)*/)) { float x = Input.GetAxis("Mouse X"); float y = Input.GetAxis("Mouse Y"); float x_rotate = x * _rotateAmountX; float y_rotate = y * _rotateAmountY; //Debug.Log(Input.GetAxis("Mouse Y")); OrbitCam(target, y_rotate, x_rotate); } else { //Debug.Log("No Input"); if(!isMiddleMouseButtonDown)Cursor.SetCursor(null, Vector2.zero, CursorMode.ForceSoftware); } } public void OrbitCam(Vector3 target, float y_rotate, float x_rotate) { Cursor.SetCursor(cursorTextureAround, _hotspot, CursorMode.ForceSoftware); Vector3 angles = transform.eulerAngles; angles.z = 0; transform.eulerAngles = angles; transform.RotateAround(target, transform.up, x_rotate); transform.RotateAround(target, -transform.right, y_rotate); transform.LookAt(target); _G.Camx = transform.position.x; _G.Camy = transform.position.y; _G.Camz = transform.position.z; _G.CamA = transform.eulerAngles.y; Mesure.OnUpdate(); } public void Hide() // TODO : optimize? { if (SceneModeManager.CompareSceneMode(SceneModes.Calcule)) return; Vector3 cameraPosition = Get.o1("MainCamera").transform.position; Vector3 objectPosition; foreach (Transform obj in SceneModeManager.Scene) { objectPosition = obj.position; if (!obj.TryGetComponent(out var moveObj)) continue; //if (moveObj.WaitForPlacement) continue; //print("Distance==co=="+cameraPosition +" vs "+objectPosition); bool isVisible = !_G.HIDELIST.Contains(obj.name); float sqrDistanceCameraToObject = (cameraPosition - objectPosition).sqrMagnitude; float sqrMinDistanceToCamera = _G._MinDistanceToCamera * _G._MinDistanceToCamera; //print("Distance=="+sqrDistanceCameraToObject +" vs "+sqrMinDistanceToCamera); bool isOnWall=true; if(obj.name.Length>4){ int ADN = Get.GetObjectIndex(obj.name); if(_G.OBJs[ADN][22]=="floor" || _G.OBJs[ADN][22]=="ceil")isOnWall=false; } if (isVisible && SceneModeManager.CompareSceneMode(SceneModes.Plan2D)) { SetObjectVisibility(obj.gameObject, true); } else if (!SceneModeManager.CompareSceneMode(SceneModes.Calcule) && sqrDistanceCameraToObject < sqrMinDistanceToCamera*_G.CameraCliping && isOnWall) { SetObjectVisibility(obj.gameObject, false); } else if (isVisible && sqrDistanceCameraToObject > sqrMinDistanceToCamera) { SetObjectVisibility(obj.gameObject, true); } } } public static void Show() { if (SceneModeManager.CompareSceneMode(SceneModes.Plan2D)) return; Transform scene = SceneModeManager.Scene; foreach (Transform obj in scene) { obj.gameObject.SetActive(true); if (obj.gameObject.GetComponent()) obj.gameObject.GetComponent().enabled = true; if (obj.gameObject.GetComponent()) obj.gameObject.GetComponent().enabled = true; } } public static void SetObjectVisibility(GameObject obj, bool TF) { if (obj.transform.GetChild(0).gameObject.activeSelf == TF) { return; } else { int layerDefault = 0; int layerIgnoreRaycast = 2; // object can't be clicked on obj.layer = TF ? layerDefault : layerIgnoreRaycast; } for (int i = 0; i < obj.transform.childCount; i++) { GameObject child = obj.transform.GetChild(i).gameObject; if (child.name == "Group") { continue; } child.SetActive(TF); GameObject OgeeSet=Get.o2("OgeeContainer","OgeeSet"+child.transform.parent.name); if(OgeeSet!=null){ OgeeSet.SetActive(TF); } } } public static bool ChecknotGhost(string name) { bool ng = true; if (_G.GHOSTLIST.Contains(name)) { ng = false; }; return ng; } public Vector2 GetSimulatedDeltaPosition() { return simulatedDeltaPosition; } public TouchPhase GetSimulatedTouchPhase() { return simulatedTouchPhase; } public void WheelUpdate(){ // Check if the middle mouse button is pressed if (Input.GetMouseButtonDown(2) && !isMiddleMouseButtonDown) { isMiddleMouseButtonDown = true; lastMousePosition = Input.mousePosition; // Initialize the last position simulatedDeltaPosition = Vector2.zero; // Reset delta position simulatedTouchPhase = TouchPhase.Began; // Simulate "Began" phase } // Check if the middle mouse button is held down if (Input.GetMouseButton(2) && isMiddleMouseButtonDown) { Vector3 target = _G.CC; Vector2 currentMousePosition = Input.mousePosition; simulatedDeltaPosition = currentMousePosition - lastMousePosition; Vector2 touchDeltaPositionBase = GetSimulatedDeltaPosition(); // If movement is detected, set phase to "Moved" if (simulatedDeltaPosition.sqrMagnitude > 0.01f) // Movement threshold { simulatedTouchPhase = TouchPhase.Moved; float maxSpeed = 10f; SceneModeManager.Instance.SetSceneMode("No Fix"); // Calculate direction and magnitude Vector2 direction = touchDeltaPositionBase.normalized; float magnitude = Mathf.Min(touchDeltaPositionBase.magnitude, maxSpeed); direction.Scale(magnitude * new Vector2(_touchRotateAmountX, _touchRotateAmountY)); // Perform the camera rotation OrbitCam(target, direction.y, direction.x); } else { //simulatedTouchPhase = TouchPhase.Moved; simulatedTouchPhase = TouchPhase.Stationary; } lastMousePosition = currentMousePosition; // Update the last position } // Check if the middle mouse button is released if (Input.GetMouseButtonUp(2)) { isMiddleMouseButtonDown = false; simulatedDeltaPosition = Vector2.zero; // Reset delta position simulatedTouchPhase = TouchPhase.Ended; // Simulate "Ended" phase } //Debug.Log($"Phase: {simulatedTouchPhase}"); } }