当前位置:网站首页>Unity Max and min constraint adjustment
Unity Max and min constraint adjustment
2022-07-05 22:29:00 【M.JH】
problem
Now we have a variable , We hope that we can freely adjust its minimum and maximum values on the editor panel
Through simple code writing, we can easily achieve this function , But we notice that if the minimum value is not careful to be greater than the maximum value during adjustment , Then the result will not meet our expectations , What is a good way to realize this function ?
resolvent
Use this part of the code to replace the original four variables
You can see that the following two drag bars are our new , The problem can be solved by dragging the slider to assign the maximum and minimum values , There will be no hidden danger that the minimum value is greater than the maximum value accidentally
To achieve this effect, we need 3 Script
using System;
[Serializable]
public struct RangedFloat
{
public float minValue;
public float maxValue;
}
using System;
public class MinMaxRangeAttribute : Attribute
{
public float Min
{
get;
private set;
}
public float Max
{
get;
private set;
}
public MinMaxRangeAttribute(float min,float max)
{
Min = min;
Max = max;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(RangedFloat),true)]
public class RangedFloatDrawer : PropertyDrawer
{
//base.OnGUI(position, property, label);
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
label = EditorGUI.BeginProperty(position, label, property);
position = EditorGUI.PrefixLabel(position,label);
SerializedProperty minProp = property.FindPropertyRelative("minValue");
SerializedProperty maxProp = property.FindPropertyRelative("maxValue");
float minValue = minProp.floatValue;
float maxValue = maxProp.floatValue;
float rangeMin = 0;
float rangeMax = 1;
var ranges = (MinMaxRangeAttribute[])fieldInfo.GetCustomAttributes(typeof(MinMaxRangeAttribute),true);
if (ranges.Length > 0)
{
rangeMin = ranges[0].Min;
rangeMax = ranges[0].Max;
}
const float rangeBoundsLabelWidth = 40f;
var rangeBoundsLabel1Rect = new Rect(position);
rangeBoundsLabel1Rect.width = rangeBoundsLabelWidth;
GUI.Label(rangeBoundsLabel1Rect,new GUIContent(minValue.ToString("F2")));
position.xMin += rangeBoundsLabelWidth;
var rangeBoundsLabel2Rect = new Rect(position);
rangeBoundsLabel2Rect.xMin = rangeBoundsLabel2Rect.xMax - rangeBoundsLabelWidth;
GUI.Label(rangeBoundsLabel2Rect,new GUIContent(maxValue.ToString("F2")));
position.xMax -= rangeBoundsLabelWidth;
EditorGUI.BeginChangeCheck();
EditorGUI.MinMaxSlider(position,ref minValue,ref maxValue,rangeMin,rangeMax);
if (EditorGUI.EndChangeCheck())
{
minProp.floatValue = minValue;
maxProp.floatValue = maxValue;
}
EditorGUI.EndProperty();
}
}
边栏推荐
- Some tutorials install the database on ubantu so as not to occupy computer memory?
- 70. Climbing Stairs. Sol
- Request preview display of binary data and Base64 format data
- Sub total of Pico development
- Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程
- Oracle hint understanding
- Sparse array [matrix]
- Distance entre les points et les lignes
- Business introduction of Zhengda international futures company
- Metaverse ape received $3.5 million in seed round financing from negentropy capital
猜你喜欢

Metaverse ape ape community was invited to attend the 2022 Guangdong Hong Kong Macao Great Bay metauniverse and Web3.0 theme summit to share the evolution of ape community civilization from technology

Navigation day answer applet: preliminary competition of navigation knowledge competition

50. Pow(x, n). O(logN) Sol

Common interview questions of redis factory

链表之双指针(快慢指针,先后指针,首尾指针)

A substring with a length of three and different characters in the leetcode simple question

Business introduction of Zhengda international futures company

What changes has Web3 brought to the Internet?

Alternating merging strings of leetcode simple questions

实战:fabric 用户证书吊销操作流程
随机推荐
The simple problem of leetcode is to split a string into several groups of length K
如何创建线程
2022软件测试工程师涨薪攻略,3年如何达到30K
thinkphp5.1跨域问题解决
Leetcode simple question: find the nearest point with the same X or Y coordinate
如何快速体验OneOS
Oracle triggers
点到直线的距离直线的交点及夹角
Matlab draws a cute fat doll
Overriding equals() & hashCode() in sub classes … considering super fields
南京:全面启用商品房买卖电子合同
Common interview questions of JVM manufacturers
了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
Postman核心功能解析-参数化和测试报告
MySQL服务莫名宕机的解决方案
Win11缺少dll文件怎么办?Win11系统找不到dll文件修复方法
點到直線的距離直線的交點及夾角
ESP32 hosted
a-tree 树的全部展开和收起




