Version: 2020.3
public static float Clamp (float value, float min, float max);

参数

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 = -1.0f, xMax = 1.0f; 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); } }

public static int Clamp (int value, int min, int max);

参数

value 要限制在最小到最大范围内的整数点值
min 要比较的最小整数点值。
max 要比较的最大整数点值。

返回

int 最小值到最大值之间的整数结果。

描述

将给定值钳制在给定最小整数和最大整数值定义的范围之间。如果在最小和最大范围内,则返回给定值。

如果给定值小于最小值,则返回最小值。如果给定值大于最大值,则返回最大值。min 和 max 参数包括在内。例如,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")); } }