Version: 2017.2
public static Rect PrefixLabel (Rect totalPosition, GUIContent label);
public static Rect PrefixLabel (Rect totalPosition, GUIContent label, GUIStyle style);
public static Rect PrefixLabel (Rect totalPosition, int id, GUIContent label);
public static Rect PrefixLabel (Rect totalPosition, int id, GUIContent label, GUIStyle style);

参数

totalPosition 屏幕上同时用于标签和控件的矩形。
id 控件的唯一 ID。如果未指定,则使用下一个控件的 ID。
label 显示在控件前的标签。
style 用于标签的样式。

返回

Rect 屏幕上仅适用于控件本身的矩形。

描述

创建一个显示在特定控件前的标签。

\ 编辑器窗口中的前缀标签。

请注意,大多数编辑器控件都已内置了可指定为其中一个参数的可选标签。如果没有可用的此类内置标签,或要从头开始创建自己的编辑器控件,则可以使用 PrefixLabel。

PrefixLabel 获取用于整个控件(包括标签)的矩形,并返回一个仅适用于控件本身(不包括标签)的矩形。

PrefixLabel 还可确保在单击标签时,关联的控件可获取键盘焦点(如果此控件支持键盘焦点)。关联的控件的 ID 可以选择性地进行指定;如果未提供 ID,标签会自动关联到其后的控件。

// Inflates a mesh
//
// Usage: Select a mesh and drag it to the object field.
// Press calculate and after finishing just press play and see your mesh growing.
//
// Note: To control the ratio of inflation just change the increaseRatio
// var in the "InflateMesh.js" sript

class InflateMeshEditor extends EditorWindow {

var object : MeshFilter;

@MenuItem("Examples/Inflate Mesh") static function Init () { var window = GetWindow (InflateMeshEditor); window.Show (); }

function OnGUI () { var rect = EditorGUILayout.GetControlRect (); rect = EditorGUI.PrefixLabel (rect, GUIContent ("Select a mesh")); object = EditorGUI.ObjectField (rect, "Calculate:", object, MeshFilter);

EditorGUI.BeginDisabledGroup (!object); if(GUI.Button (EditorGUILayout.GetControlRect (), "Calculate!")) Calculate (); EditorGUI.EndDisabledGroup (); }

function Calculate () { var finalNormals = new Vector3[0]; var mesh = object.sharedMesh; var vertices = mesh.vertices; var normals = mesh.normals;

// Find identical vertices // this will hold an ID for each vertex, vertices at // the same position will share the same ID! var vertexIDs = new int[vertices.length]; var counter : int = 0;

for (var i = 0; i < vertices.length; i++) { for (var j = 0; j < vertices.length; j++) { if (vertexIDs[i] == 0) { counter++; vertexIDs[i] = counter; } if (i != j) if (vertices[i] == vertices[j] &amp;&amp; vertices[i] != 0) vertexIDs[j] = vertexIDs[i]; } }

finalNormals = normals; calculated = 0.5; // Calcualte average normals // counter is the highest vertexID, now go through all the groups and collect normal data for (var k = 1; k <= counter; k++) { var curAvgNormal : Vector3 = Vector3.zero; for (var l = 0; l < vertexIDs.length; l++) if (vertexIDs[l] == k) { // Add up all the normals of the vertices with identical positions curAvgNormal += normals[l]; } curAvgNormal.Normalize(); //Normalize the result for (var m = 0; m < vertexIDs.length; m++) if (vertexIDs[m] == k) finalNormals[m] = curAvgNormal; } object.gameObject.AddComponent ("InflateMesh").fNormals = finalNormals; Debug.Log ("Done Adding Component, press play and see your mesh being inflated!"); } }

附加到编辑器脚本的脚本:

// InflateMesh.js
private var mesh : Mesh;
private var vertices = new Vector3[0];
private var normals = new Vector3[0];
var fNormals = new Vector3[0];
var increaseRatio = 0.005;
function Start () {
    mesh = GetComponent (MeshFilter).mesh;
    vertices = mesh.vertices;
    normals = mesh.normals;
}

function Update () { for (var i = 0; i < vertices.length; i++) { vertices[i] += fNormals[i] * Time.deltaTime * increaseRatio; } mesh.vertices = vertices; }