unity3d 自定义属性视图

自定义属性窗口,也就是可以更方便宜的图形代属性,但这个类必须继承Editor 且放在Editor文件夹之中,以下代码为例

SerializedObject 是连接 SerializedProperty 和Editor 绑定的类的桥梁

.如randomOnStart = script.FindProperty("randomOnStart");前面是SerializedProperty 后面的是类中的一个变量,这样就可以连接起来了!

OnInspectorGUI 属性绘制的回调方法,相当于onGui


using UnityEditor;
using UnityEngine;
using System.Collections;


/// <summary> ##################################
/// 
/// NOTICE :
/// This script is the custom inspector for the boardLayout script.
/// 
/// DO NOT TOUCH UNLESS REQUIRED
/// 
/// </summary> ##################################
//指明要定义视图的脚本
[CustomEditor (typeof(BoardLayout))]
public class BoardGUISetup : Editor {
	
	//指向脚本
	SerializedObject script;
	BoardLayout bl;
	//列表属性
	SerializedProperty randomOnStart;
	
	SerializedProperty hidePanel1;
	SerializedProperty hidePanel2;
	SerializedProperty hidePanel3;
	SerializedProperty scrollPos;

	SerializedProperty panelArray;
	SerializedProperty pStrength;

	SerializedProperty randomPanelLimit;
	SerializedProperty randomPanelCount;

	SerializedProperty panelEditVisuals;

	public PanelDefinition[] scripts;
	
	public void initMe(){
		//target就是指向的那个脚本
		script = new SerializedObject(target);
		bl = ((BoardLayout) target);

		// scripts to use
		scripts = bl.gm.panelDefinitionObj.GetComponents<PanelDefinition>();

		// visual textures
		panelEditVisuals = script.FindProperty("panelEditVisuals");

		// random on start boolean
		randomOnStart = script.FindProperty("randomOnStart");

		// max panels during randomization
		randomPanelLimit = script.FindProperty("randomPanelLimit");
		randomPanelCount = script.FindProperty("randomPanelCount");
		
		// gui usage booleans
		hidePanel1 = script.FindProperty("hidePanel1");
		hidePanel2 = script.FindProperty("hidePanel2");
		hidePanel3 = script.FindProperty("hidePanel3");

		// board GUI setups
		panelArray = script.FindProperty("panelArray"); // for the button arrays
		pStrength = script.FindProperty("pStrength"); // for the strength fields
		scrollPos = script.FindProperty("scrollPos"); // for the scrollbar to conpensate for big boards
	}
	
	public override void OnInspectorGUI () {
		initMe(); // initialize the required serialized stuff
		script.Update();

		setRequiredValues(); // set the variables with the correct value - called after script.Update()
		drawLayoutTable(); // shows the custom tables
		//应用设置的更新
		script.ApplyModifiedProperties();
		EditorUtility.SetDirty(bl); // refresh the changes
	}


	void setRequiredValues(){

		if(scripts.Length > 0 ){
			bl.panelScripts = scripts;
			
			// auto adjust array sizes for all arrays according to the number of available scripts
			panelEditVisuals.arraySize = randomPanelLimit.arraySize = randomPanelCount.arraySize =  bl.panelScripts.Length;
			script.ApplyModifiedProperties();
			script.Update();
		} else {
			Debug.LogError("No panels found... go to PanelsManager and add your panels!");
		}
	}

	void drawLayoutTable()
    {
		if( GUILayout.Button( "Launch Window", GUILayout.Width(250) ) ){
			//得到一个新的窗口但必须继承于EditWindow
			EditorWindow.GetWindow (typeof (SetupWindow),false, "Board Setup");
		}
		
		if( GUILayout.Button( "Show/Hide Panel 1", GUILayout.Width(250) ) ){
			hidePanel1.boolValue = !hidePanel1.boolValue;
		}
		if(!hidePanel1.boolValue){
			drawPanel1();
		}
		
		if( GUILayout.Button( "Show/Hide Panel 2", GUILayout.Width(250) ) ){
			hidePanel2.boolValue = !hidePanel2.boolValue;
		}
		
		if(!hidePanel2.boolValue){
			drawPanel2();
		}
		
		if( GUILayout.Button( "Show/Hide NOTES", GUILayout.Width(250) ) ){
			hidePanel3.boolValue = !hidePanel3.boolValue;
		}
		
		if(!hidePanel3.boolValue){
			drawPanel3();
		}

		// custom inspector auto naming for ease of use.
		for(int x = 0; x < panelEditVisuals.arraySize;x++){
			bl.panelEditVisuals[x].name = scripts[x].GetType().Name + "'s";
			panelEditVisuals.GetArrayElementAtIndex(x).isExpanded = true;
		}
		for(int x = 0; x < randomPanelLimit.arraySize;x++){
			bl.randomPanelLimit[x].name = scripts[x].GetType().Name + "'s";
			randomPanelLimit.GetArrayElementAtIndex(x).isExpanded = true;
		}


		EditorGUILayout.LabelField("\n*If enabled, will generate a random board" +
			"\nwhenever a new game starts disregarding the below layout.", GUILayout.Height(45));
		EditorGUILayout.PropertyField(randomOnStart,GUILayout.Height(30));
		
		panelArray.arraySize = pStrength.arraySize = bl.gm.boardWidth * bl.gm.boardHeight;
		
		if(panelArray != null){
			GUILayoutOption[] scrollParams = {GUILayout.MinHeight(200), GUILayout.MaxHeight(888)};
			scrollPos.vector2Value = EditorGUILayout.BeginScrollView(scrollPos.vector2Value,scrollParams);
			
			int count = 0;
			GUILayoutOption[] layoutParams = {GUILayout.Width(55),GUILayout.Height(30)};
			
			for(int y = 0; y < bl.gm.boardHeight; y++){
			   //与GuiLayout用法类似
				EditorGUILayout.BeginHorizontal();

				for(int x = 0; x < bl.gm.boardWidth; x++){

					int num = panelArray.GetArrayElementAtIndex(count).intValue;

					if(bl.panelEditVisuals[num] != null && bl.panelEditVisuals[num].texture != null){ // assigned texture version
						if(GUILayout.Button( bl.panelEditVisuals[num].texture, layoutParams ) ){
							bl.togglePanel(count);
						}
					} else { // script name version
						if(GUILayout.Button( bl.panelScripts[num].GetType().Name.Substring(0,
							Mathf.Min (5,bl.panelScripts[num].name.Length)).ToString(), layoutParams ) ){
							bl.togglePanel(count);
						}
					}
					
					// the strength field
					pStrength.GetArrayElementAtIndex(count).intValue =
						EditorGUILayout.IntField(pStrength.GetArrayElementAtIndex(count).intValue,
						new GUILayoutOption[] {GUILayout.Width(20),GUILayout.Height(30)});
					
					count++;
				}
				EditorGUILayout.EndHorizontal();
			}
			EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space();
			EditorGUILayout.EndScrollView();
		}
		
		// the bottom buttons for easy board modifications
		EditorGUILayout.BeginHorizontal();
		GUILayoutOption[] layoutParams2 = {GUILayout.Width(80),GUILayout.Height(30)};
		
		if( GUILayout.Button( "Reset", layoutParams2 ) ){
			bl.resetMe();
		}
		
		if( GUILayout.Button( "Click All", layoutParams2 ) ){
			bl.clickAll();
		}
		if( GUILayout.Button( "Randomize!", layoutParams2 ) ){
			bl.randomize();
		}
		EditorGUILayout.EndHorizontal();
	}
	
	void drawPanel1(){
		EditorGUILayout.LabelField("These textures below is just for visuals on the\n" +
		                           "layout board and has no effect on the game itself", GUILayout.Height(30));
		EditorGUILayout.PropertyField(panelEditVisuals, true);
	}
	
	void drawPanel2(){
		EditorGUILayout.LabelField("\n*Properties below are the max amount of the panels " +
			"\ngenerated during \"randomize!\"/\'Random on Start\"", GUILayout.Height(45));
		EditorGUILayout.PropertyField(randomPanelLimit, true);
	}
	
	void drawPanel3(){
		EditorGUILayout.LabelField("\n*Click the buttons below to cycle through each panel type.." +
			"\nNOTE: layout set below does not work when \"Random on Start\" is enabled~!" +
			"\nNOTE 2: The numbers represents the strength of the panel (hits it take before destroyed)" +
			"\n   *** 0 means destroyed; 1 = takes one hit / Empty & Basic cannot be destroyed." +
			"\nNOTE 3 : panel looks will follow the panel prefab on GameManager (towards the bottom)" +
			"\n   *** e.g. Rock strength 1 will use rock prefab array element 0; and so on... " +
			"\n   **** if strength > prefab array size, it will use the last element defined." +
			"\n   ***** e.g., Rock strength 10; array size 5; will use element 4 for strength 10 until strength 5"
			, GUILayout.Height(120));
	}
}

以下代码为生成一个大的单独窗口,

EditorWindow.GetWindow 得到窗口,

 Editor editor = Editor.CreateEditor(layout);创建设一个编辑的面板!

using UnityEditor;
using UnityEngine;



/// <summary> ##################################
/// 
/// NOTICE :
/// This script is just an editor extension to call and open the board layout custom
/// inspector onto a gui window. This makes it easier to see the custom inspector as
/// it is naturally going to be quite big.
/// 
/// DO NOT TOUCH UNLESS REQUIRED
/// 
/// </summary> ##################################


public class SetupWindow : EditorWindow
{
	//在window工具栏里加入这个选项
    [MenuItem ("Window/Match Framework/Editor Window")]
    static void Init () 
    {
        // Get existing open window or if none, make a new one:
		EditorWindow.GetWindow (typeof (SetupWindow),false, "Board Setup");
    }

    void OnGUI () {

        GameObject sel = Selection.activeGameObject;
		
		if( sel != null){
			 BoardLayout layout = sel.GetComponent<BoardLayout>();

	        if (layout != null)
	        {
	            Editor editor = Editor.CreateEditor(layout);
	            editor.OnInspectorGUI();
	        } else { 
				showErrorMsg(); // tells user to select the GameManger object
			}
		} else {
			showErrorMsg(); // tells user to select the GameManger object
		}
    }
	
	void showErrorMsg(){
		EditorGUILayout.LabelField("\n* Please select the object that contains the " +
					"\"GameManager\" script.\nThen check back here again.", GUILayout.Height(45));
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值