Unity

Unity) Draw gizmo selection

Todah 2023. 10. 17. 21:22
반응형

Unity에서 Gizmo는 Scene View 관점에서 시각적 디버깅이나 설정등을 편하게 할 수 있도록 돕는다고 설명되어 있다.

그리고 Gizmo 관련 함수는  OnDrawGizmos 나 OnDrawGizmosSelected 함수 내에서만 사용할 수 있도록 설정되어 있다.

 

아래는 Sample 사용 Scipt다.

using UnityEngine;

public class gizmoTest : MonoBehaviour
{
    public float explosionRadius = 5.0f;

    void OnDrawGizmosSelected()
    {
        // Display the explosion radius when selected
        Gizmos.color = new Color(1, 1, 0, 0.75F);
        Gizmos.DrawSphere(transform.position, explosionRadius);
    }
}

 

아래는 적의 탐지 범위를 확인하기 위해 기즈모를 사용한 모습이다.

 

관련코드

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Enemy_AI : MonoBehaviour
{
    [SerializeField] Transform target;
    [SerializeField] float chaseRange = 5f;
    NavMeshAgent navMeshAgent;
    float distanceToTarget = Mathf.Infinity;
    bool isProvoked = false;

    private void Start() {
        navMeshAgent = GetComponent<NavMeshAgent>();
        
    }

    private void Update() {
        distanceToTarget = Vector3.Distance(target.position, this.transform.position);
        if(isProvoked){
            EngageTarget();
        }
        else if(distanceToTarget <= chaseRange){
            isProvoked = true;

            // navMeshAgent.SetDestination(target.position);
        }
    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, chaseRange);
    }
}

 

이외에도 Gizmos 에서 자주 사용되는 옵션은 다음과 같다.

정적 변수

color Sets the color for the gizmos that will be drawn next.
matrix Set the gizmo matrix used to draw all gizmos.

 

정적 함수

DrawCube Draw a solid box with center and size.
*DrawFrustum Draw a camera frustum using the currently set Gizmos.matrix for it's location and rotation.
DrawGUITexture Draw a texture in the scene.
DrawIcon Draw an icon at a position in the scene view.
DrawLine Draws a line starting at from towards to.
DrawMesh Draws a mesh.
DrawRay Draws a ray starting at from to from + direction.
DrawSphere Draws a solid sphere with center and radius.
DrawWireCube Draw a wireframe box with center and size.
DrawWireMesh Draws a wireframe mesh.
DrawWireSphere Draws a wireframe sphere with center and radius.

 

여기서 *DrawFrustum 함수를 사용하기 위해서는 뷰 절두체에 대한 이해가 필요하다.

 

빗자루 손잡이 또는 연필과 같은 곧은 막대를 들고 카메라를 향해 사진을 찍는다고 가정해보자. 막대가 카메라 렌즈에 수직 방향으로 놓인 것 처럼 사진의 중앙에 놓이게 되면 막대의 끝부분만 사진에 원형으로 나타날 것이고, 다른 부분은 전부 보이지 않게 된다. 여기서 막대를 위 방향으로 움직이면 막대의 아랫면이 점점 보이기 시작하겠지만, 막대를 위쪽으로 비스듬히 올리면 다시 가려지게 된다. 그리고, 막대를 계속 위로 올리면서 각도를 같이 올리면, 동그란 끝 점은 결국 사진의 맨 위 가장자리까지 올라가게 된다. 이 지점에서 막대를 따라 만들어지는 선보다 월드 공간 위에 있는 모든 오브젝트는 사진에 나타지 않게 된다.

쉽게 말해 Far clipping plane과 Near clipping plane 사이의 잘린 피라미드 모양을 만들어 내는 것이 뷰 절두체라는 것이다.

Camera의 Clipping Planes 조절을 통해 뷰 절두체의 크기를 조절할 수 있다.

 

#참조

 

https://docs.unity3d.com/2019.2/Documentation/ScriptReference/MonoBehaviour.OnDrawGizmosSelected.html

 

Unity - Scripting API: MonoBehaviour.OnDrawGizmosSelected()

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

https://learn.unity.com/tutorial/creating-custom-gizmos-for-development-2019-2

 

Creating Custom Gizmos for Development - 2019.2 - Unity Learn

In this tutorial, we’ll explore the many wonders of custom Gizmos. Most often experienced in the form of the scale, rotate, and translate tools in the Unity Editor, Gizmos are capable of much more, and are among the easiest, most versatile ways to custom

learn.unity.com

https://docs.unity3d.com/Manual/GizmosMenu.html

 

Unity - Manual: Gizmos menu

Scene view View Options toolbar Gizmos menu The Scene viewAn interactive view into the world you are creating. You use the Scene View to select and position scenery, characters, cameras, lights, and all other types of Game Object. More infoSee in Glossary

docs.unity3d.com

https://docs.unity3d.com/kr/530/ScriptReference/Gizmos.html

 

UnityEngine.Gizmos - Unity 스크립팅 API

Gizmos are used to give visual debugging or setup aids in the scene view.

docs.unity3d.com

https://docs.unity3d.com/kr/530/Manual/UnderstandingFrustum.html

 

뷰 절두체 이해 - Unity 매뉴얼

절두체 는 피라미드 같은 모양의 윗부분을 밑면에 병렬로 잘라낸 입체 형상을 가리킵니다. 이는 원근 카메라에 의해 보여지고 렌더링되는 영역의 형상입니다. 다음의 실험 예제를 통해 그 이유

docs.unity3d.com

 

반응형

'Unity' 카테고리의 다른 글

Unity) Animator  (2) 2023.10.20
Unity) RayCast  (0) 2023.10.18
Unity) Realm Rush Defense Game  (0) 2023.10.16
2023.02.28 Cloud Adventure  (0) 2023.05.29
Unity) SWSM(Summoners War Stat Maker) Ver 3.2  (0) 2022.12.25