Unity Workflows by cryptorabea/claude_unity_dev_plugin
npx skills add https://github.com/cryptorabea/claude_unity_dev_plugin --skill 'Unity Workflows'Unity 编辑器脚本编写、输入系统、UI 开发和资源管理的基本工作流。
高效的 Unity 工作流可以加速开发并减少错误。本技能涵盖编辑器自定义、现代输入处理、UI 系统和资源管线优化。
核心工作流领域:
创建自定义菜单命令:
using UnityEditor;
public static class CustomMenu
{
[MenuItem("Tools/My Tool")]
private static void ExecuteTool()
{
Debug.Log("Custom tool executed");
}
[MenuItem("Tools/My Tool", true)] // Validation
private static bool ValidateTool()
{
return Selection.activeGameObject != null;
}
// Keyboard shortcut: Ctrl+Shift+T (Windows), Cmd+Shift+T (Mac)
[MenuItem("Tools/Quick Action %#t")]
private static void QuickAction()
{
// Action
}
}
快捷键按键:
% = Ctrl (Mac 上为 Cmd)# = Shift广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
& = Alt_g = G 键为 GameObject/Component 创建右键上下文菜单:
[MenuItem("GameObject/My Custom Action", false, 10)]
private static void CustomAction()
{
GameObject selected = Selection.activeGameObject;
// Perform action
}
// Component context menu
public class MyComponent : MonoBehaviour
{
[ContextMenu("Do Something")]
private void DoSomething()
{
Debug.Log("Context menu action");
}
[ContextMenuItem("Reset Value", "ResetValue")]
[SerializeField] private int value;
private void ResetValue()
{
value = 0;
}
}
为自定义类型重写检视面板:
[CustomEditor(typeof(MyScript))]
public class MyScriptEditor : Editor
{
public override void OnInspectorGUI()
{
// Draw default inspector
DrawDefaultInspector();
MyScript script = (MyScript)target;
// Custom button
if (GUILayout.Button("Execute Action"))
{
script.ExecuteAction();
}
// Custom fields
EditorGUILayout.LabelField("Custom Section", EditorStyles.boldLabel);
script.customValue = EditorGUILayout.IntSlider("Custom Value", script.customValue, 0, 100);
// Apply changes
if (GUI.changed)
{
EditorUtility.SetDirty(target);
}
}
}
在检视面板中自定义属性显示:
[System.Serializable]
public class Range
{
public float min;
public float max;
}
[CustomPropertyDrawer(typeof(Range))]
public class RangeDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
var minProp = property.FindPropertyRelative("min");
var maxProp = property.FindPropertyRelative("max");
float halfWidth = position.width / 2;
Rect minRect = new Rect(position.x, position.y, halfWidth - 5, position.height);
Rect maxRect = new Rect(position.x + halfWidth, position.y, halfWidth - 5, position.height);
EditorGUI.PropertyField(minRect, minProp, GUIContent.none);
EditorGUI.PropertyField(maxRect, maxProp, GUIContent.none);
EditorGUI.EndProperty();
}
}
创建自定义编辑器窗口:
public class MyEditorWindow : EditorWindow
{
private string textField = "";
private int intField = 0;
[MenuItem("Window/My Editor Window")]
private static void ShowWindow()
{
var window = GetWindow<MyEditorWindow>();
window.titleContent = new GUIContent("My Tool");
window.minSize = new Vector2(300, 200);
window.Show();
}
private void OnGUI()
{
GUILayout.Label("My Custom Tool", EditorStyles.boldLabel);
textField = EditorGUILayout.TextField("Text Field", textField);
intField = EditorGUILayout.IntField("Int Field", intField);
if (GUILayout.Button("Execute"))
{
Execute();
}
}
private void Execute()
{
Debug.Log($"Executed with: {textField}, {intField}");
}
}
以编程方式操作资源:
using UnityEditor;
public static class AssetUtilities
{
[MenuItem("Assets/Create Prefab From Selection")]
private static void CreatePrefab()
{
GameObject selected = Selection.activeGameObject;
if (selected == null)
return;
string path = $"Assets/Prefabs/{selected.name}.prefab";
// Create prefab
PrefabUtility.SaveAsPrefabAsset(selected, path);
AssetDatabase.Refresh();
}
public static T LoadAsset<T>(string path) where T : UnityEngine.Object
{
return AssetDatabase.LoadAssetAtPath<T>(path);
}
public static void CreateFolder(string path)
{
if (!AssetDatabase.IsValidFolder(path))
{
AssetDatabase.CreateFolder(System.IO.Path.GetDirectoryName(path), System.IO.Path.GetFileName(path));
}
}
}
安装:Window > Package Manager > Input System
创建输入动作:
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
private PlayerInput playerInput;
private InputAction moveAction;
private InputAction jumpAction;
private void Awake()
{
playerInput = GetComponent<PlayerInput>();
moveAction = playerInput.actions["Move"];
jumpAction = playerInput.actions["Jump"];
}
private void OnEnable()
{
jumpAction.performed += OnJump;
}
private void OnDisable()
{
jumpAction.performed -= OnJump;
}
private void Update()
{
Vector2 moveInput = moveAction.ReadValue<Vector2>();
Move(moveInput);
}
private void OnJump(InputAction.CallbackContext context)
{
Jump();
}
private void Move(Vector2 input) { }
private void Jump() { }
}
// After generating C# class from Input Actions asset
private PlayerInputActions inputActions;
private void Awake()
{
inputActions = new PlayerInputActions();
}
private void OnEnable()
{
inputActions.Player.Enable();
inputActions.Player.Jump.performed += OnJump;
}
private void OnDisable()
{
inputActions.Player.Disable();
inputActions.Player.Jump.performed -= OnJump;
}
private void Update()
{
Vector2 move = inputActions.Player.Move.ReadValue<Vector2>();
}
优点:
// Still functional, simpler for basic games
private void Update()
{
// Keyboard
if (Input.GetKeyDown(KeyCode.Space))
Jump();
// Mouse
if (Input.GetMouseButtonDown(0))
Fire();
// Axis (configured in Edit > Project Settings > Input Manager)
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Move(new Vector2(horizontal, vertical));
}
适用于:
新版输入系统更适用于:
标准的 Unity UI 系统:
using UnityEngine.UI;
using TMPro; // TextMeshPro
public class UIManager : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI scoreText;
[SerializeField] private Button playButton;
[SerializeField] private Slider healthSlider;
private void Start()
{
playButton.onClick.AddListener(OnPlayClicked);
}
private void OnDestroy()
{
playButton.onClick.RemoveListener(OnPlayClicked);
}
public void UpdateScore(int score)
{
scoreText.text = $"Score: {score}";
}
public void UpdateHealth(float health, float maxHealth)
{
healthSlider.value = health / maxHealth;
}
private void OnPlayClicked()
{
Debug.Log("Play button clicked");
}
}
Canvas 渲染模式:
UI 优化:
运行时和编辑器 UI(Unity 2021+):
UXML(UI 结构):
<ui:UXML>
<ui:VisualElement name="root">
<ui:Label text="Score: 0" name="scoreLabel"/>
<ui:Button text="Play" name="playButton"/>
</ui:VisualElement>
</ui:UXML>
USS(样式):
.root {
flex-grow: 1;
background-color: rgb(50, 50, 50);
}
#scoreLabel {
font-size: 24px;
color: white;
}
C#(逻辑):
using UnityEngine.UIElements;
public class UIController : MonoBehaviour
{
private UIDocument uiDocument;
private Label scoreLabel;
private Button playButton;
private void Awake()
{
uiDocument = GetComponent<UIDocument>();
var root = uiDocument.rootVisualElement;
scoreLabel = root.Q<Label>("scoreLabel");
playButton = root.Q<Button>("playButton");
playButton.clicked += OnPlayClicked;
}
public void UpdateScore(int score)
{
scoreLabel.text = $"Score: {score}";
}
private void OnPlayClicked()
{
Debug.Log("Play clicked");
}
}
优点:
适用于:
异步资源加载和内存管理:
设置:
加载资源:
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
public class AssetLoader : MonoBehaviour
{
private async void Start()
{
// Load asset asynchronously
var handle = Addressables.LoadAssetAsync<GameObject>("Enemy");
await handle.Task;
if (handle.Status == AsyncOperationStatus.Succeeded)
{
GameObject prefab = handle.Result;
Instantiate(prefab);
}
// Release when done
Addressables.Release(handle);
}
// Instantiate directly
private async void SpawnEnemy()
{
var handle = Addressables.InstantiateAsync("Enemy");
await handle.Task;
GameObject enemy = handle.Result;
// Use enemy
}
}
优点:
适用于:
简单的资源加载:
// Load from Resources folder
GameObject prefab = Resources.Load<GameObject>("Prefabs/Enemy");
// All assets include in build - inefficient
避免: 新项目请使用 Addressables。
using UnityEditor;
using UnityEditor.Build.Reporting;
public static class BuildUtility
{
[MenuItem("Build/Build Windows")]
private static void BuildWindows()
{
BuildPlayerOptions options = new BuildPlayerOptions
{
scenes = new[] { "Assets/Scenes/MainMenu.unity", "Assets/Scenes/Game.unity" },
locationPathName = "Builds/Windows/Game.exe",
target = BuildTarget.StandaloneWindows64,
options = BuildOptions.None
};
BuildReport report = BuildPipeline.BuildPlayer(options);
if (report.summary.result == BuildResult.Succeeded)
{
Debug.Log($"Build succeeded: {report.summary.totalSize} bytes");
}
else
{
Debug.LogError($"Build failed: {report.summary.result}");
}
}
}
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
public class BuildPreprocessor : IPreprocessBuildWithReport
{
public int callbackOrder => 0;
public void OnPreprocessBuild(BuildReport report)
{
Debug.Log("Preprocessing build...");
// Validate assets
// Update version number
// Generate build info
}
}
✅ 应该:
❌ 不应该:
黄金法则:使用编辑器工具自动化工作流。花时间创建工具可以成倍地节省迭代时间。
应用这些工作流优化和现代系统,以实现高效、可扩展的 Unity 开发。
每周安装
0
仓库
GitHub 星标
3
首次出现
1970年1月1日
安全审计
Essential workflows for Unity editor scripting, input systems, UI development, and asset management.
Efficient Unity workflows accelerate development and reduce errors. This skill covers editor customization, modern input handling, UI systems, and asset pipeline optimization.
Core workflow areas:
Create custom menu commands:
using UnityEditor;
public static class CustomMenu
{
[MenuItem("Tools/My Tool")]
private static void ExecuteTool()
{
Debug.Log("Custom tool executed");
}
[MenuItem("Tools/My Tool", true)] // Validation
private static bool ValidateTool()
{
return Selection.activeGameObject != null;
}
// Keyboard shortcut: Ctrl+Shift+T (Windows), Cmd+Shift+T (Mac)
[MenuItem("Tools/Quick Action %#t")]
private static void QuickAction()
{
// Action
}
}
Shortcut keys:
% = Ctrl (Cmd on Mac)# = Shift& = Alt_g = G keyRight-click context menu for GameObjects/Components:
[MenuItem("GameObject/My Custom Action", false, 10)]
private static void CustomAction()
{
GameObject selected = Selection.activeGameObject;
// Perform action
}
// Component context menu
public class MyComponent : MonoBehaviour
{
[ContextMenu("Do Something")]
private void DoSomething()
{
Debug.Log("Context menu action");
}
[ContextMenuItem("Reset Value", "ResetValue")]
[SerializeField] private int value;
private void ResetValue()
{
value = 0;
}
}
Override Inspector for custom types:
[CustomEditor(typeof(MyScript))]
public class MyScriptEditor : Editor
{
public override void OnInspectorGUI()
{
// Draw default inspector
DrawDefaultInspector();
MyScript script = (MyScript)target;
// Custom button
if (GUILayout.Button("Execute Action"))
{
script.ExecuteAction();
}
// Custom fields
EditorGUILayout.LabelField("Custom Section", EditorStyles.boldLabel);
script.customValue = EditorGUILayout.IntSlider("Custom Value", script.customValue, 0, 100);
// Apply changes
if (GUI.changed)
{
EditorUtility.SetDirty(target);
}
}
}
Custom property display in Inspector:
[System.Serializable]
public class Range
{
public float min;
public float max;
}
[CustomPropertyDrawer(typeof(Range))]
public class RangeDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
var minProp = property.FindPropertyRelative("min");
var maxProp = property.FindPropertyRelative("max");
float halfWidth = position.width / 2;
Rect minRect = new Rect(position.x, position.y, halfWidth - 5, position.height);
Rect maxRect = new Rect(position.x + halfWidth, position.y, halfWidth - 5, position.height);
EditorGUI.PropertyField(minRect, minProp, GUIContent.none);
EditorGUI.PropertyField(maxRect, maxProp, GUIContent.none);
EditorGUI.EndProperty();
}
}
Create custom editor windows:
public class MyEditorWindow : EditorWindow
{
private string textField = "";
private int intField = 0;
[MenuItem("Window/My Editor Window")]
private static void ShowWindow()
{
var window = GetWindow<MyEditorWindow>();
window.titleContent = new GUIContent("My Tool");
window.minSize = new Vector2(300, 200);
window.Show();
}
private void OnGUI()
{
GUILayout.Label("My Custom Tool", EditorStyles.boldLabel);
textField = EditorGUILayout.TextField("Text Field", textField);
intField = EditorGUILayout.IntField("Int Field", intField);
if (GUILayout.Button("Execute"))
{
Execute();
}
}
private void Execute()
{
Debug.Log($"Executed with: {textField}, {intField}");
}
}
Manipulate assets programmatically:
using UnityEditor;
public static class AssetUtilities
{
[MenuItem("Assets/Create Prefab From Selection")]
private static void CreatePrefab()
{
GameObject selected = Selection.activeGameObject;
if (selected == null)
return;
string path = $"Assets/Prefabs/{selected.name}.prefab";
// Create prefab
PrefabUtility.SaveAsPrefabAsset(selected, path);
AssetDatabase.Refresh();
}
public static T LoadAsset<T>(string path) where T : UnityEngine.Object
{
return AssetDatabase.LoadAssetAtPath<T>(path);
}
public static void CreateFolder(string path)
{
if (!AssetDatabase.IsValidFolder(path))
{
AssetDatabase.CreateFolder(System.IO.Path.GetDirectoryName(path), System.IO.Path.GetFileName(path));
}
}
}
Install: Window > Package Manager > Input System
Create Input Actions:
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
private PlayerInput playerInput;
private InputAction moveAction;
private InputAction jumpAction;
private void Awake()
{
playerInput = GetComponent<PlayerInput>();
moveAction = playerInput.actions["Move"];
jumpAction = playerInput.actions["Jump"];
}
private void OnEnable()
{
jumpAction.performed += OnJump;
}
private void OnDisable()
{
jumpAction.performed -= OnJump;
}
private void Update()
{
Vector2 moveInput = moveAction.ReadValue<Vector2>();
Move(moveInput);
}
private void OnJump(InputAction.CallbackContext context)
{
Jump();
}
private void Move(Vector2 input) { }
private void Jump() { }
}
// After generating C# class from Input Actions asset
private PlayerInputActions inputActions;
private void Awake()
{
inputActions = new PlayerInputActions();
}
private void OnEnable()
{
inputActions.Player.Enable();
inputActions.Player.Jump.performed += OnJump;
}
private void OnDisable()
{
inputActions.Player.Disable();
inputActions.Player.Jump.performed -= OnJump;
}
private void Update()
{
Vector2 move = inputActions.Player.Move.ReadValue<Vector2>();
}
Benefits:
// Still functional, simpler for basic games
private void Update()
{
// Keyboard
if (Input.GetKeyDown(KeyCode.Space))
Jump();
// Mouse
if (Input.GetMouseButtonDown(0))
Fire();
// Axis (configured in Edit > Project Settings > Input Manager)
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Move(new Vector2(horizontal, vertical));
}
Use for:
New Input System preferred for:
Standard Unity UI system:
using UnityEngine.UI;
using TMPro; // TextMeshPro
public class UIManager : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI scoreText;
[SerializeField] private Button playButton;
[SerializeField] private Slider healthSlider;
private void Start()
{
playButton.onClick.AddListener(OnPlayClicked);
}
private void OnDestroy()
{
playButton.onClick.RemoveListener(OnPlayClicked);
}
public void UpdateScore(int score)
{
scoreText.text = $"Score: {score}";
}
public void UpdateHealth(float health, float maxHealth)
{
healthSlider.value = health / maxHealth;
}
private void OnPlayClicked()
{
Debug.Log("Play button clicked");
}
}
Canvas render modes:
UI Optimization:
Runtime and Editor UI (Unity 2021+):
UXML (UI structure):
<ui:UXML>
<ui:VisualElement name="root">
<ui:Label text="Score: 0" name="scoreLabel"/>
<ui:Button text="Play" name="playButton"/>
</ui:VisualElement>
</ui:UXML>
USS (Styling):
.root {
flex-grow: 1;
background-color: rgb(50, 50, 50);
}
#scoreLabel {
font-size: 24px;
color: white;
}
C# (Logic):
using UnityEngine.UIElements;
public class UIController : MonoBehaviour
{
private UIDocument uiDocument;
private Label scoreLabel;
private Button playButton;
private void Awake()
{
uiDocument = GetComponent<UIDocument>();
var root = uiDocument.rootVisualElement;
scoreLabel = root.Q<Label>("scoreLabel");
playButton = root.Q<Button>("playButton");
playButton.clicked += OnPlayClicked;
}
public void UpdateScore(int score)
{
scoreLabel.text = $"Score: {score}";
}
private void OnPlayClicked()
{
Debug.Log("Play clicked");
}
}
Benefits:
Use for:
Async asset loading and memory management:
Setup:
Loading assets:
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
public class AssetLoader : MonoBehaviour
{
private async void Start()
{
// Load asset asynchronously
var handle = Addressables.LoadAssetAsync<GameObject>("Enemy");
await handle.Task;
if (handle.Status == AsyncOperationStatus.Succeeded)
{
GameObject prefab = handle.Result;
Instantiate(prefab);
}
// Release when done
Addressables.Release(handle);
}
// Instantiate directly
private async void SpawnEnemy()
{
var handle = Addressables.InstantiateAsync("Enemy");
await handle.Task;
GameObject enemy = handle.Result;
// Use enemy
}
}
Benefits:
Use for:
Simple asset loading:
// Load from Resources folder
GameObject prefab = Resources.Load<GameObject>("Prefabs/Enemy");
// All assets include in build - inefficient
Avoid: Use Addressables instead for new projects.
using UnityEditor;
using UnityEditor.Build.Reporting;
public static class BuildUtility
{
[MenuItem("Build/Build Windows")]
private static void BuildWindows()
{
BuildPlayerOptions options = new BuildPlayerOptions
{
scenes = new[] { "Assets/Scenes/MainMenu.unity", "Assets/Scenes/Game.unity" },
locationPathName = "Builds/Windows/Game.exe",
target = BuildTarget.StandaloneWindows64,
options = BuildOptions.None
};
BuildReport report = BuildPipeline.BuildPlayer(options);
if (report.summary.result == BuildResult.Succeeded)
{
Debug.Log($"Build succeeded: {report.summary.totalSize} bytes");
}
else
{
Debug.LogError($"Build failed: {report.summary.result}");
}
}
}
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
public class BuildPreprocessor : IPreprocessBuildWithReport
{
public int callbackOrder => 0;
public void OnPreprocessBuild(BuildReport report)
{
Debug.Log("Preprocessing build...");
// Validate assets
// Update version number
// Generate build info
}
}
✅ DO:
❌ DON'T:
Golden rule : Automate workflows with editor tools. Time spent creating tools saves exponentially more time in iteration.
Apply these workflow optimizations and modern systems for efficient, scalable Unity development.
Weekly Installs
0
Repository
GitHub Stars
3
First Seen
Jan 1, 1970
Security Audits
agent-browser 浏览器自动化工具 - Vercel Labs 命令行网页操作与测试
147,400 周安装