就在于通过VIA软件,将旋钮的顺时针/逆时针旋转,映射到普通键盘按键上,然后让你的Unity灯光控制脚本去监听这些按键。
整个方案的实现路径可以清晰地概括为以下流程图:

下面是具体的操作步骤:
第一步:在VIA软件中为旋钮分配按键
这是最核心的一步。你需要为三个旋钮的顺时针和逆时针旋转,共6个动作,分配6个不冲突的键盘按键。
打开VIA软件,识别你的小键盘。
找到旋钮的映射设置(通常显示为 Knob 1 CW / Knob 1 CCW 等,CW为顺时针,CCW为逆时针)。
为它们分配按键。建议方案如下(避免与Unity常用快捷键冲突):
控制功能 旋钮方向 建议映射按键 在脚本中对应的 KeyCode
强度 顺时针增加 U KeyCode.U
强度 逆时针减少 I KeyCode.I
色相 顺时针增加 O KeyCode.O
色相 逆时针减少 P KeyCode.P
饱和度 顺时针增加 J KeyCode.J
饱和度 逆时针减少 K KeyCode.K
关键:在VIA中,将旋钮的“动作类型”设置为 KC(按键代码),而不是媒体键或其他。这样Unity才能识别为普通的键盘事件。
第二步:修改Unity灯光控制脚本
你需要将你现有脚本的键盘监听部分进行扩展,以支持旋钮映射的新按键。
找到 DuringSceneGUI 方法中处理键盘输入的部分(if (e.type == EventType.KeyDown) 代码块),在现有逻辑后面添加对新按键的检测。
// ... 你脚本中现有的按键检测代码(如H/J/K/L等)...
// === 新增:旋钮按键检测 ===
else if (e.keyCode == KeyCode.U) // 强度+
{
currentLight.intensity += intensityStep; // intensityStep 是你已有的强度步长变量
handled = true;
}
else if (e.keyCode == KeyCode.I) // 强度-
{
currentLight.intensity = Mathf.Max(0, currentLight.intensity - intensityStep);
handled = true;
}
else if (e.keyCode == KeyCode.O) // 色相+
{
AdjustLightHue(currentLight, hueStep); // hueStep 是你已有的色相步长变量
handled = true;
}
else if (e.keyCode == KeyCode.P) // 色相-
{
AdjustLightHue(currentLight, -hueStep);
handled = true;
}
else if (e.keyCode == KeyCode.J) // 饱和度+
{
AdjustLightSaturation(currentLight, saturationStep); // saturationStep 是你已有的饱和度步长变量
handled = true;
}
else if (e.keyCode == KeyCode.K) // 饱和度-
{
AdjustLightSaturation(currentLight, -saturationStep);
handled = true;
}
// 如果旋钮是“按下即重复”,你可能需要处理 `KeyCode` 的持续按下状态。
// 但通常VIA映射的旋钮转动是快速连续发送多次KeyDown事件,这种方式足够用了。
第三步:测试与优化
基础测试:在VIA中设置好映射并“烧录”到键盘后,在Unity编辑器中打开一个文本字段,转动旋钮,看是否会输出 U I 等字符。这是验证映射成功的关键一步。
控制测试:打开你的灯光控制工具,选中一个灯光,在Scene视图获得焦点,转动旋钮测试控制是否生效。
优化手感(进阶):
调节步长:在脚本中调整 intensityStep, hueStep, saturationStep 的值,找到最跟手的调节速度。
利用VIA宏(可选):如果感觉旋钮转动一次发送的按键事件太多(导致变化过快),可以尝试在VIA中为旋钮动作设置宏命令。例如,录制的宏是“按下U -> 延迟10ms -> 抬起U”,这样能更精细地控制每次转动触发的事件频率。
解决冲突:检查新映射的按键(U/I/O等)是否与Unity编辑器或你的项目其他功能冲突,如有冲突,在VIA中更换其他按键即可。
本质是按键映射:这个方案的原理是“旋钮转动 -> 模拟按键按下 -> Unity脚本响应按键”,因此脚本端无需任何特殊硬件接口代码,只需监听对应的 KeyCode。
确保焦点在Scene视图:和之前一样,你的鼠标必须在Unity编辑器的Scene视图窗口中,按键(旋钮)事件才能被正确捕获。
与现有功能并存:这个修改是增量的,你原有的 +/-、H/J、[ / ] 等键盘快捷键会继续正常工作,两者互不影响。
按照这个流程,你应该可以顺利地将旋钮小键盘变成一盏实体化的灯光调节台。如果测试映射时按键不触发,或者控制效果不理想,可以随时告诉我,我们再一起看看是VIA设置还是脚本逻辑的问题。
// ... 你脚本中现有的按键检测代码(如H/J/K/L等)... // === 新增:旋钮按键检测 === else if (e.keyCode == KeyCode.U) // 强度+ { currentLight.intensity += intensityStep; // intensityStep 是你已有的强度步长变量 handled = true; } else if (e.keyCode == KeyCode.I) // 强度- { currentLight.intensity = Mathf.Max(0, currentLight.intensity - intensityStep); handled = true; } else if (e.keyCode == KeyCode.O) // 色相+ { AdjustLightHue(currentLight, hueStep); // hueStep 是你已有的色相步长变量 handled = true; } else if (e.keyCode == KeyCode.P) // 色相- { AdjustLightHue(currentLight, -hueStep); handled = true; } else if (e.keyCode == KeyCode.J) // 饱和度+ { AdjustLightSaturation(currentLight, saturationStep); // saturationStep 是你已有的饱和度步长变量 handled = true; } else if (e.keyCode == KeyCode.K) // 饱和度- { AdjustLightSaturation(currentLight, -saturationStep); handled = true; } // 如果旋钮是“按下即重复”,你可能需要处理 `KeyCode` 的持续按下状态。 // 但通常VIA映射的旋钮转动是快速连续发送多次KeyDown事件,这种方式足够用了。
第三步:测试与优化
核心要点与提醒
按照这个流程,你应该可以顺利地将旋钮小键盘变成一盏实体化的灯光调节台。如果测试映射时按键不触发,或者控制效果不理想,可以随时告诉我,我们再一起看看是VIA设置还是脚本逻辑的问题。
以下是完整脚本:
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
public class LightEditorTool : EditorWindow
{
private List<Light> targetLights = new List<Light>();
private int selectedLightIndex = 0;
private float intensityStep = 1.0f;
private bool isListeningToKeyboard = true;
// 颜色控制参数
private float hueStep = 0.005f;
private float saturationStep = 0.005f;
private Vector2 colorScrollPos;
// 新增:记录根父级对象(最初选中的容器)
private GameObject rootParentObject;

// 新增:强度步长循环数组和当前索引
private readonly float[] intensityStepValues = { 1.0f, 10.0f, 50.0f };
private int currentIntensityStepIndex = 0;
private static readonly Color[] colorPresets = new Color[]
{
Color.white,
new Color(1f, 0.9f, 0.7f),
new Color(0.8f, 0.9f, 1f),
new Color(1f, 0.95f, 0.9f),
Color.red,
Color.green,
Color.blue,
Color.yellow,
Color.cyan,
Color.magenta,
new Color(1f, 0.5f, 0.3f),
new Color(0.5f, 1f, 0.3f),
};
[MenuItem("Tools/灯光控制器 (步长切换版)")]
public static void ShowWindow()
{
GetWindow<LightEditorTool>("灯光控制器");
}
void OnEnable()
{
SceneView.duringSceneGui += DuringSceneGUI;
Selection.selectionChanged += OnSelectionChanged;
minSize = new Vector2(400, 500);
// 初始化时尝试刷新灯光列表
RefreshLightList();
}
void OnDisable()
{
SceneView.duringSceneGui -= DuringSceneGUI;
Selection.selectionChanged -= OnSelectionChanged;
}
// 修改:响应选择变化
void OnSelectionChanged()
{
// 如果当前没有根父级或用户手动选择了一个父级容器,则更新根父级
GameObject currentSelected = Selection.activeGameObject;
if (currentSelected != null)
{
// 检查选中的物体是否包含灯光
Light selectedLight = currentSelected.GetComponent<Light>();
bool hasLightInChildren = currentSelected.GetComponentInChildren<Light>() != null;
// 如果选中了灯光,但不更新根父级(除非用户手动点击了容器)
// 这里通过检查是否有子物体灯光来判断是否是容器
if (selectedLight == null && hasLightInChildren)
{
// 用户选中了一个包含灯光的容器,更新根父级
rootParentObject = currentSelected;
RefreshLightList();
}
else if (selectedLight != null)
{
// 用户选中了一个具体的灯光
// 只更新选中的灯光索引,但不改变根父级
UpdateSelectedLightIndex(currentSelected);
}
}
Repaint();
}
// 更新:基于根父级查找灯光
void RefreshLightList()
{
targetLights.Clear();
// 如果没有指定根父级,尝试查找当前选中的合适父级
if (rootParentObject == null)
{
GameObject currentSelected = Selection.activeGameObject;
if (currentSelected != null)
{
// 如果是灯光,向上查找包含多个灯光的父级
if (currentSelected.GetComponent<Light>() != null)
{
// 尝试找到父级容器
Transform parent = currentSelected.transform.parent;
while (parent != null)
{
Light[] lightsInParent = parent.GetComponentsInChildren<Light>(true);
if (lightsInParent.Length > 1)
{
rootParentObject = parent.gameObject;
break;
}
parent = parent.parent;
}
// 如果没找到合适的父级,使用直接父级
if (rootParentObject == null && currentSelected.transform.parent != null)
{
rootParentObject = currentSelected.transform.parent.gameObject;
}
}
else
{
// 选中的不是灯光,直接作为根父级
rootParentObject = currentSelected;
}
}
}
// 如果有根父级,查找所有灯光
if (rootParentObject != null)
{
Light[] lights = rootParentObject.GetComponentsInChildren<Light>(true);
foreach (Light light in lights)
{
if (light != null) targetLights.Add(light);
}
Debug.Log($"在 '{rootParentObject.name}' 下找到 {targetLights.Count} 个灯光");
// 更新选中索引
UpdateSelectedLightIndex(Selection.activeGameObject);
}
Repaint();
}
// 更新选中灯光索引
void UpdateSelectedLightIndex(GameObject selectedObject)
{
if (selectedObject != null && targetLights.Count > 0)
{
Light selectedLight = selectedObject.GetComponent<Light>();
if (selectedLight != null)
{
int index = targetLights.IndexOf(selectedLight);
if (index >= 0)
{
selectedLightIndex = index;
}
}
}
// 确保索引有效
if (selectedLightIndex >= targetLights.Count)
{
selectedLightIndex = Mathf.Max(0, targetLights.Count - 1);
}
}
void DuringSceneGUI(SceneView sceneView)
{
if (!isListeningToKeyboard || targetLights.Count == 0) return;
Event e = Event.current;
if (e.type == EventType.KeyDown)
{
if (selectedLightIndex < 0 || selectedLightIndex >= targetLights.Count) return;
Light currentLight = targetLights[selectedLightIndex];
if (currentLight == null) return;
bool handled = false;
// 强度控制
if (e.keyCode == KeyCode.Plus || e.keyCode == KeyCode.Equals ||
e.keyCode == KeyCode.KeypadPlus)
{
if (e.keyCode == KeyCode.Equals && !e.shift) { }
else
{
currentLight.intensity += intensityStep;
handled = true;
}
}
else if (e.keyCode == KeyCode.Minus || e.keyCode == KeyCode.KeypadMinus)
{
currentLight.intensity = Mathf.Max(0, currentLight.intensity - intensityStep);
handled = true;
}
// 色相控制
else if (e.keyCode == KeyCode.H)
{
AdjustLightHue(currentLight, hueStep);
handled = true;
}
else if (e.keyCode == KeyCode.J)
{
AdjustLightHue(currentLight, -hueStep);
handled = true;
}
// 饱和度控制
else if (e.keyCode == KeyCode.K)
{
AdjustLightSaturation(currentLight, saturationStep);
handled = true;
}
else if (e.keyCode == KeyCode.L)
{
AdjustLightSaturation(currentLight, -saturationStep);
handled = true;
}
// 灯光切换
else if (e.keyCode == KeyCode.LeftBracket)
{
SelectPreviousLight();
handled = true;
}
else if (e.keyCode == KeyCode.RightBracket)
{
SelectNextLight();
handled = true;
}
// 重置颜色
else if (e.keyCode == KeyCode.U)
{
currentLight.color = Color.white;
handled = true;
}
// 新增:切换强度步长 (I键)
else if (e.keyCode == KeyCode.I)
{
CycleIntensityStep();
handled = true;
}
if (handled)
{
e.Use();
EditorUtility.SetDirty(currentLight);
Repaint();
SceneView.RepaintAll();
}
}
}
// 新增:循环切换强度步长
void CycleIntensityStep()
{
currentIntensityStepIndex = (currentIntensityStepIndex + 1) % intensityStepValues.Length;
intensityStep = intensityStepValues[currentIntensityStepIndex];
Debug.Log($"强度调节步长已切换为: {intensityStep}");
}
void AdjustLightHue(Light light, float delta)
{
Color.RGBToHSV(light.color, out float h, out float s, out float v);
h = Mathf.Repeat(h + delta, 1f);
light.color = Color.HSVToRGB(h, s, v);
}
void AdjustLightSaturation(Light light, float delta)
{
Color.RGBToHSV(light.color, out float h, out float s, out float v);
s = Mathf.Clamp01(s + delta);
light.color = Color.HSVToRGB(h, s, v);
}
Vector3 GetColorHSV(Color color)
{
Color.RGBToHSV(color, out float h, out float s, out float v);
return new Vector3(h, s, v);
}
void SelectPreviousLight()
{
if (targetLights.Count == 0) return;
selectedLightIndex = (selectedLightIndex - 1 + targetLights.Count) % targetLights.Count;
// 在Hierarchy中选中这个灯光
if (targetLights[selectedLightIndex] != null)
{
Selection.activeObject = targetLights[selectedLightIndex].gameObject;
}
}
void SelectNextLight()
{
if (targetLights.Count == 0) return;
selectedLightIndex = (selectedLightIndex + 1) % targetLights.Count;
// 在Hierarchy中选中这个灯光
if (targetLights[selectedLightIndex] != null)
{
Selection.activeObject = targetLights[selectedLightIndex].gameObject;
}
}
void OnGUI()
{
GUILayout.Label("灯光控制器 - 步长切换版", EditorStyles.boldLabel);
EditorGUILayout.Space();
// 键盘监听开关
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
isListeningToKeyboard = EditorGUILayout.Toggle("启用键盘监听", isListeningToKeyboard);
// 修改:在快捷键说明中添加I键功能
EditorGUILayout.HelpBox(
"键盘快捷键:\n" +
" +/- : 强度调节\n" +
" H/J : 色相调节\n" +
" K/L : 饱和度调节\n" +
" [ ] : 切换灯光\n" +
" U : 重置颜色\n" +
$" I : 切换强度步长 (当前: {intensityStep})", // 显示当前步长
MessageType.Info
);
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
// 显示根父级信息
EditorGUILayout.LabelField("灯光组信息", EditorStyles.boldLabel);
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
if (rootParentObject != null)
{
EditorGUILayout.LabelField($"根父级: {rootParentObject.name}", EditorStyles.boldLabel);
EditorGUILayout.LabelField($"包含灯光数: {targetLights.Count}");
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("刷新列表"))
{
RefreshLightList();
}
if (GUILayout.Button("更换父级"))
{
// 清除当前根父级,让下一次选择确定新的父级
rootParentObject = null;
targetLights.Clear();
EditorGUILayout.HelpBox("请重新选择一个灯光组父级", MessageType.Info);
}
EditorGUILayout.EndHorizontal();
}
else
{
EditorGUILayout.HelpBox("请选择一个灯光组父级或包含灯光的物体", MessageType.Info);
if (GUILayout.Button("扫描场景所有灯光"))
{
// 备用方案:查找场景所有灯光
Light[] allLights = FindObjectsOfType<Light>();
targetLights.Clear();
foreach (Light light in allLights)
{
if (light != null) targetLights.Add(light);
}
if (targetLights.Count > 0)
{
rootParentObject = new GameObject("All Scene Lights");
EditorUtility.DisplayDialog("提示", $"找到了 {targetLights.Count} 个场景灯光", "确定");
}
}
}
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
// 灯光列表
if (targetLights.Count > 0)
{
EditorGUILayout.LabelField("灯光列表", EditorStyles.boldLabel);
for (int i = 0; i < targetLights.Count; i++)
{
if (targetLights[i] == null) continue;
EditorGUILayout.BeginHorizontal();
bool isSelected = (i == selectedLightIndex);
Color originalColor = GUI.backgroundColor;
if (isSelected)
{
GUI.backgroundColor = Color.green;
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
}
// 颜色预览
Rect colorRect = GUILayoutUtility.GetRect(20, 20, GUILayout.Width(20));
EditorGUI.DrawRect(colorRect, targetLights[i].color);
// 灯光名称和选择按钮
string buttonText = $"{targetLights[i].name}";
if (isSelected) buttonText = "▶ " + buttonText;
if (GUILayout.Button(buttonText, GUILayout.ExpandWidth(true)))
{
selectedLightIndex = i;
// 在Hierarchy中选中这个灯光
Selection.activeObject = targetLights[i].gameObject;
}
// 显示强度
EditorGUILayout.LabelField($"{targetLights[i].intensity:F1}", GUILayout.Width(40));
if (isSelected)
{
EditorGUILayout.EndVertical();
GUI.backgroundColor = originalColor;
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.Space();
// 当前选中灯光的详细控制
if (selectedLightIndex >= 0 && selectedLightIndex < targetLights.Count)
{
Light currentLight = targetLights[selectedLightIndex];
if (currentLight != null)
{
EditorGUILayout.LabelField($"当前控制: {currentLight.name}", EditorStyles.boldLabel);
// 强度控制
EditorGUILayout.LabelField("强度控制", EditorStyles.miniBoldLabel);
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
float newIntensity = EditorGUILayout.Slider("强度值", currentLight.intensity, 0, 10);
if (newIntensity != currentLight.intensity)
{
currentLight.intensity = newIntensity;
EditorUtility.SetDirty(currentLight);
}
// 已移除:+0.5, -0.5, 重置为1 等手动调节按钮
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
// 颜色控制
EditorGUILayout.LabelField("颜色控制", EditorStyles.miniBoldLabel);
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
// 当前颜色预览
Rect previewRect = GUILayoutUtility.GetRect(100, 20);
EditorGUI.DrawRect(previewRect, currentLight.color);
EditorGUI.LabelField(previewRect, " 当前颜色", EditorStyles.whiteBoldLabel);
// HSV值显示
Vector3 hsv = GetColorHSV(currentLight.color);
EditorGUILayout.LabelField($"色相: {hsv.x:F2}, 饱和度: {hsv.y:F2}, 明度: {hsv.z:F2}");
// 颜色拾取器
Color newColor = EditorGUILayout.ColorField("颜色选择", currentLight.color);
if (newColor != currentLight.color)
{
currentLight.color = newColor;
EditorUtility.SetDirty(currentLight);
}
// HSL控制滑块
EditorGUILayout.BeginHorizontal();
GUILayout.Label("色相", GUILayout.Width(60));
float newHue = GUILayout.HorizontalSlider(hsv.x, 0f, 1f);
if (newHue != hsv.x)
{
Color.RGBToHSV(currentLight.color, out float h, out float s, out float v);
currentLight.color = Color.HSVToRGB(newHue, s, v);
EditorUtility.SetDirty(currentLight);
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
GUILayout.Label("饱和度", GUILayout.Width(60));
float newSaturation = GUILayout.HorizontalSlider(hsv.y, 0f, 1f);
if (newSaturation != hsv.y)
{
Color.RGBToHSV(currentLight.color, out float h, out float s, out float v);
currentLight.color = Color.HSVToRGB(h, newSaturation, v);
EditorUtility.SetDirty(currentLight);
}
EditorGUILayout.EndHorizontal();
// 已移除:快速调整按钮组(色相+、色相-、饱和+、饱和-、重置白色)
EditorGUILayout.EndVertical();
}
}
}
else if (rootParentObject != null)
{
EditorGUILayout.HelpBox($"在 '{rootParentObject.name}' 下未找到灯光", MessageType.Warning);
}
EditorGUILayout.Space();
// 已移除:调节设置面板(包含步长设置的UI)
}
}
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删