value | 要限制在最小值和最大值定义的范围内的浮点值。 |
min | 要比较的最小浮点值。 |
max | 要比较的最大浮点值。 |
float 最小值和最大值之间的浮点结果。
将给定值限制在给定的最小浮点值和最大浮点值之间。如果给定值在最小值和最大值范围内,则返回该值。
如果给定的浮点值小于最小值,则返回最小值。如果给定值大于最大值,则返回最大值。使用 Clamp 将值限制在由最小值和最大值定义的范围内。
如果最小值大于最大值,则返回未定义的值。
using UnityEngine;
// Mathf.Clamp example. // // Animate a cube along the x-axis using a sine wave. // Let the minimum and maximum positions on the x-axis // be changed. The cube will be visible inside the // minimum and maximum values.
public class ExampleScript : MonoBehaviour { private float xMin = -0.5f, xMax = 0.5f; private float timeValue = 0.0f;
void Update() { // Compute the sin position. float xValue = Mathf.Sin(timeValue * 5.0f);
// Now compute the Clamp value. float xPos = Mathf.Clamp(xValue, xMin, xMax);
// Update the position of the cube. transform.position = new Vector3(xPos, 0.0f, 0.0f);
// Increase animation time. timeValue = timeValue + Time.deltaTime;
// Reset the animation time if it is greater than the planned time. if (xValue > Mathf.PI * 2.0f) { timeValue = 0.0f; } }
void OnGUI() { // Let the minimum and maximum values be changed xMin = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), xMin, -1.0f, +1.0f); xMax = GUI.HorizontalSlider(new Rect(25, 60, 100, 30), xMax, -1.0f, +1.0f);
// xMin is kept less-than or equal to xMax. if (xMin > xMax) { xMin = xMax; }
// Display the xMin and xMax value with better size labels. GUIStyle fontSize = new GUIStyle(GUI.skin.GetStyle("label")); fontSize.fontSize = 24;
GUI.Label(new Rect(135, 10, 150, 30), "xMin: " + xMin.ToString("f2"), fontSize); GUI.Label(new Rect(135, 45, 150, 30), "xMax: " + xMax.ToString("f2"), fontSize); } }
value | 要限制在最小值到最大值范围内的整数值。 |
min | 要比较的最小整数值。 |
max | 要比较的最大整数值。 |
int 最小值和最大值之间的整数值结果。
将给定值限制在由给定的最小整数值和最大整数值定义的范围内。如果给定值在最小值和最大值范围内,则返回该值。
如果给定值小于最小值,则返回最小值。如果给定值大于最大值,则返回最大值。最小值和最大值参数都是包含的。例如,Clamp(10, 0, 5) 将返回最大参数 5 而不是 4。
using UnityEngine;
// Mathf.Clamp integer example. // // Add or subtract values from health. // Keep health between 1 and 100. Start at 17.
public class ExampleScript : MonoBehaviour { public int health = 17; private int[] healthUp = new int[] {25, 10, 5, 1}; private int[] healthDown = new int[] {-10, -5, -2, -1};
// Width and height for the buttons. private int xButton = 75; private int yButton = 50;
// Place of the top left button. private int xPos1 = 50, yPos1 = 100; private int xPos2 = 125, yPos2 = 100;
void OnGUI() { GUI.skin.label.fontSize = 20; GUI.skin.button.fontSize = 20;
// Generate and show positive buttons. for (int i = 0; i < healthUp.Length; i++) { if (GUI.Button(new Rect(xPos1, yPos1 + i * yButton, xButton, yButton), healthUp[i].ToString())) { health += healthUp[i]; } }
// Generate and show negative buttons. for (int i = 0; i < healthDown.Length; i++) { if (GUI.Button(new Rect(xPos2, yPos2 + i * yButton, xButton, yButton), healthDown[i].ToString())) { health += healthDown[i]; } }
// Show health between 1 and 100. health = Mathf.Clamp(health, 1, 100); GUI.Label(new Rect(xPos1, xPos1, 2 * xButton, yButton), "Health: " + health.ToString("D3")); } }