菜單相關
PreferenceItem
使用該屬性可以定制Unity的Preference界面,如Vscode插件。官方示例:
public class ExampleScript : MonoBehaviour
{
// Have we loaded the prefs yet
private static bool prefsLoaded = false;
// The Preferences
public static bool boolPreference = false;
// Add preferences section named "My Preferences" to the Preferences Window
[PreferenceItem("My Preferences")]
public static void PreferencesGUI()
{
// Load the preferences
if (!prefsLoaded)
{
boolPreference = EditorPrefs.GetBool("BoolPreferenceKey", false);
prefsLoaded = true;
}
// Preferences GUI
boolPreference = EditorGUILayout.Toggle("Bool Preference", boolPreference);
// Save the preferences
if (GUI.changed)
EditorPrefs.SetBool("BoolPreferenceKey", boolPreference);
}
}
AddComponentMenu
(添加Component菜單).名稱(TestComponent)不能和類名相同。
[AddComponentMenu("TestMenu/TestComponent")]
public class ComponentMenuTest : MonoBehaviour {
}
MenuItem
普通菜單項
Project面板右鍵菜單
#region 右鍵菜單 導出資源包(選中資源時有效)
[MenuItem("Assets/導出Unity資源包", true)]
static bool ExportPackageValidation()
{
for (var i = 0; i < Selection.objects.Length; i++)
{
if (AssetDatabase.GetAssetPath(Selection.objects[i]) != "")
return true;
}
return false;
}
[MenuItem("Assets/導出Unity資源包")]
static void ExportPackage()
{
var path = EditorUtility.SaveFilePanel("Save unitypackage", "", "", "unitypackage");
if (path == "")
return;
UnityEngine.Debug.Log("路徑:" + path);
var assetPathNames = new string[Selection.objects.Length];
for (var i = 0; i < assetPathNames.Length; i++)
{
assetPathNames[i] = AssetDatabase.GetAssetPath(Selection.objects[i]);
}
assetPathNames = AssetDatabase.GetDependencies(assetPathNames);
AssetDatabase.ExportPackage(assetPathNames, path,
ExportPackageOptions.Interactive | ExportPackageOptions.Recurse | ExportPackageOptions.IncludeDependencies);
}
#endregion
Hierarchy右鍵菜單
[MenuItem("GameObject/MenuTest/MenuTest1", false, -2)]
static void RightHierarchy()
{
Debug.Log("Hierarchy右鍵菜單");
}
Inspector面板
ContextMenu
可以在對應腳本的Inspector的中增加右鍵菜單選項。
[ContextMenu("菜單項")]
void DoSomething()
{
Debug.Log("腳本上的菜單項");
}
ContextMenuItem
在Inspector上面對變量追加一個右鍵菜單。
[ContextMenuItem("Reset", "ResetValue")]
public string value = "Default";
void ResetValue()
{
value = "Default";
}
Header
在Inspector中變量的上面增加Header。
[Header("小標題")]
Space
inspector面板上空一些位置。
TextArea
該屬性可以把string在Inspector上的編輯區(qū)變成一個TextArea。
Tooltip
當鼠標指針移動到Inspector上時候顯示。
HideInInspector
在Inspector上隱藏public變量。
RangeAttribute
在int或者float類型上使用,限制輸入值的范圍
初始化
InitializeOnLoad
在Class上使用,可以在Unity啟動的時候,運行Editor腳本。需要該Class擁有靜態(tài)的構造函數(shù)。
[InitializeOnLoad]
public class ScriptExecuteOrderSetting
{
static ScriptExecuteOrderSetting()
{
UnityEngine.Debug.Log("ScriptExecuteOrderSetting");
}
}
InitializeOnLoadMethod
在Method上使用,是InitializeOnLoad的Method版本。Method必須是static的。
Editor回調
以下三個Callback的屬性,都需要方法為static。
OnOpenAsset
在打開一個Asset后被調用,如打開腳本。
[OnOpenAsset(1)]
public static bool step1(int instanceID, int line)
{
string name = EditorUtility.InstanceIDToObject(instanceID).name;
Debug.Log("Open Asset step: 1, (" + name + ")");
return false; //使用默認方式打開
}
PostProcessBuild
該屬性是在build完成后,被調用的callback。同時具有多個的時候,可以指定先后順序。
[PostProcessBuild(1)]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
{
Debug.Log(pathToBuiltProject);
}
PostProcessScene
使用該屬性的函數(shù),在scene被build之前,會被調用。具體使用方法和PostProcessBuild類似。
其他
UnityAPICompatibilityVersion
用來聲明API的版本兼容性
SerializeField
強制序列化屬性。
RequireComponent
當該Class被添加到一個GameObject上的時候,如果這個GameObject不含有依賴的Component,會自動添加該Component,且不可移除。
當該Class被添加到一個GameObject上的時候,如果這個GameObject不含有依賴的Component,會自動添加該Component。
RPC
在方法上添加該屬性,可以網(wǎng)絡通信中對該方法進行RPC調用。
DisallowMultipleComponent
繼承MonoBehavior的類,在同一個GameObject上面,最多只能添加一個該類的實例。