unity3d 사용자 정의 속성 보기

9734 단어
사용자 정의 속성 창, 즉 더 저렴한 도형 대속성을 만들 수 있지만 이 클래스는 Editor를 계승하여 Editor 폴더에 두어야 한다. 다음 코드는 다음과 같다.
SerializedObject는 SerializedProperty와 Editor가 귀속된 클래스를 연결하는 다리입니다
. 예를 들어 randomOnStart = script.FindProperty("randomOnStart");앞쪽은serializedProperty이고 뒤쪽은 클래스의 변수입니다. 이렇게 하면 연결할 수 있습니다!
OnInspector GUI 속성에 대한 콜백 방법은 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("
*If enabled, will generate a random board" + "
whenever 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
" + "layout board and has no effect on the game itself", GUILayout.Height(30)); EditorGUILayout.PropertyField(panelEditVisuals, true); } void drawPanel2(){ EditorGUILayout.LabelField("
*Properties below are the max amount of the panels " + "
generated during \"randomize!\"/\'Random on Start\"", GUILayout.Height(45)); EditorGUILayout.PropertyField(randomPanelLimit, true); } void drawPanel3(){ EditorGUILayout.LabelField("
*Click the buttons below to cycle through each panel type.." + "
NOTE: layout set below does not work when \"Random on Start\" is enabled~!" + "
NOTE 2: The numbers represents the strength of the panel (hits it take before destroyed)" + "
*** 0 means destroyed; 1 = takes one hit / Empty & Basic cannot be destroyed." + "
NOTE 3 : panel looks will follow the panel prefab on GameManager (towards the bottom)" + "
*** e.g. Rock strength 1 will use rock prefab array element 0; and so on... " + "
**** if strength > prefab array size, it will use the last element defined." + "
***** 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("
* Please select the object that contains the " + "\"GameManager\" script.
Then check back here again.", GUILayout.Height(45)); } }

좋은 웹페이지 즐겨찾기