ProceduralMaterial.GetProceduralPropertyDescriptions 获取程序材质属性描述数组

function GetProceduralPropertyDescriptions () : ProceduralPropertyDescription[]

Description描述

Get an array of descriptions of all the properties this Procedural Material has.

获取这个程序材质所有属性描述的一个数组。

This can be used to build generic GUI that can be used to edit the properties of any Procedural Material without knowing the properties of it in advance.

这可以用来构建泛型GUI,能用来编辑任意程序材质的属性,而事先不知道它的属性。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public Vector2 scrolling;
	void OnGUI() {
		if (renderer.sharedMaterial as ProceduralMaterial) {
			Rect windowRect = new Rect(Screen.width - 250, 30, 220, Screen.height - 60);
			GUI.Window(0, windowRect, ProceduralPropertiesGUI, "Procedural Properties");
		}
	}
	void ProceduralPropertiesGUI(int windowId) {
		scrolling = GUILayout.BeginScrollView(scrolling);
		ProceduralMaterial substance = renderer.sharedMaterial as ProceduralMaterial;
		ProceduralPropertyDescription[] inputs = substance.GetProceduralPropertyDescriptions();
		int i = 0;
		while (i < inputs.Length) {
			ProceduralPropertyDescription input = inputs[i];
			ProceduralPropertyType type = input.type;
			if (type == ProceduralPropertyType.Boolean) {
				bool inputBool = substance.GetProceduralBoolean(input.name);
				bool oldInputBool = inputBool;
				inputBool = GUILayout.Toggle(inputBool, input.name);
				if (inputBool != oldInputBool)
					substance.SetProceduralBoolean(input.name, inputBool);

			} else
			if (type == ProceduralPropertyType.Float)
				if (input.hasRange) {
					GUILayout.Label(input.name);
					float inputFloat = substance.GetProceduralFloat(input.name);
					float oldInputFloat = inputFloat;
					inputFloat = GUILayout.HorizontalSlider(inputFloat, input.minimum, input.maximum);
					if (inputFloat != oldInputFloat)
						substance.SetProceduralFloat(input.name, inputFloat);

				}
			else
			if (type == ProceduralPropertyType.Vector2 || type == ProceduralPropertyType.Vector3 || type == ProceduralPropertyType.Vector4)
				if (input.hasRange) {
					GUILayout.Label(input.name);
					int vectorComponentAmount = 4;
					if (type == ProceduralPropertyType.Vector2)
						vectorComponentAmount = 2;

					if (type == ProceduralPropertyType.Vector3)
						vectorComponentAmount = 3;

					Vector4 inputVector = substance.GetProceduralVector(input.name);
					Vector4 oldInputVector = inputVector;
					int c = 0;
					while (c < vectorComponentAmount) {
						inputVector[c] = GUILayout.HorizontalSlider(inputVector[c], input.minimum, input.maximum);
						c++;
					}
					if (inputVector != oldInputVector)
						substance.SetProceduralVector(input.name, inputVector);

				}
			else
			if (type == ProceduralPropertyType.Color3 || type == ProceduralPropertyType.Color4) {
				GUILayout.Label(input.name);
				int colorComponentAmount = ((type == ProceduralPropertyType.Color3) ? 3 : 4);
				Color colorInput = substance.GetProceduralColor(input.name);
				Color oldColorInput = colorInput;
				int d = 0;
				while (d < colorComponentAmount) {
					colorInput[d] = GUILayout.HorizontalSlider(colorInput[d], 0, 1);
					d++;
				}
				if (colorInput != oldColorInput)
					substance.SetProceduralColor(input.name, colorInput);

			} else
			if (type == ProceduralPropertyType.Enum) {
				GUILayout.Label(input.name);
				int enumInput = substance.GetProceduralEnum(input.name);
				int oldEnumInput = enumInput;
				string[] enumOptions = input.enumOptions;
				enumInput = GUILayout.SelectionGrid(enumInput, enumOptions, 1);
				if (enumInput != oldEnumInput)
					substance.SetProceduralEnum(input.name, enumInput);

			}
			i++;
		}
		substance.RebuildTextures();
		GUILayout.EndScrollView();
	}
}
var scrolling : Vector2;

function OnGUI () {
	if (renderer.sharedMaterial as ProceduralMaterial) {
		// Show substance properties inside window
		//在窗口中显示substance属性
		var windowRect = new Rect (Screen.width-250, 30, 220, Screen.height-60);
		GUI.Window (0, windowRect, ProceduralPropertiesGUI, "Procedural Properties");
	}
}

function ProceduralPropertiesGUI (windowId : int) {
	scrolling = GUILayout.BeginScrollView (scrolling);

	var substance = renderer.sharedMaterial as ProceduralMaterial;

	// Get a list of all the Procedural properties
	//获取所有程序属性的列表
	var inputs = substance.GetProceduralPropertyDescriptions ();
	// Iterate through all the properties
	//遍历所有属性
	for (var i : int = 0; i < inputs.Length; i++) {
		var input = inputs[i];

		// Handle the different types of properties
		//处理属性的不同类型
		var type = input.type;

		// Show a boolean as a toggle
		//显示一个布尔,作为开关
		if (type == ProceduralPropertyType.Boolean) {
			var inputBool = substance.GetProceduralBoolean (input.name);
			var oldInputBool = inputBool;
			inputBool = GUILayout.Toggle (inputBool, input.name);
			if (inputBool != oldInputBool)
				substance.SetProceduralBoolean (input.name, inputBool);
		}
		// Show a float as a slider
		//显示一个浮点数,作为滑动条
		else if (type == ProceduralPropertyType.Float) {
			// Only show GUI for numbers with a defined range
			//只显示带有定义范围数字的GUI
			if (input.hasRange) {
				GUILayout.Label (input.name);

				var inputFloat = substance.GetProceduralFloat (input.name);
				var oldInputFloat = inputFloat;

				inputFloat = GUILayout.HorizontalSlider (inputFloat, input.minimum, input.maximum);
				if (inputFloat != oldInputFloat)
					substance.SetProceduralFloat (input.name, inputFloat);
			}
		}
		// Show a vector as multiple sliders
		//显示一个向量,作为多重滑动条
		else if (type == ProceduralPropertyType.Vector2 ||
			type == ProceduralPropertyType.Vector3 ||
			type == ProceduralPropertyType.Vector4
		) {
			// Only show GUI for numbers with a defined range
			//只显示带有定义范围数字的GUI
			if (input.hasRange) {
				GUILayout.Label (input.name);

				// How many components are in this vector?
				//在这个向量有多少组件
				var vectorComponentAmount = 4;
				if (type == ProceduralPropertyType.Vector2) vectorComponentAmount = 2;
					if (type == ProceduralPropertyType.Vector3) vectorComponentAmount = 3;

					var inputVector = substance.GetProceduralVector (input.name);
					var oldInputVector = inputVector;

				// Loop through the vector component and show a slider for each
				//循环向量组件并显示每一个滑动条
				for (var c : int = 0; c < vectorComponentAmount; c++)
					inputVector[c] = GUILayout.HorizontalSlider (
					inputVector[c], input.minimum, input.maximum);


				if (inputVector != oldInputVector)
					substance.SetProceduralVector (input.name, inputVector);
			}
		}
		// Show a color as multiple sliders
		//显示一个颜色,作为多重滑动条
		else if (type == ProceduralPropertyType.Color3 || type == ProceduralPropertyType.Color4) {
			GUILayout.Label (input.name);

			// How many numbers are in this color?
			//有多少在这个颜色?
			var colorComponentAmount = (type == ProceduralPropertyType.Color3 ? 3 : 4);

			var colorInput = substance.GetProceduralColor (input.name);
			var oldColorInput = colorInput;

			// Loop through the vector numbers and show a slider for each
			//循环向量数并显示每个滑动条
			for (var d : int = 0; d < colorComponentAmount; d++)
				colorInput[d] = GUILayout.HorizontalSlider (colorInput[d], 0, 1);

			if (colorInput != oldColorInput)
				substance.SetProceduralColor (input.name, colorInput);
		}

		// Show an enum as a selection grid
		//显示枚举,作为一个选择表格
		else if (type == ProceduralPropertyType.Enum) {
			GUILayout.Label (input.name);

			var enumInput = substance.GetProceduralEnum (input.name);
			var oldEnumInput = enumInput;
			var enumOptions = input.enumOptions;

			enumInput = GUILayout.SelectionGrid (enumInput, enumOptions, 1);
			if (enumInput != oldEnumInput)
				substance.SetProceduralEnum (input.name, enumInput);
		}
	}
	// Rebuild the textures to show the changes
	//重建纹理,来更新改变
	substance.RebuildTextures();

	GUILayout.EndScrollView();
}
最后修改:2011年9月13日 Tuesday 10:25

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。