当前位置:网站首页>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();
}
}
边栏推荐
- Win11 runs CMD to prompt the solution of "the requested operation needs to be promoted"
- The difference between MVVM and MVC
- 344. Reverse String. Sol
- Understand the basic concept of datastore in Android kotlin and why SharedPreferences should be stopped in Android
- ESP32 hosted
- APK加固技术的演变,APK加固技术和不足之处
- The code generator has deoptimised the styling of xx/typescript. js as it exceeds the max of 500kb
- EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?
- [agc009e] eternal average - conclusion, DP
- Promql demo service
猜你喜欢
a-tree 树的全部展开和收起
Practice: fabric user certificate revocation operation process
Win11 runs CMD to prompt the solution of "the requested operation needs to be promoted"
科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
Golang writes the opening chapter of selenium framework
90后测试员:“入职阿里,这一次,我决定不在跳槽了”
The simple problem of leetcode is to split a string into several groups of length K
Pl/sql basic syntax
Usage Summary of scriptable object in unity
EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?
随机推荐
Nacos installation and service registration
Oracle views the data size of a table
MySQL服务莫名宕机的解决方案
Promql demo service
Oracle hint understanding
What about data leakage? " Watson k'7 moves to eliminate security threats
Overriding equals() & hashCode() in sub classes … considering super fields
Summary of concurrency control
Golang writes the opening chapter of selenium framework
[groovy] mop meta object protocol and meta programming (execute groovy methods through metamethod invoke)
C language - structural basis
[groovy] groovy dynamic language features (automatic type inference of function arguments in groovy | precautions for function dynamic parameters)
Oracle is sorted by creation time. If the creation time is empty, the record is placed last
FBO and RBO disappeared in webgpu
Distributed resource management and task scheduling framework yarn
Character conversion PTA
[Chongqing Guangdong education] National Open University autumn 2018 0088-21t Insurance Introduction reference questions
Sub total of Pico development
Metaverse ape received $3.5 million in seed round financing from negentropy capital
U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程