当前位置:网站首页>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();
}
}
边栏推荐
- CA certificate trampled pit
- FBO and RBO disappeared in webgpu
- 二叉树(二)——堆的代码实现
- The introduction to go language is very simple: String
- Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
- New 3D particle function in QT 6.3
- Navigation day answer applet: preliminary competition of navigation knowledge competition
- a-tree 树的全部展开和收起
- Depth first DFS and breadth first BFS -- traversing adjacency tables
- [groovy] mop meta object protocol and meta programming (Introduction to groovyobject interface | introduction to metaclass | implementation of class methods using groovyobject invokemethod)
猜你喜欢
CA certificate trampled pit
What changes has Web3 brought to the Internet?
點到直線的距離直線的交點及夾角
Oracle triggers
鏈錶之雙指針(快慢指針,先後指針,首尾指針)
Metaverse ape received $3.5 million in seed round financing from negentropy capital
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
BFC block level formatting context
Oracle hint understanding
随机推荐
APK加固技术的演变,APK加固技术和不足之处
Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)
Common interview questions of JVM manufacturers
Web3为互联网带来了哪些改变?
Leetcode simple question ring and rod
Understand the basic concept of datastore in Android kotlin and why SharedPreferences should be stopped in Android
Leetcode simple question: find the nearest point with the same X or Y coordinate
[groovy] mop meta object protocol and meta programming (execute groovy methods through metamethod invoke)
Hcip day 16
Draw a red lantern with MATLAB
点到直线的距离直线的交点及夹角
Business introduction of Zhengda international futures company
How to develop and introduce applet plug-ins
Binary tree (II) -- code implementation of heap
Assign the output of a command to a variable [repeat] - assigning the output of a command to a variable [duplicate]
Alternating merging strings of leetcode simple questions
GWT module may need to be (RE) compiled reduce - GWT module may need to be (RE) compiled reduce
Character conversion PTA
C language - structural basis
Lesson 1: serpentine matrix