在unity腳本中控制Inspector面板的參數(shù)操作
在編寫腳本的過程中我們會遇到一些小問題
比如一個的變量 為了在其他腳本中可以調(diào)用 我們需要寫成public類型的
這樣的話在Inspector面板中會出現(xiàn)此變量 這篇博客會給大家介紹一些方法去避免這些小問題
1.[Header(" ")]
這個的作用是給它下面的所有變量一個總標題
2.[Tooltip("")]
這個的作用是給下面的第一行(緊鄰的語句)注釋
這個注釋和雙斜杠的注釋不同
這個注釋的效果是在unity中鼠標拖到變量的名字上 他會出現(xiàn)注釋(括號的雙引號中的注釋)
在這里我給大家一個例子 讓大家可以明白一點
[Header("GameObject")] [Tooltip("手槍")] private GameObject SmallGunPlayer; [Tooltip ("AK47"), SerializeField] private GameObject BiglGunPlayer;
它的效果是
然后把鼠標拖到變量上邊的時候是
大家對照腳本中的語句可以很輕易的理解了
3.[HideInInspector]
這個的用法是和在這篇博客開頭的差不多
就是我們在一個不想出現(xiàn)在Inspector面板上出現(xiàn)的public變量
我們就在這個語句上添加[HideInInspector]即可
在unity的Inspector面板上就不會出現(xiàn)了
因為這個比較好理解 就不給例子了
4.[SerializeField]
這個和上面的語句相反 這個是private的變量
添加上這個語句之后的話
在unity的Inspector面板上可以改變其值 相當于public變量
不過private 具有一定的保護級別 這是和直接用public的區(qū)別
在這里最后提一下,以上語句可以一起用 效果可以重疊
具體效果在上面給出的代碼和圖片中可以發(fā)現(xiàn)
5.[Range( , )]
這個的用法是寫在一個變量的前邊(上邊)
然后在unity的Inspector面板上就是一個類似于Slider的效果
[Range(2, 25)] public int maxRandomValue = 5;
6.[Serializable]
這個的作用是 我們在腳本中聲明一個類的時候
我們可以把它其中的變量改成一個可以展開 折疊的變量
可能上面的描述不太準確 下面給大家一個例子
public Mydate date; [Serializable] public class Mydate { public float q; public float w; public float m; public float r; }
它的效果是
很容易就看懂了吧
在上邊提到了類,在這里給一些小白朋友們介紹一下最簡單的類在unity中的用法
[Serializable ] public class prefebs { public GameObject Bullet; public GameObject Robot; public Transform Position; } public prefebs Prefebs; private void Update { Prefebs.Robot.transform.Translate(transform.up*Time.deltatime*10); }
在上邊很容易可以知道 在創(chuàng)建類之后在沒在update start等中調(diào)用
通過上邊介紹的所有方法可以制作出很多讓你意想不到的效果
并且可以讓你感覺這個真的好用
補充:Unity編輯器——Inspector 面板擴展
將EditorGUI 擴展在 Inspector 面板上
EditorGUI 和 GUI 的用法幾乎完全一致,目前來說前者多用于編輯器開發(fā),后者多用于發(fā)布后調(diào)試編輯器??傊?,它們都是起輔助作用的。 EditorGUI 提供的組件非常豐富,常用的繪制元素包括文本、按鈕、圖片和滾動框等。做一個好的編輯器,是離不開 EditorGUI 的。如圖:我們將 EditorGUI 拓展在 Inspector 面板上了,相關代碼如下:
using System.Collections; using System.Collections.Generic; using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif public class Script_03_23 : MonoBehaviour { public Vector3 scrollPos; public int myId; public string myName; public GameObject prefab; public MyEnum myEnum = MyEnum.One; public bool toogle1; public bool toogle2; public enum MyEnum { One=1, Two, } #if UNITY_EDITOR [CustomEditor(typeof(Script_03_23))] public class ScriptEditor_03_23 : Editor { private bool m_EnableToogle; public override void OnInspectorGUI() { //獲取腳本對象 Script_03_23 script = target as Script_03_23; //繪制滾動條 script.scrollPos = EditorGUILayout.BeginScrollView(script.scrollPos, false, true); script.myName = EditorGUILayout.TextField("text", script.myName); script.myId = EditorGUILayout.IntField("int", script.myId); script.prefab = EditorGUILayout.ObjectField("GameObject", script.prefab, typeof(GameObject), true) as GameObject; //繪制按鈕 EditorGUILayout.BeginHorizontal(); GUILayout.Button("1"); GUILayout.Button("2"); script.myEnum = (Script_03_23.MyEnum)EditorGUILayout.EnumPopup("MyEnum:", script.myEnum); EditorGUILayout.EndHorizontal(); //Toogle 組件 m_EnableToogle = EditorGUILayout.BeginToggleGroup("EnableToogle", m_EnableToogle); script.toogle1 = EditorGUILayout.Toggle("toogle1", script.toogle1); script.toogle2 = EditorGUILayout.Toggle("toogle2", script.toogle2); EditorGUILayout.EndToggleGroup(); EditorGUILayout.EndScrollView(); } } } #endif
EditorGUILayout —— 是EditorGUI 自動 布局版本。
EditorGUILayout.BeginScrollView —— 函數(shù)原型: public static Vector2 BeginScrollView( Vector2 scrollPosition, bool alwaysShowHorizontal, bool alwaysShowVertical, params GUILayoutOption[] options);
scrollPosition參數(shù): 顯式使用的位置
alwaysShowHorizontal —— 表示可選的參數(shù),始終顯示水平滾動條。如果為false或省略,那么則僅當ScrollView中的內(nèi)容比ScrollView本身更寬時才會顯示。
alwayShowVertical —— 表示可選的參數(shù),始終顯示垂直滾動條。如果為false或省略,那么只有當ScrollView中的內(nèi)容比ScrollView本身高時才會顯示。
EditorWindows 窗口
Unity 提供編輯器窗口,開發(fā)者可以自由拓展自己的窗口。 Unity 編輯器系統(tǒng)自帶的視圖窗口其實也是用 EditorWindows 實現(xiàn)的。如圖所示,我們來制作一個簡單的編輯窗口,它繪制元素時同樣使用 EditorGUI 代碼。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; public class Script_03_24Window : EditorWindow { [MenuItem("Window/Open My Window")] static void Init() { Script_03_24Window window = (Script_03_24Window)EditorWindow.GetWindow(typeof(Script_03_24Window)); window.Show(); } private Texture m_MyTexture = null; private float m_MyFloat = 0.5f; void Awake() { Debug.LogFormat("窗口初始化時調(diào)用"); m_MyTexture = AssetDatabase.LoadAssetAtPath<Texture>("Assets/unity1.png"); } void OnGUI() { GUILayout.Label("Hello World!!", EditorStyles.boldLabel); m_MyFloat = EditorGUILayout.Slider("Slider", m_MyFloat, -5, 5); GUI.DrawTexture(new Rect(0, 30, 100, 100), m_MyTexture); } void OnDestroy() { Debug.LogFormat("窗口銷毀時調(diào)用"); } void OnFocus() { Debug.LogFormat("窗口擁有焦點時調(diào)用"); } void OnHierarchyChange() { Debug.LogFormat("Hierarchy視圖發(fā)生改變時調(diào)用"); } void OnInspectorUpdate() { //Debug.LogFormat ("Inspector每幀更新"); } void OnLostFocus() { Debug.LogFormat("失去焦點"); } void OnProjectChange() { Debug.LogFormat("Project視圖發(fā)生改變時調(diào)用"); } void OnSelectionChange() { Debug.LogFormat("Hierarchy或者Project視圖中選擇一個對象時調(diào)用"); } void Update() { //Debug.LogFormat ("每幀更新"); } }
EditorWindows 下拉菜單
如圖 所示,在 EditorWindows 編輯窗口的右上角,有個下拉菜單,我們也可以對該菜單中的選項進行拓展,不過這里需要實現(xiàn) IHasCustomMenu 接口。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; public class Script_03_25Window : EditorWindow,IHasCustomMenu { void IHasCustomMenu.AddItemsToMenu(GenericMenu menu) { menu.AddDisabledItem(new GUIContent("Disable")); menu.AddItem(new GUIContent("Test1"), true, () => { Debug.Log("Test1"); }); menu.AddItem(new GUIContent("Test2"), true, () => { Debug.Log("Test2"); }); menu.AddSeparator("Test/"); menu.AddItem(new GUIContent("Test/Tes3"), true, () => { Debug.Log("Tes3"); }); } [MenuItem("Window/TAOTAO My Window")] static void Init() { Script_03_25Window window = (Script_03_25Window)EditorWindow.GetWindow(typeof(Script_03_25Window)); window.Show(); } }
上述代碼中,我們通過 AddItem()方法來添加列表元素,并且監(jiān)聽選擇后的事件。
預覽窗口
選擇游戲?qū)ο蠡蛘哂螒蛸Y源后, Inspector 面板下方將會出現(xiàn)它的預覽窗口,但是有些資源是沒有預覽信息的,不過我們可以監(jiān)聽它的窗口方法來重新繪制它,如圖 所示,相關代碼如下。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; [CustomPreview(typeof(GameObject))] public class Script_03_26 : ObjectPreview { public override bool HasPreviewGUI() { return true; } public override void OnPreviewGUI(Rect r, GUIStyle background) { GUI.DrawTexture(r, AssetDatabase.LoadAssetAtPath<Texture>("Assets/Unity.png")); GUILayout.Label("Hello World!"); } }
獲取預覽信息
有些資源是有預覽信息的,比如模型資源。在預覽窗口中,我們可以看到它的樣式。如果需要在自定義窗口中顯示它,就需要獲取它的預覽信息。如圖所示,選擇一個游戲?qū)ο蠛螅瑫谧远x窗口中顯示它,相關代碼如下:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; public class Script_03_27window : EditorWindow { private GameObject m_MyGo; private Editor m_MyEditor; [MenuItem("Window/TAOSHUI Open My Window")] static void Init() { Script_03_27window window = (Script_03_27window)EditorWindow.GetWindow(typeof(Script_03_27window)); window.Show(); } void OnGUI() { //設置一個游戲?qū)ο? m_MyGo = (GameObject)EditorGUILayout.ObjectField(m_MyGo, typeof(GameObject), true); if(m_MyGo !=null) { if(m_MyEditor==null) { //創(chuàng)建Editor 實例 m_MyEditor = Editor.CreateEditor(m_MyGo); } //預覽它 m_MyEditor.OnPreviewGUI(GUILayoutUtility.GetRect(500, 500), EditorStyles.whiteLabel ); } } }
在上述代碼中,預覽對象首先需要通過 Editor.CreateEditor()拿到它的 Editor 實例對象,接著調(diào)用 OnPreviewGUI()方法傳入窗口的顯示區(qū)域。
相關文章
C#編程實現(xiàn)發(fā)送郵件的方法(可添加附件)
這篇文章主要介紹了C#編程實現(xiàn)發(fā)送郵件的方法,具備添加附件的功能,涉及C#文件傳輸及郵件發(fā)送的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11利用Aspose.Word控件實現(xiàn)Word文檔的操作
偶然一次機會,一個項目的報表功能指定需要導出為Word文檔,因此尋找了很多篇文章,不過多數(shù)介紹的比較簡單一點,于是也參考了官方的幫助介紹,終于滿足了客戶的需求。下面我由淺入深來介紹這個控件在實際業(yè)務中的使用過程吧2013-05-05