Skip to content

Commit bb38389

Browse files
committed
Added "swap controls" button to rebinding sample to allow swapping left and right sticks.
1 parent fabe22a commit bb38389

File tree

2 files changed

+241
-1
lines changed

2 files changed

+241
-1
lines changed

Assets/Samples/RebindingUI/RebindActionUI.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,31 @@ public void ResetToDefault()
250250
UpdateBindingDisplay();
251251
}
252252

253+
/// <summary>
254+
/// Attempts to swap associated binding of this instance with another instance.
255+
/// </summary>
256+
/// <remarks>It is expected that the other control is of a compatible type.</remarks>
257+
/// <param name="other">The other instance to swap binding with.</param>
258+
/// <returns>true if successfully swapped, else false.</returns>
259+
public void Swap(RebindActionUI other)
260+
{
261+
if (this == other)
262+
return; // Silently ignore any request to swap binding with itself
263+
if (ongoingRebind != null || other.ongoingRebind != null)
264+
return; // Do not allow swapping with ongoing rebinding .
265+
266+
if (!ResolveActionAndBinding(out var action, out var bindingIndex))
267+
throw new Exception("Failed to resolve action and binding index");
268+
if (!other.ResolveActionAndBinding(out var otherAction, out var otherBindingIndex))
269+
throw new Exception("Failed to resolve action and binding index");
270+
271+
// Apply binding override to target binding
272+
var path = action.bindings[bindingIndex].path;
273+
var otherPath = otherAction.bindings[otherBindingIndex].path;
274+
action.ApplyBindingOverride(bindingIndex, otherPath);
275+
otherAction.ApplyBindingOverride(otherBindingIndex, path);
276+
}
277+
253278
/// <summary>
254279
/// Initiate an interactive rebind that lets the player actuate a control to choose a new binding
255280
/// for the action.

Assets/Samples/RebindingUI/RebindingUISampleScene.unity

Lines changed: 216 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1893,6 +1893,7 @@ RectTransform:
18931893
m_Children:
18941894
- {fileID: 356416661}
18951895
- {fileID: 331853782}
1896+
- {fileID: 725406578}
18961897
- {fileID: 720160215}
18971898
- {fileID: 982377776}
18981899
- {fileID: 852899507}
@@ -2437,6 +2438,141 @@ MonoBehaviour:
24372438
m_Script: {fileID: 11500000, guid: cc11d99b1a6682f4cbc4aba1fcd9ac5d, type: 3}
24382439
m_Name:
24392440
m_EditorClassIdentifier:
2441+
--- !u!1 &725406577
2442+
GameObject:
2443+
m_ObjectHideFlags: 0
2444+
m_CorrespondingSourceObject: {fileID: 0}
2445+
m_PrefabInstance: {fileID: 0}
2446+
m_PrefabAsset: {fileID: 0}
2447+
serializedVersion: 6
2448+
m_Component:
2449+
- component: {fileID: 725406578}
2450+
- component: {fileID: 725406581}
2451+
- component: {fileID: 725406580}
2452+
- component: {fileID: 725406579}
2453+
m_Layer: 5
2454+
m_Name: Swap
2455+
m_TagString: Untagged
2456+
m_Icon: {fileID: 0}
2457+
m_NavMeshLayer: 0
2458+
m_StaticEditorFlags: 0
2459+
m_IsActive: 1
2460+
--- !u!224 &725406578
2461+
RectTransform:
2462+
m_ObjectHideFlags: 0
2463+
m_CorrespondingSourceObject: {fileID: 0}
2464+
m_PrefabInstance: {fileID: 0}
2465+
m_PrefabAsset: {fileID: 0}
2466+
m_GameObject: {fileID: 725406577}
2467+
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
2468+
m_LocalPosition: {x: 0, y: 0, z: 0}
2469+
m_LocalScale: {x: 1, y: 1, z: 1}
2470+
m_ConstrainProportionsScale: 0
2471+
m_Children:
2472+
- {fileID: 1737030749}
2473+
m_Father: {fileID: 589143015}
2474+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
2475+
m_AnchorMin: {x: 0.5, y: 0}
2476+
m_AnchorMax: {x: 0.5, y: 0}
2477+
m_AnchoredPosition: {x: -23.19, y: 325.5}
2478+
m_SizeDelta: {x: 19.4, y: 17.7}
2479+
m_Pivot: {x: 0.5, y: 0.5}
2480+
--- !u!114 &725406579
2481+
MonoBehaviour:
2482+
m_ObjectHideFlags: 0
2483+
m_CorrespondingSourceObject: {fileID: 0}
2484+
m_PrefabInstance: {fileID: 0}
2485+
m_PrefabAsset: {fileID: 0}
2486+
m_GameObject: {fileID: 725406577}
2487+
m_Enabled: 1
2488+
m_EditorHideFlags: 0
2489+
m_Script: {fileID: 11500000, guid: 4e29b1a8efbd4b44bb3f3716e73f07ff, type: 3}
2490+
m_Name:
2491+
m_EditorClassIdentifier:
2492+
m_Navigation:
2493+
m_Mode: 3
2494+
m_WrapAround: 0
2495+
m_SelectOnUp: {fileID: 0}
2496+
m_SelectOnDown: {fileID: 0}
2497+
m_SelectOnLeft: {fileID: 0}
2498+
m_SelectOnRight: {fileID: 0}
2499+
m_Transition: 1
2500+
m_Colors:
2501+
m_NormalColor: {r: 0.25, g: 0.3071429, b: 0.45, a: 1}
2502+
m_HighlightedColor: {r: 0.36173913, g: 0.4408696, b: 0.65, a: 1}
2503+
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
2504+
m_SelectedColor: {r: 0.8679245, g: 0.4550042, b: 0.045033824, a: 1}
2505+
m_DisabledColor: {r: 0.21960784, g: 0.21960784, b: 0.25882354, a: 1}
2506+
m_ColorMultiplier: 1
2507+
m_FadeDuration: 0.1
2508+
m_SpriteState:
2509+
m_HighlightedSprite: {fileID: 0}
2510+
m_PressedSprite: {fileID: 0}
2511+
m_SelectedSprite: {fileID: 0}
2512+
m_DisabledSprite: {fileID: 0}
2513+
m_AnimationTriggers:
2514+
m_NormalTrigger: Normal
2515+
m_HighlightedTrigger: Highlighted
2516+
m_PressedTrigger: Pressed
2517+
m_SelectedTrigger: Selected
2518+
m_DisabledTrigger: Disabled
2519+
m_Interactable: 1
2520+
m_TargetGraphic: {fileID: 725406580}
2521+
m_OnClick:
2522+
m_PersistentCalls:
2523+
m_Calls:
2524+
- m_Target: {fileID: 1627715299}
2525+
m_TargetAssemblyTypeName: UnityEngine.InputSystem.Samples.RebindUI.RebindActionUI,
2526+
Unity.InputSystem.RebindingUI
2527+
m_MethodName: Swap
2528+
m_Mode: 2
2529+
m_Arguments:
2530+
m_ObjectArgument: {fileID: 720160216}
2531+
m_ObjectArgumentAssemblyTypeName: UnityEngine.InputSystem.Samples.RebindUI.RebindActionUI,
2532+
Unity.InputSystem.RebindingUI
2533+
m_IntArgument: 0
2534+
m_FloatArgument: 0
2535+
m_StringArgument:
2536+
m_BoolArgument: 0
2537+
m_CallState: 2
2538+
--- !u!114 &725406580
2539+
MonoBehaviour:
2540+
m_ObjectHideFlags: 0
2541+
m_CorrespondingSourceObject: {fileID: 0}
2542+
m_PrefabInstance: {fileID: 0}
2543+
m_PrefabAsset: {fileID: 0}
2544+
m_GameObject: {fileID: 725406577}
2545+
m_Enabled: 1
2546+
m_EditorHideFlags: 0
2547+
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
2548+
m_Name:
2549+
m_EditorClassIdentifier:
2550+
m_Material: {fileID: 0}
2551+
m_Color: {r: 1, g: 1, b: 1, a: 1}
2552+
m_RaycastTarget: 1
2553+
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
2554+
m_Maskable: 1
2555+
m_OnCullStateChanged:
2556+
m_PersistentCalls:
2557+
m_Calls: []
2558+
m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0}
2559+
m_Type: 1
2560+
m_PreserveAspect: 0
2561+
m_FillCenter: 1
2562+
m_FillMethod: 4
2563+
m_FillAmount: 1
2564+
m_FillClockwise: 1
2565+
m_FillOrigin: 0
2566+
m_UseSpriteMesh: 0
2567+
m_PixelsPerUnitMultiplier: 1
2568+
--- !u!222 &725406581
2569+
CanvasRenderer:
2570+
m_ObjectHideFlags: 0
2571+
m_CorrespondingSourceObject: {fileID: 0}
2572+
m_PrefabInstance: {fileID: 0}
2573+
m_PrefabAsset: {fileID: 0}
2574+
m_GameObject: {fileID: 725406577}
2575+
m_CullTransparentMesh: 0
24402576
--- !u!1001 &736025223
24412577
PrefabInstance:
24422578
m_ObjectHideFlags: 0
@@ -3895,7 +4031,7 @@ GameObject:
38954031
m_Icon: {fileID: 0}
38964032
m_NavMeshLayer: 0
38974033
m_StaticEditorFlags: 0
3898-
m_IsActive: 1
4034+
m_IsActive: 0
38994035
--- !u!224 &1014357782
39004036
RectTransform:
39014037
m_ObjectHideFlags: 0
@@ -6591,6 +6727,85 @@ MonoBehaviour:
65916727
m_Script: {fileID: 11500000, guid: cc11d99b1a6682f4cbc4aba1fcd9ac5d, type: 3}
65926728
m_Name:
65936729
m_EditorClassIdentifier:
6730+
--- !u!1 &1737030748
6731+
GameObject:
6732+
m_ObjectHideFlags: 0
6733+
m_CorrespondingSourceObject: {fileID: 0}
6734+
m_PrefabInstance: {fileID: 0}
6735+
m_PrefabAsset: {fileID: 0}
6736+
serializedVersion: 6
6737+
m_Component:
6738+
- component: {fileID: 1737030749}
6739+
- component: {fileID: 1737030751}
6740+
- component: {fileID: 1737030750}
6741+
m_Layer: 5
6742+
m_Name: Text
6743+
m_TagString: Untagged
6744+
m_Icon: {fileID: 0}
6745+
m_NavMeshLayer: 0
6746+
m_StaticEditorFlags: 0
6747+
m_IsActive: 1
6748+
--- !u!224 &1737030749
6749+
RectTransform:
6750+
m_ObjectHideFlags: 0
6751+
m_CorrespondingSourceObject: {fileID: 0}
6752+
m_PrefabInstance: {fileID: 0}
6753+
m_PrefabAsset: {fileID: 0}
6754+
m_GameObject: {fileID: 1737030748}
6755+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
6756+
m_LocalPosition: {x: 0, y: 0, z: 0}
6757+
m_LocalScale: {x: 1, y: 1, z: 1}
6758+
m_ConstrainProportionsScale: 0
6759+
m_Children: []
6760+
m_Father: {fileID: 725406578}
6761+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
6762+
m_AnchorMin: {x: 0, y: 0}
6763+
m_AnchorMax: {x: 1, y: 1}
6764+
m_AnchoredPosition: {x: 0.4999999, y: 0.5}
6765+
m_SizeDelta: {x: -1.0000002, y: 1}
6766+
m_Pivot: {x: 0.5, y: 0.5}
6767+
--- !u!114 &1737030750
6768+
MonoBehaviour:
6769+
m_ObjectHideFlags: 0
6770+
m_CorrespondingSourceObject: {fileID: 0}
6771+
m_PrefabInstance: {fileID: 0}
6772+
m_PrefabAsset: {fileID: 0}
6773+
m_GameObject: {fileID: 1737030748}
6774+
m_Enabled: 1
6775+
m_EditorHideFlags: 0
6776+
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
6777+
m_Name:
6778+
m_EditorClassIdentifier:
6779+
m_Material: {fileID: 0}
6780+
m_Color: {r: 0.8901961, g: 0.8901961, b: 0.8901961, a: 1}
6781+
m_RaycastTarget: 1
6782+
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
6783+
m_Maskable: 1
6784+
m_OnCullStateChanged:
6785+
m_PersistentCalls:
6786+
m_Calls: []
6787+
m_FontData:
6788+
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
6789+
m_FontSize: 14
6790+
m_FontStyle: 0
6791+
m_BestFit: 0
6792+
m_MinSize: 10
6793+
m_MaxSize: 40
6794+
m_Alignment: 4
6795+
m_AlignByGeometry: 0
6796+
m_RichText: 1
6797+
m_HorizontalOverflow: 0
6798+
m_VerticalOverflow: 0
6799+
m_LineSpacing: 1
6800+
m_Text: "\u21C5"
6801+
--- !u!222 &1737030751
6802+
CanvasRenderer:
6803+
m_ObjectHideFlags: 0
6804+
m_CorrespondingSourceObject: {fileID: 0}
6805+
m_PrefabInstance: {fileID: 0}
6806+
m_PrefabAsset: {fileID: 0}
6807+
m_GameObject: {fileID: 1737030748}
6808+
m_CullTransparentMesh: 0
65946809
--- !u!1 &1801770802
65956810
GameObject:
65966811
m_ObjectHideFlags: 0

0 commit comments

Comments
 (0)