国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

unity腳本基礎(chǔ)+編輯器UnityEditor學(xué)習(xí)

這篇具有很好參考價值的文章主要介紹了unity腳本基礎(chǔ)+編輯器UnityEditor學(xué)習(xí)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

一,MonoBehaviour類及其生命周期

1.1創(chuàng)建腳本

新建腳本文件后,文件內(nèi)會有一段默認(rèn)代碼:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

它源自基類MonoBehaviour,以確保此腳本將在游戲循環(huán)中運(yùn)行,并具有對某些事件作出反應(yīng)的附加功能。

1.2生命周期

  • 當(dāng)腳本運(yùn)行時,unity會按照預(yù)定順序依次調(diào)用以下方法,完成生命周期:
    [所有腳本的順序是并行的]

unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎
unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎
unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎

  • 初始化:
    Awake():僅執(zhí)行一次,游戲物體創(chuàng)建時調(diào)用一次(不管腳本激沒激活),常用于初始化
    OnEnable():可調(diào)用多次,當(dāng)組件設(shè)置enable = true、游戲物體設(shè)置active = true或腳本激活第一次run時調(diào)用
    Start():僅執(zhí)行一次,在腳本被激活并完成初始化時調(diào)用一次(在第一次Update之前),與組件相關(guān)
  • 更新:
    FixedUpdate():固定時間更新,默認(rèn)50fps,默認(rèn)時間通過Edit->Project Settings->Time面板中的Fixed Timestep設(shè)置(與物理引擎相關(guān)的更新在這里)
    OnTrigger……():觸發(fā),與碰撞互斥,Collider或Rigidbody接觸時調(diào)用「Enter表示接觸瞬間執(zhí)行一次;Stay表示物體一直接觸持續(xù)執(zhí)行;Exit表示物體分開瞬間執(zhí)行一次。」
    OnCollision……():物理碰撞,與觸發(fā)互斥,Collider或Rigidbody接觸時調(diào)用
    Update():當(dāng)游戲運(yùn)行且腳本啟用時,每幀觸發(fā)一次(時間不固定,與物理引擎無關(guān)的更新在這里)[由于不同設(shè)備幀率不同,比如人物移動等需乘上Time.deltaTime----每幀的時間增量]
    LateUpdate():每幀執(zhí)行,在其他更新執(zhí)行完之后執(zhí)行。(一般用于命令腳本執(zhí)行,比如物體移動位置之后的相機(jī)跟隨)
  • 渲染:
    OnPreCull(): 在相機(jī)剔除cull場景之前調(diào)用。剔除過程決定相機(jī)的可見對象。
    OnBecameVisible/OnBecameInvisible(): 在對象對于相機(jī)可見/不可見時調(diào)用。
    OnWillRenderObject(): 如果對象可見,則為每個相機(jī)調(diào)用一次。
    OnPreRender(): 在相機(jī)開始渲染場景之前調(diào)用。
    OnRenderObject():在完成所有常規(guī)場景渲染后調(diào)用此函數(shù)。此時,可使用 GL 類或 Graphics.DrawMeshNow 繪制自定義幾何圖形。
    OnPostRender(): 在相機(jī)完成場景渲染后調(diào)用此函數(shù)。
    OnRenderImage(): 在完成場景渲染后調(diào)用此函數(shù),以便對屏幕圖像進(jìn)行后處理。
    OnGUI(): 響應(yīng) GUI 事件,可多次調(diào)用。程序首先將處理 Layout 和 Repaint 事件,然后再處理每個輸入事件的 Layout 和 keyboard/鼠標(biāo)事件。
    OnDrawGizmos(): 場景可視化繪制。
  • 結(jié)束:
    OnApplicationQuit():應(yīng)用程序退出時調(diào)用
    OnDisable():OnApplicationQuit調(diào)用時同時調(diào)用,并且行為被禁用enable = false 、對象被銷毀active = false時(由激活轉(zhuǎn)向禁用,一直是禁用不管)調(diào)用,加載完后調(diào)用OnEnable
    OnDestroy():OnApplicationQuit調(diào)用時同時調(diào)用,并且在Destroy游戲物體時被調(diào)用

1.3MonoBehaviour類與普通類的區(qū)別

  1. MonoBehaviour類不用創(chuàng)建實(shí)例或new,unity會自動完成。但可以通過添加組件的方式替代。
  2. 只有繼承了MonoBehaviour才能用Invoke,Coroutine,print以及生命周期函數(shù)等
  3. 只有繼承了MonoBehaviour才能在面板上看到,掛在物體上

二,拓展菜單和面板

  • 小tips:Unity目錄下的Editor文件夾是專門用來進(jìn)行編輯器拓展的,盡在編輯模式下有效;可以放在Assets下,也可以放在任意目錄下;這個文件夾的內(nèi)容打包是會被自動忽略。

2.1 Inspector面板參數(shù)

Inspector面板參數(shù)與shader類似:

  1. [Header(" ")]----總標(biāo)題
  2. [Tooltip(" ")]----給緊挨著的下一行注釋,在unity里鼠標(biāo)放在下一行的變量上時出現(xiàn)該注釋
  3. [HideInInspector]----不想在面板上出現(xiàn)的public變量
  4. [SerializeField]----可以在面板上出現(xiàn)的private變量
  5. [Range( , )]----表示下一行變量的滑竿范圍
  6. [Serializable]----表示聲明的類或結(jié)構(gòu)體在unity面板上以折疊的方式展示
  7. [System.Serializable]----序列化,unity里自定義數(shù)據(jù)類型無法顯示在inspectior面板里,需要加上[System.Serializable]使其序列化以顯示自定義數(shù)據(jù)或結(jié)構(gòu)體

2.2 頂部菜單欄

[MenuItem("Tools/菜單選項(xiàng)")]
public static void ToolsTest()
{
	//設(shè)置菜單選中狀態(tài):
    string path = "Tools/菜單選項(xiàng)"; //菜單路徑
    bool beChosen =  Menu.GetChecked(path); //獲取菜單的選中狀態(tài)
    Menu.SetChecked(path, !beChosen); //設(shè)置新的選中狀態(tài)
}

通過AddItem()添加列表元素,效果如下圖
unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎

  • MenuItem屬性
//itemName:菜單項(xiàng)的名稱
//isValidateFunction:表示是否為驗(yàn)證函數(shù)
//priority:菜單項(xiàng)的排序權(quán)重,越大越下邊
public MenuItem(string itemName, bool isValidateFunction=false, int priority=1000);

2.3 拓展Assets右鍵菜單

[MenuItem("Assets/Test", false, 1)]
static void Test()

2.4 拓展Inspector組件右鍵菜單

//CONTEXT路徑下
[MenuItem("CONTEXT/組件名/Test5")]
static void Test()

unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎

2.5 拓展Hierarchy右鍵菜單

//GameObject路徑下
[MenuItem("GameObject/Create Other/XXX", priority = 0)]
static void Test()

效果如下圖
unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎

2.6 CustomEditor自定義編輯器

自定義編輯器可以定義序列化字段的外觀, 同時在字段發(fā)生變化時做一些對應(yīng)的操作,使自己的功能像購買的那些nb的插件一樣好用!

[CustomEditor(typeof(MonoTest), true)]
public class MonoTestEditor : Editor
{
    public override void OnInspectorGUI()
    {
        MonoTest _target = target as MonoTest;
        //base.OnInspectorGUI();//按照Editor默認(rèn)的的方式繪制所有序列化屬性
        serializedObject.Update();//刷新serializedObject
		var param = serializedObject.FindProperty("Params");  
		//獲取參數(shù)重繪等等…………//   
        serializedObject.ApplyModifiedProperties();//應(yīng)用serializedObject,返回值能夠表明序列化字段是否有修改
    }
}
  • 第一行[CustomEditor(typeof(MonoTest), true)]用來描述要自定義的是哪個類, 第二個參數(shù)代表是否對其子類起效.
  • 將類繼承父類Editor(需要using UnityEditor;)
  • CustomEditor兩個關(guān)鍵屬性:
    1. target:代表自己類的實(shí)例對象,默認(rèn)是Object類型
    2. serializedObject:封裝了對應(yīng)類上的「序列化」字段及一些方法,序列化可以用SerializedProperty(單個屬性)、[System.Serializable](類似Param的類屬性);可以通過serializedObject.FindProperty()查找。
  • 重寫OnInspectorGUI,需要用到GUI, GUILayout, EditorGUILayout等,接下來詳細(xì)介紹

三,GUI/GUILayout

GUI:運(yùn)行時(game視圖)和編輯器里都可以繪制UI,需要自己算Rect
GUILayout:基于GUI實(shí)現(xiàn)的自動排版的繪制功能

3.1按鈕

  • 普通Button
GUI.backgroundColor = Color.red;
if (GUILayout.Button("Create"))
{
    CreateComponent(scr);
}

unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎

  • button網(wǎng)格
private int index;
private string[] buttonNames = new string[] { "1", "2", "3" };
index = GUILayout.SelectionGrid(index, buttonNames, 2); //第三個參數(shù)是指一行有幾個按鈕
//或者
index = GUILayout.Toolbar(index, buttonNames);//“工具欄”功能,都在一行了
  • 打勾Toggle
private bool toggleValue; 
toggleValue = GUILayout.Toggle(toggleValue, "ues XXX"); //第二個參數(shù)是Toggle文本信息

3.2文本

  • 普通文本標(biāo)簽
GUILayout.Label("FPS:");//屏幕左上顯示一行字的最簡單那種
  • 輸入文本區(qū)域
private string areaText = "multi-line text";
private string singleText = "single-line text..";
areaText = GUILayout.TextArea(areaText); //TextArea 支持多行編輯,回車換行
singleText = GUILayout.TextField(singleText); //Textfield 不支持回車換行,只能用\n的換行符

unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎

3.3滑條

  • 滑條Slider–用于改變值
private float hValue = 0;
private float vValue = 0;
//橫向、縱向滑條
hValue = GUILayout.HorizontalSlider(hValue, 0, 100, GUILayout.Width(300)); 
vValue = GUILayout.VerticalSlider(vValue, 0, 100);
  • 滾動條ScrollBar–用于改變百分比
private float blockSize = 10; //滑動塊大小
private float leftValue = 0; //最左側(cè)/最上側(cè)數(shù)值
private float rightValue = 100; //最右側(cè)/最下側(cè)數(shù)值
//橫向、縱向滾動條
hValue = GUILayout.HorizontalScrollbar(hValue, blockSize, leftValue, rightValue, GUILayout.Width(300));
vValue = GUILayout.VerticalScrollbar(vValue, blockSize, leftValue, rightValue);

3.4布局

  • Box區(qū)域
GUILayout.Box("Box Area", GUILayout.Width(200), GUILayout.Height(200));
  • 可視化Area–可用于自定義可視化內(nèi)容
GUILayout.BeginArea(new Rect(0, 0, 300, 700)); 
GUILayout.Button("Button in Area"); 
GUILayout.EndArea(); 
  • 水平/垂直布局–一半成對使用
GUILayout.BeginHorizontal(); 
GUILayout.Button("Horizontal Button No.1");
GUILayout.Button("Horizontal Button No.2"); 
GUILayout.EndHorizontal(); 

GUILayout.BeginVertical();
GUILayout.Button("Veritical Button No.1");
GUILayout.Button("Veritical Button No.2"); 
GUILayout.EndVertical();

unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎

  • 滑動視圖ScrollView
private Vector2 scrollViewRoot;
GUILayout.BeginArea(new Rect(0, 0, 400, 400)); //上邊的可視化Area
scrollViewRoot = GUILayout.BeginScrollView(scrollViewRoot); 
GUILayout.Button("Buttons", GUILayout.Height(200)); //這里加的組件要越界才能滑動
GUILayout.Button("Buttons", GUILayout.Height(200));
GUILayout.Button("Buttons", GUILayout.Height(200));
GUILayout.EndScrollView();
GUILayout.EndArea();

unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎

  • 空白間隔Space
GUILayout.FlexibleSpace();//把上下兩行代碼的組件合理間隔于Area里
GUILayout.Space(100); //手動間隔100個像素
  • 控件寬高
//不手動設(shè)置則會自動根據(jù)Area大小擴(kuò)展
GUILayout.Width(float width) // 設(shè)置控件的寬度
GUILayout.Height(float height) // 設(shè)置控件的高度
GUILayout.MinWidth(float width) // 設(shè)置控件的最小寬度
GUILayout.MinHeight(float height) // 設(shè)置控件的最小高度
GUILayout.MaxWidth(float width)// 設(shè)置控件的最大寬度
GUILayout.MaxHeight(float width) // 設(shè)置控件的最大高度
GUILayout.ExpandHeight(bool expand) // 是否允許自動擴(kuò)展高度
GUILayout.ExpandWidth(bool expand) // 是否允許自動擴(kuò)展寬度--不擴(kuò)展就是自適應(yīng)文字

四,EditorGUI/EditorGUILayout

EditorGUI、EditorGUILayout:只能在編輯器里用,需要自己算Rect;屬于GUILayoutd1派生,也可以用GUILayout替代

4.1基本面板

4.1.1 浮動窗口EditorWindow

就是這玩意:
unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎

using UnityEditor;

public class ClothMonitorMenu : EditorWindow
{
    private static ClothMonitorMenu Monitor { get; set; } //窗口實(shí)例對象,必須是一個static

    [MenuItem("Tools/Magica Cloth/Cloth Monitor", false)] //定義菜單欄位置
    public static void InitWindow() //必須是static
    {
        GetWindow<ClothMonitorMenu>(); //實(shí)例化窗口
    }
    /// 窗口內(nèi)顯示的GUI面板
    private void OnGUI()
    {
    }
}

之后的繪制都是在OnGUI()函數(shù)中

4.1.2 檢視Inspector面板

也就是重構(gòu)面板界面

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(MagicaMeshCloth))]typeof(TutorialMono)
public class MagicaMeshClothInspector : ClothEditor
//「ClothEditor內(nèi)部是public abstract class ClothEditor : Editor」----繼承Editor
{
    public override void OnInspectorGUI()
    {
    	MagicaMeshCloth scr = target as MagicaMeshCloth;
        …………
    }
}

之后的繪制都是在OnInspectorGUI()函數(shù)中

4.2 普通參數(shù)

  • 整數(shù)、浮點(diǎn)、字符串、向量等
m_intValue = EditorGUILayout.IntField("Int 輸入:", m_intValue);
m_floatValue = EditorGUILayout.FloatField("Float 輸入:", m_floatValue);
m_textValue = EditorGUILayout.TextField("Text輸入:", m_textValue);
m_vec2 = EditorGUILayout.Vector2Field("Vec2輸入: ", m_vec2);
m_vec3 = EditorGUILayout.Vector3Field("Vec3輸入: ", m_vec3);
m_vec4 = EditorGUILayout.Vector4Field("Vec4輸入: ", m_vec4);
m_bounds = EditorGUILayout.BoundsField("Bounds輸入: ", m_bounds);
m_boundsInt = EditorGUILayout.BoundsIntField("Bounds輸入: ", m_boundsInt);

unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎

  • 文本、標(biāo)簽層級選項(xiàng)等
EditorGUILayout.LabelField("文本標(biāo)題", "文本內(nèi)容");
EditorGUILayout.SelectableLabel("可復(fù)制的只讀文本內(nèi)容。");
m_layer = EditorGUILayout.LayerField("層級選擇", m_layer);
m_tag = EditorGUILayout.TagField("標(biāo)簽選擇", m_tag);//=GameObject Inspector上邊的“Tag”“Layer”
  • 曲線CurveField
private AnimationCurve m_curve = AnimationCurve.Linear(0, 0, 1, 1);
m_curve = EditorGUILayout.CurveField("動畫曲線:", m_curve);
  • 顏色
private Color m_color;
private GUIContent colorTitle = new GUIContent("顏色選擇");
...
//著重強(qiáng)調(diào)一下這個重載方法
//第一個參數(shù): GUIContent,通常拿來作為Title
//第二個參數(shù): Color,目標(biāo)修改數(shù)據(jù)
//第三個參數(shù): bool ,是否顯示拾色器
//第四個參數(shù): bool ,是否顯示透明度通道
//第五個參數(shù): bool ,是否支持HDR。
m_color = EditorGUILayout.ColorField(colorTitle, m_color, true, true, true);
  • 選擇對象GameObject、Transform、Material
private Transform objectReference;
objectReference = EditorGUILayout.ObjectField("Reference Object", objectReference, typeof(Transform/GameObject), true);//可以最后加一個as GameObject;進(jìn)行類型轉(zhuǎn)換
  • 注意?。核械摹癋ield”類,如果不把數(shù)據(jù)放在左邊賦值,那么在輸入框內(nèi)的修改將不會保存

4.3 拓展功能–相比自動界面

4.3.1 按鈕

  • 單個按鈕
if (GUILayout.Button("Create"))
{
   Undo.RecordObject(scr, "CreateMeshCloth");
   if (scr.ClothSelection == null)
       InitSelectorData();
}
  • 按鈕網(wǎng)格
public string[] selectGridStrs = new string[] { "按鈕1", "按鈕2", "按鈕3", "按鈕4" };
GUILayout.BeginVertical();
//(1.按鈕索引,2.按鈕顯示文本或圖片數(shù)組,3.一行多少列)
selectGridIdx = GUILayout.SelectionGrid(selectGridIdx, selectGridStrs, 2);
selectGridIdx = GUILayout.Toolbar(selectGridIdx, selectGridStrs);//“工具欄”功能,都在一行了
GUILayout.EndVertical();

unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎

4.3.2 枚舉

  • 單選枚舉
private enum TutorialEnum{One,Two,Three}
private TutorialEnum m_enum;
m_enum = (TutorialEnum)EditorGUILayout.EnumPopup("枚舉選擇", m_enum);
  • 多選枚舉
private enum TutorialEnum
{
    None = 0,
    OneAndTwo = One | Two,
    One = 1 << 0,
    Two = 1 << 1,
    Three = 1 << 2
}
private TutorialEnum m_enum;
m_enum = (TutorialEnum)EditorGUILayout.EnumFlagsField("枚舉多選", m_enum);

unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎

  • 整型單選、多選
private string[] intSelections = new string[] { "整數(shù)10", "整數(shù)20", "整數(shù)30" };
private string[] intMultiSelections = new string[] { "1號", "2號", "3號" };
private int[] intValues = new int[] { 10, 20, 30 };
...
m_singleInt = EditorGUILayout.IntPopup("整數(shù)單選框", m_singleInt, intSelections, intValues);
EditorGUILayout.LabelField($"m_singleInt is {m_singleInt}");
m_multiInt = EditorGUILayout.MaskField("整數(shù)多選框", m_multiInt, intMultiSelections);
EditorGUILayout.LabelField($"m_multiInt is {m_multiInt}");

unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎

4.3.3 折疊

  • 單個折疊
private bool foldOut;
foldOut = EditorGUILayout.Foldout(foldOut, "一般折疊欄");
if (foldOut)
{
        //折疊的內(nèi)容
}
  • 折疊組
private bool foldOut;
foldOut = EditorGUILayout.BeginFoldoutHeaderGroup(foldOut, "折疊欄組");
if (foldOut)
{
        //折疊的內(nèi)容
}
EditorGUILayout.EndFoldoutHeaderGroup();

4.3.4 滑動條

  • 單值滑條
EditorGUILayout.Slider(maxRotationSpeed, 0.0f, 720.0f, "Max Rotation Speed");
EditorGUILayout.IntSlider(bendMaxCount, 1, 6, "Bend Max Connection");
  • 范圍滑條
EditorGUILayout.MinMaxSlider("雙塊滑動條", ref m_leftValue, ref m_rightValue, 0.25f, 10.25f);
EditorGUILayout.FloatField("滑動左值:", m_leftValue);
EditorGUILayout.FloatField("滑動右值:", m_rightValue);

unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎

4.3.4 開關(guān)

  • 開關(guān)
//下邊兩個格式不同
toggle = EditorGUILayout.Toggle("Normal Toggle", toggle);//標(biāo)題      打勾框
toggle = EditorGUILayout.ToggleLeft("Left Toggle", toggle);//打勾框 標(biāo)題
  • 開關(guān)組(打開才能調(diào)整下邊參數(shù))
//下邊兩個格式不同
toggleGroupValue_1 = EditorGUILayout.BeginToggleGroup("Pos", toggleGroupValue_1);
vector3Filed = EditorGUILayout.Vector3Field("位置修改:", vector3Filed);
EditorGUILayout.EndToggleGroup();

unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎

4.3.5 提示警告

EditorGUILayout.HelpBox("一般提示,你應(yīng)該這樣做...", MessageType.Info);
EditorGUILayout.HelpBox("警告提示,你可能需要這樣做...", MessageType.Warning);
EditorGUILayout.HelpBox("錯誤提示,你不能這樣做...", MessageType.Error);

unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎

4.3.6 其他

  • 間隔Space
EditorGUILayout.Space();//出現(xiàn)最頻繁
GUILayout.FlexibleSpace(); 
  • 布局,同GUILayout
EditorGUILayout.BeginHorizontal(); //開始水平布局
EditorGUILayout.EndHorizontal();//結(jié)束水平布局

EditorGUILayout.BeginVertical();//開始垂直布局
EditorGUILayout.EndVertical();//結(jié)束垂直布局

scrollRoot = EditorGUILayout.BeginScrollView(scrollRoot); //開啟滾動視圖
EditorGUILayout.EndScrollView(); //結(jié)束滾動視圖

4.4 案例:互斥開關(guān)的實(shí)現(xiàn)

提個magica cloth選點(diǎn)界面的案例

using (new EditorGUILayout.HorizontalScope())
{
	int nowtype = selectPointType;
	for(int i = 0; i < tcnt; i++)
	{
	    GUI.backgroundColor = pointTypeList[i].col;
	    bool ret = GUILayout.Toggle(i == nowtype, pointTypeList[i].label, EditorStyles.miniButtonLeft);
	    if (ret)
	    {
	        nowtype = i;
	    }
	}
	if (nowtype != selectPointType)
	{
	    selectPointType = nowtype;
	}
}

unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎

五,Handles–Scene界面拓展

在Scene界面可視化一些UI空間并且實(shí)現(xiàn)用戶的交互操作,Handles手柄其實(shí)就是Scene里模型的坐標(biāo)拉桿

5.1 在Scene界面自定義繪制

在SceneView接口中,有一個duringSceneGui,可以實(shí)現(xiàn)自定義繪制

void OnEnable()
{
    SceneView.duringSceneGui += OnSceneFunc; //onSceneFunc為你自己的繪制方法
}

//我們自己的繪制方法
static void OnSceneFunc(SceneView view)
{
    //在這里面實(shí)現(xiàn)你想要的繪制效果
}

void OnDisable()
{
    SceneView.duringSceneGui -= OnSceneFunc; 
}

5.2 Handles常用接口方法

  • transform相關(guān)
//位置
Vector3 PositionHandle(Vector3 position, Quaternion rotation);
//旋轉(zhuǎn)
Quaternion RotationHandle(Quaternion rotation, Vector3 position);
//大小
Vector3 ScaleHandle(Vector3 scale, Vector3 position, Quaternion rotation, float size);
//變換(可以同時改變位置、旋轉(zhuǎn)、大小)
void TransformHandle(ref Vector3 position, ref Quaternion rotation, ref Vector3 scale);
void TransformHandle(ref Vector3 position, ref Quaternion rotation, ref float uniformScale);
void TransformHandle(ref Vector3 position, ref Quaternion rotation);
  • 繪制相關(guān)
//實(shí)線
void DrawLine(Vector3 p1, Vector3 p2); //實(shí)線
void DrawLines(Vector3[] lineSegments); //一系列線段
//多邊線
void DrawPolyLine(params Vector3[] points); //多邊線(穿過points列表的線)
//虛線
void DrawDottedLine(Vector3 p1, Vector3 p2, float screenSpaceSize);
 //一系列虛線
void DrawDottedLines(Vector3[] lineSegments, float screenSpaceSize); 

//貝塞爾曲線
void DrawBezier(Vector3 startPosition, Vector3 endPosition, Vector3 startTangent, Vector3 endTangent, Color color, Texture2D texture, float width);

//線形圓弧
void DrawWireArc(Vector3 center, Vector3 normal, Vector3 from, float angle, float radius);
//填充扇形
void DrawSolidArc(Vector3 center, Vector3 normal, Vector3 from, float angle, float radius);
//線形圓形
void DrawWireDisc(Vector3 center, Vector3 normal, float radius);
//填充圓形
void DrawSolidDisc(Vector3 center, Vector3 normal, float radius);

//矩形
void DrawSolidRectangleWithOutline(Vector3[] verts, Color faceColor, Color outlineColor);

//標(biāo)簽、文本
void Label(Vector3 position, string text);
void Label(Vector3 position, Texture image);

//攝像機(jī)
void DrawCamera(Rect position, Camera camera);

//縮放滑動條
float ScaleSlider(float scale, Vector3 position, Vector3 direction, Quaternion rotation, float size, float snap);

//繪制2D的UI
void BeginGUI(); //在3D的GUI塊中開啟2D的GUI塊
void EndGUI(); //結(jié)束2D的GUI塊

5.3 Event簡單介紹

在實(shí)際中搭配Event可以實(shí)現(xiàn)筆刷交互、地形調(diào)整高度等等,主要就是交互的一些事件,這里簡單介紹下,詳情見Unity官方Event API說明

  • 這里截取一段magica cloth的mesh選點(diǎn)操作
if (Event.current.type == EventType.MouseDown && Event.current.button == 0 && !Event.current.alt)
{
    hitTest(spos, epos, pointSize * 0.5f);
    GUIUtility.hotControl = controlId;
    Event.current.Use(); 
}
if (Event.current.type == EventType.MouseDrag && Event.current.button == 0 && !Event.current.alt)
{
    hitTest(spos, epos, pointSize * 0.5f);
    Event.current.Use();
}
if (Event.current.type == EventType.MouseUp && Event.current.button == 0 && !Event.current.alt)
{
    GUIUtility.hotControl = 0;
    Event.current.Use(); // ?
}

if (Event.current.type == EventType.Repaint)
………………
  • EventType類型
    unity editorwindow 游戲體,unity,學(xué)習(xí),游戲引擎

參考資料

1,Unity’s documentation on MonoBehaviours
2,Unity中的CustomEditor(自定義編輯器)
3,Unity3d Editor 編輯器擴(kuò)展功能詳解一系列文章
4,UnityEditor編輯器擴(kuò)展
5,Unity編輯器開發(fā)(七)Scene界面拓展之Handles文章來源地址http://www.zghlxwxcb.cn/news/detail-767475.html

到了這里,關(guān)于unity腳本基礎(chǔ)+編輯器UnityEditor學(xué)習(xí)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Unity 編輯器-創(chuàng)建模板腳本,并自動綁定屬性,添加點(diǎn)擊事件

    Unity 編輯器-創(chuàng)建模板腳本,并自動綁定屬性,添加點(diǎn)擊事件

    當(dāng)使用框架開發(fā)時,Prefab掛載的很多腳本都有固定的格式。從Unity的基礎(chǔ)模板創(chuàng)建cs文件,再修改到應(yīng)有的模板,會浪費(fèi)一些時間。尤其是有大量的不同界面時,每個都改一遍,浪費(fèi)時間不說,還有可能遺漏或錯改。寫個腳本創(chuàng)建指定的模板代替C#基礎(chǔ)模板。 注:當(dāng)前腳本使用

    2024年02月13日
    瀏覽(86)
  • Unity編輯器擴(kuò)展 | 編輯器擴(kuò)展基礎(chǔ)入門

    Unity編輯器擴(kuò)展 | 編輯器擴(kuò)展基礎(chǔ)入門

    前言 當(dāng)談到游戲開發(fā)工具,Unity編輯器是一個備受贊譽(yù)的平臺。它為開發(fā)者提供了一個強(qiáng)大且靈活的環(huán)境,使他們能夠創(chuàng)建令人驚嘆的游戲和交互式體驗(yàn)。 然而,Unity編輯器本身也是可以擴(kuò)展和定制的,這為開發(fā)者提供了進(jìn)一步提升工作流程和增強(qiáng)功能的機(jī)會。 在Unity 編輯器

    2024年02月10日
    瀏覽(88)
  • 【Unity編輯器擴(kuò)展】 | 編輯器擴(kuò)展入門基礎(chǔ)

    【Unity編輯器擴(kuò)展】 | 編輯器擴(kuò)展入門基礎(chǔ)

    前言 當(dāng)談到游戲開發(fā)工具,Unity編輯器是一個備受贊譽(yù)的平臺。它為開發(fā)者提供了一個強(qiáng)大且靈活的環(huán)境,使他們能夠創(chuàng)建令人驚嘆的游戲和交互式體驗(yàn)。 然而,Unity編輯器本身也是可以擴(kuò)展和定制的,這為開發(fā)者提供了進(jìn)一步提升工作流程和增強(qiáng)功能的機(jī)會。 在Unity 編輯器

    2024年02月10日
    瀏覽(33)
  • Unity快手上手【熟悉unity編輯器,C#腳本控制組件一些屬性之類的】

    首先了解unity相關(guān)概述,快速認(rèn)識unity編輯器,然后抓住重點(diǎn)的學(xué):游戲?qū)ο?、組件|C#腳本、預(yù)制體、UI ? 學(xué)習(xí)過程你會發(fā)現(xiàn),其實(shí)Unity中主要是用c#進(jìn)行開發(fā)。 因?yàn)樵谶@個過程中,無非就是,對游戲?qū)ο笸ㄟ^掛載的C#腳本,修改一下組件的一些屬性,控制一下激活之類的操作

    2023年04月13日
    瀏覽(36)
  • 面試題 :Unity編輯器基礎(chǔ)

    面試題 :Unity編輯器基礎(chǔ)

    ? 1、請描述游戲動畫有幾種,以及其原理。 關(guān)鍵幀動畫 :每一幀動畫序列當(dāng)中包含了頂點(diǎn)的空間位置信息以及改變量,然后通過插值運(yùn)算,得出動畫效果。選中某一游戲?qū)ο螅瑒?chuàng)建animation,添加屬性Transform,MeshRender、collider。還可以添加關(guān)鍵幀,在關(guān)鍵幀上Add Animation Even

    2023年04月17日
    瀏覽(24)
  • 【Unity UIToolkit】UIBuilder基礎(chǔ)教程-制作簡易的對話系統(tǒng)編輯器 3步教你玩轉(zhuǎn)Unity編輯器擴(kuò)展工具

    【Unity UIToolkit】UIBuilder基礎(chǔ)教程-制作簡易的對話系統(tǒng)編輯器 3步教你玩轉(zhuǎn)Unity編輯器擴(kuò)展工具

    隨著Unity開發(fā)的深入,基本的Unity編輯器界面并不能滿足大部分玩家高階開發(fā)的要求。為了提高開發(fā)的效率,有針對性的定制化擴(kuò)展編輯器界面是提高開發(fā)效率的不錯選擇。 今天就給大家?guī)鞺nity官方提高的編輯器擴(kuò)展工具UIToolkit(集成了UIBuilder和UI Debugger等插件)的使用教程。

    2024年02月04日
    瀏覽(77)
  • Unity編輯器基礎(chǔ) EditorGUILayout (大部分用法)

    Unity編輯器基礎(chǔ) EditorGUILayout (大部分用法)

    如圖 關(guān)于效果圖最后它的代碼我隱藏掉了如何想看看可以自行打開

    2024年02月11日
    瀏覽(18)
  • 【Linux命令行與Shell腳本編程】第十八章 文本處理與編輯器基礎(chǔ)

    【Linux命令行與Shell腳本編程】第十八章 文本處理與編輯器基礎(chǔ)

    文本處理 學(xué)習(xí)sed編輯器 sed編輯器基礎(chǔ)命令 gawk編輯器入門 sed編輯器基礎(chǔ) shell腳本可以將文本文件中各種數(shù)據(jù)的日常處理任務(wù)自動化Linux中的sed和gawk兩款工具能夠極大地簡化數(shù)據(jù)處理任務(wù)。 想要即時處理文本文件中的文本,有一個可以自動格式化、插入、修改或刪除文本元素

    2024年02月13日
    瀏覽(16)
  • Visual Studio連接unity編輯器_unity基礎(chǔ)開發(fā)教程

    Visual Studio連接unity編輯器_unity基礎(chǔ)開發(fā)教程

    當(dāng)我們在unity編輯器中打開C#腳本的時候發(fā)現(xiàn)Visual Studio沒有連接unity編輯器,在編寫代碼的時候也沒有unity的提醒。 簡單來說就是敲代碼沒有代碼提示。 這時候需要在unity中進(jìn)行設(shè)置,與Visual Studio進(jìn)行連接 在unity編輯器中,選擇Edit,點(diǎn)擊Preference 在Preference窗口中選擇

    2024年02月04日
    瀏覽(26)
  • Sprite Editor圖片編輯器的使用_unity基礎(chǔ)開發(fā)教程

    Sprite Editor圖片編輯器的使用_unity基礎(chǔ)開發(fā)教程

    SpriteEditor是Unity引擎中的一個工具,用于創(chuàng)建和編輯2D圖片。它提供了一系列功能,可以對圖片進(jìn)行剪裁、切割、翻轉(zhuǎn)、旋轉(zhuǎn)、調(diào)整大小等操作,以及設(shè)置圖片的碰撞檢測形狀和渲染模式。 SpriteEditor可以幫助開發(fā)者將多張圖片合并成動畫精靈,并為每一幀設(shè)置播放時間和循環(huán)

    2024年02月01日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包