ShovelingLife
A Game Programmer
ShovelingLife
전체 방문자
오늘
어제
  • 분류 전체보기 (1072)
    • 그래픽스 (57)
      • 공통 (19)
      • 수학 물리 (22)
      • OpenGL & Vulkan (1)
      • DirectX (14)
    • 게임엔진 (183)
      • Unreal (69)
      • Unity (103)
      • Cocos2D-X (3)
      • 개인 플젝 (8)
    • 코딩테스트 (221)
      • 공통 (7)
      • 프로그래머스 (22)
      • 백준 (162)
      • LeetCode (19)
      • HackerRank (2)
      • 코딩테스트 알고리즘 (8)
    • CS (235)
      • 공통 (21)
      • 네트워크 (44)
      • OS & 하드웨어 (55)
      • 자료구조 & 알고리즘 (98)
      • 디자인패턴 (6)
      • UML (4)
      • 데이터베이스 (7)
    • 프로그래밍 언어 (348)
      • C++ (167)
      • C# (90)
      • Java (9)
      • Python (33)
      • SQL (30)
      • JavaScript (8)
      • React (7)
    • 그 외 (9)
      • Math (5)
      • 일상 (5)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • Source Code 좌측 상단에 복사 버튼 추가 완료
  • 언리얼 엔진 C++ 빌드시간 단축 꿀팁
  • 게임 업계 코딩테스트 관련
  • 1인칭 시점으로 써내려가는 글들

인기 글

태그

  • 파이썬
  • 그래픽스
  • 문자열
  • 유니티
  • 프로그래머스
  • 알고리즘
  • C
  • C++
  • Unity
  • 함수
  • string
  • 언리얼
  • 클래스
  • 배열
  • 포인터
  • SQL
  • 티스토리챌린지
  • 오블완
  • 백준
  • c#

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ShovelingLife

A Game Programmer

게임엔진/Unity

[Unity] Range 어트리뷰트를 단위로 설정할 수 있는 방법

2023. 2. 17. 11:39

아래는 어트리뷰트를 그려줄 클래스

using UnityEngine;
using UnityEditor;
using System;

[CustomPropertyDrawer(typeof(RangeExAttribute))]
internal sealed class RangeExDrawer : PropertyDrawer
{
    private int value;

    /**
     * Return exact precision of reel decimal
     * ex :
     * 0.01     = 2 digits
     * 0.02001  = 5 digits
     * 0.02000  = 2 digits
     */
    private int Precision(float value)
    {
        int _precision;
        if (value == .0f) return 0;
        _precision = value.ToString().Length - (((int)value).ToString().Length + 1);
        // Math.Round function get only precision between 0 to 15
        return Mathf.Clamp(_precision, 0, 15);
    }

    /**
     * Return new float value with step calcul (and step decimal precision)
     */
    private float Step(float value, float min, float step)
    {
        if (step == 0) return value;
        float newValue = min + Mathf.Round((value - min) / step) * step;
        return (float)Math.Round(newValue, Precision(step));
    }

    /**
     * Return new integer value with step calcul
     * (It's more simple ^^)
     */
    private int Step(int value, float step)
    {
        if (step == 0) return value;
        value -= (value % (int)Math.Round(step));
        return value;
    }

    //
    // Methods
    //
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        var rangeAttribute = (RangeExAttribute)base.attribute;

        if (rangeAttribute.label != "")
            label.text = rangeAttribute.label;

        switch (property.propertyType)
        {
            case SerializedPropertyType.Float:
                float _floatValue = EditorGUI.Slider(position, label, property.floatValue, rangeAttribute.min, rangeAttribute.max);
                property.floatValue = Step(_floatValue, rangeAttribute.min, rangeAttribute.step);
                break;
            case SerializedPropertyType.Integer:
                int _intValue = EditorGUI.IntSlider(position, label, property.intValue, (int)rangeAttribute.min, (int)rangeAttribute.max);
                property.intValue = Step(_intValue, rangeAttribute.step);
                break;
            default:
                EditorGUI.LabelField(position, label.text, "Use Range with float or int.");
                break;
        }
    }
}

아래는 어트리뷰트에 설정될 값

using System;
using UnityEngine;

// 범위 기반 어트리뷰트 [Range(최저, 최대)] 에서 n값에 나눠서 가능하게끔 함 / 예) 1~10에서 2단위로 설정한다. 2,4,6,8,10
// 사용 예시 [RangeEx(-10f, 10f, 0.5f, "간격이 0.5인 float 어트리뷰트")]
[AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
public sealed class RangeExAttribute : PropertyAttribute
{
    public readonly float min;
    public readonly float max;
    public readonly float step;
    public readonly string label = "";

    public RangeExAttribute(float min, float max, float step, string label)
    {
        this.min = min;
        this.max = max;
        this.step = step;
        this.label = label;
    }
}
저작자표시 (새창열림)

'게임엔진 > Unity' 카테고리의 다른 글

[Unity] SQL 데이터 베이스 연동 SQLite  (0) 2023.03.23
[Unity] 에디터에서만 사용할 수 있는 커스텀 코루틴  (0) 2023.02.17
[Unity] 화면 위치 > 월드 좌표 치환 RectTransformUtility  (0) 2023.02.05
[Unity] UI 카메라 설정하기 (screen space - camera)  (0) 2023.01.15
[Unity] Job 시스템 이해 2, NativeContainer  (0) 2023.01.15
    '게임엔진/Unity' 카테고리의 다른 글
    • [Unity] SQL 데이터 베이스 연동 SQLite
    • [Unity] 에디터에서만 사용할 수 있는 커스텀 코루틴
    • [Unity] 화면 위치 > 월드 좌표 치환 RectTransformUtility
    • [Unity] UI 카메라 설정하기 (screen space - camera)
    ShovelingLife
    ShovelingLife
    Main skill stack => Unity C# / Unreal C++ Studying Front / BackEnd, Java Python

    티스토리툴바