기즈모 방법 요약
1_기즈모캡쳐
- Camera_Gizmo 에 Compass Gizmo Control를 넣는다
2 3DOBJ
-각 객체마다 BoxCollider를 가지고있고 Compass Mouse Event코드를 넣는다
-이쁘게 하기위한 Sprite_Base도 넣는다(Sprite Renderer)
3 Canvas
ort,per 나누기위한용
-Compass Ui Control 코드 넣기
Toggle은 각자 위 코드를 참조하여 모드를 변경한다.
기즈모를 화면에 안보이는 상단위에 위치시킨다 |
카메라의 빈부모 Pivot을 잡는다 |
기즈모를 보여줄 Base카메라를 하나 더 올린다 Compass Gizmo Control 코드를 추가한다. |
실제 기즈모 객체는 LocalPosition으로 적용될것임
가장 부모인 3dObjects 는 Compass_Gizmo의 자식으로 두고 0,0,0에위치하도록한다.
실제 기즈모 객체를 만든다_(트랜스폼 위치참조) |
실제 기즈모 객체를 만든다_(트랜스폼 위치참조) |
실제 기즈모 객체 바닥을 만든다(Sprite Renderer) |
기즈모카메라에 보일 캔버스를 하나 준비하여 토글 기능을 추가
Screen Space - Camera에 두고 Compass Ui Control 코드 추가 |
각 토글에는 알맞는 토글기능을 넣는다 |
각 토글에는 알맞는 토글기능을 넣는다 |
CompassGizmoControl
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
[RequireComponent(typeof(Camera))]
public class CompassGizmoControl : MonoBehaviour
{
//public CameraMovement cameraMovement;
public CameraMove cameraMovement;
[HideInInspector]
public bool IsGizmoClick = false;
private float ScreenWidth = 400;
private float ScreenHeight = 300;
public float xPos = 750;
public float yPos = 300f;
private Vector2 lastScreenSize;
private const float CAMERA_MOVE_TIME_DURATION = 0.5f;
private const float DEFAULT_CAMERA_PIVOT_DISTANT = 10F; 축을 어디로 둘지 정하는 변수
private Camera cam_Gizmo;
private Transform cam_Gizmo_Pivot;
private GameObject mainCameraTargetObject;
private Coroutine c;
private Rect viewportRect;
private float viewportX = 0.88f;
void Reset()
{
cam_Gizmo = gameObject.GetComponent<Camera>();
cam_Gizmo.gameObject.name = "Camera_Gizmo";
cam_Gizmo.clearFlags = CameraClearFlags.Depth;
cam_Gizmo.depth = 100;
cam_Gizmo.pixelRect = new Rect(Screen.width - xPos, Screen.height - yPos, ScreenWidth, ScreenHeight);
cam_Gizmo.fieldOfView = 25f;
cam_Gizmo.nearClipPlane = 0.01f;
cam_Gizmo.farClipPlane = 10f;
cam_Gizmo.transform.localPosition = transform.parent.transform.forward * -8f;//new Vector3(4f, 4f, 6f);
cam_Gizmo.transform.LookAt(transform.parent.transform);
}
void Start()
{
cam_Gizmo = gameObject.GetComponent<Camera>();
cam_Gizmo_Pivot = transform.parent.transform;
mainCameraTargetObject = new GameObject();
viewportRect = cam_Gizmo.rect;
}
void Update()
{
if (Camera.main != null)
{
cam_Gizmo_Pivot.localRotation = Camera.main.transform.rotation;
//ResizeScreenCheck();
}
}
private void ResizeScreenCheck()
{
//Vector2 screenSize = new Vector2(Screen.width, Screen.height);
//if (this.lastScreenSize != screenSize)
//{
// this.lastScreenSize = screenSize;
// cam_Gizmo.pixelRect = new Rect(Screen.width - xPos, Screen.height - yPos, ScreenWidth, ScreenHeight);
//}
}
/// called from CompassMouseEvent.cs
public void clickedGizmoObject(Transform clickedGizmoObject)
{
if (EventSystem.current.IsPointerOverGameObject())
return;
IsGizmoClick = true;
Camera.main.transform.parent = null;
Vector3 normalVector3 = Vector3.Normalize(clickedGizmoObject.localPosition);
Vector3 MainCameraPivot = findMainCameraPivot();
mainCameraTargetObject.transform.position = MainCameraPivot + (normalVector3 * Vector3.Distance(Camera.main.transform.position, MainCameraPivot));
mainCameraTargetObject.transform.LookAt(MainCameraPivot);
if (c != null)
{
StopCoroutine(c);
c = null;
}
c = StartCoroutine(moveCamera(mainCameraTargetObject.transform));
}
private IEnumerator moveCamera(Transform target_Point)
{
Vector3 destposition = target_Point.position;
Quaternion destrotation = target_Point.rotation;
Transform cam_transform = Camera.main.transform;
float time = 0f;
float duration_lerf = 0f;
while (cam_transform.position != destposition && cam_transform.rotation != destrotation)
{
time = Mathf.Clamp(time, 0, CAMERA_MOVE_TIME_DURATION);
duration_lerf = time / CAMERA_MOVE_TIME_DURATION;
cam_transform.position = Vector3.Lerp(cam_transform.position, destposition, duration_lerf);
cam_transform.rotation = Quaternion.Lerp(cam_transform.rotation, destrotation, duration_lerf);
yield return null;
time += Time.deltaTime;
cameraMovement.SetTarget();
}
cameraMovement.SetTarget();
IsGizmoClick = false;
}
private Vector3 findMainCameraPivot()
{
Vector3 v3 = Camera.main.transform.position + Camera.main.transform.forward * DEFAULT_CAMERA_PIVOT_DISTANT;// 레이 타겟이 없을때 카메라 전방 10m
Ray ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
RaycastHit hitpoint;
if (Physics.Raycast(ray, out hitpoint))
{
v3 = hitpoint.point;
}
return v3;
}
private float findMainCameraPivotDist()
{
Vector3 MainCameraPivot = findMainCameraPivot();
return (Vector3.Distance(MainCameraPivot, Camera.main.transform.position));
}
public void change_camera_OrthographicMode()
{
Camera.main.orthographic = true;
float camOrthographicSize = (Mathf.Tan(Camera.main.fieldOfView / 2f * Mathf.Deg2Rad) * findMainCameraPivotDist());
Camera.main.orthographicSize = camOrthographicSize;
}
public void change_camera_PerspectiveMode()
{
Camera.main.orthographic = false;
}
public void OnValueChangedEditToggle(bool value)
{
if (value)
{
cam_Gizmo.rect = viewportRect;
}
else
{
cam_Gizmo.rect = new Rect(viewportX, viewportRect.yMin, viewportRect.xMax, viewportRect.yMax);
}
}
}
Compass Mouse Event
using UnityEngine;
using UnityEngine.EventSystems;
[RequireComponent(typeof(BoxCollider))]
public class CompassMouseEvent : MonoBehaviour {
public CompassGizmoControl ref_CompassGizmoControl;
private Color orgColor;
private Color OverColor = Color.cyan;
private Material m_mat;
void Start () {
m_mat = GetComponent<MeshRenderer> ().material;
orgColor = m_mat.color;
}
void OnMouseEnter()
{
if (!EventSystem.current.IsPointerOverGameObject())
{
m_mat.color = OverColor;
}
}
void OnMouseExit()
{
m_mat.color = orgColor;
}
void OnMouseDown()
{
ref_CompassGizmoControl.clickedGizmoObject (this.transform);
}
}
Compass Ui Control
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CompassUiControl : MonoBehaviour {
public CompassGizmoControl ref_CompassGizmoControl;
public void _mainCamera_OrthographicMode(Toggle t)
{
if (t.isOn)
{
ref_CompassGizmoControl.change_camera_OrthographicMode ();
}
}
public void _mainCamera_PerspectiveMode(Toggle t)
{
if (t.isOn)
{
ref_CompassGizmoControl.change_camera_PerspectiveMode();
}
}
}
댓글 없음:
댓글 쓰기