Created
May 24, 2022 19:00
-
-
Save thestonefox/f604c6e1681117752ca1ac633f30656e to your computer and use it in GitHub Desktop.
Tilia Interactables Example of how to get two handed grabbing to work with a precision grabbed object so either hand can be holding it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using Tilia.Interactions.Interactables.Interactables; | |
using Tilia.Interactions.Interactables.Interactables.Grab.Provider; | |
using UnityEngine; | |
using Zinnia.Data.Collection.Stack; | |
public class PrecisionFollowPrimarySecondarySwapper : MonoBehaviour | |
{ | |
[SerializeField] | |
private InteractableFacade interactable; | |
public InteractableFacade Interactable | |
{ | |
get { return interactable; } | |
set | |
{ | |
UnregisterListener(interactable); | |
interactable = value; | |
RegisterListener(interactable); | |
} | |
} | |
[SerializeField] | |
private int stackIndex; | |
public int StackIndex | |
{ | |
get { return stackIndex; } | |
set { stackIndex = value; } | |
} | |
protected Coroutine swapRoutine; | |
protected virtual void OnEnable() | |
{ | |
RegisterListener(Interactable); | |
} | |
protected virtual void OnDisable() | |
{ | |
StopSwapRoutine(); | |
UnregisterListener(Interactable); | |
} | |
protected virtual void RegisterListener(InteractableFacade data) | |
{ | |
GameObjectObservableStack stack = GetStack(data); | |
if (stack == null) | |
{ | |
return; | |
} | |
stack.ElementEvents[1].ForcePopped.AddListener(Swap); | |
} | |
protected virtual void UnregisterListener(InteractableFacade data) | |
{ | |
GameObjectObservableStack stack = GetStack(data); | |
if (stack == null) | |
{ | |
return; | |
} | |
stack.ElementEvents[1].ForcePopped.RemoveListener(Swap); | |
} | |
protected virtual GameObjectObservableStack GetStack(InteractableFacade input) | |
{ | |
if (input == null) | |
{ | |
return null; | |
} | |
GrabInteractableStackInteractorProvider provider = (GrabInteractableStackInteractorProvider)input.Configuration.GrabConfiguration.GrabProviderOptions[StackIndex]; | |
return provider != null && provider.EventStack.ElementEvents.Count > 1 ? provider.EventStack : null; | |
} | |
protected virtual void Swap(GameObject interactor) | |
{ | |
StopSwapRoutine(); | |
swapRoutine = StartCoroutine(SwapAtEndOfFrame(interactor)); | |
} | |
protected virtual void StopSwapRoutine() | |
{ | |
if (swapRoutine != null) | |
{ | |
StopCoroutine(swapRoutine); | |
swapRoutine = null; | |
} | |
} | |
protected virtual IEnumerator SwapAtEndOfFrame(GameObject interactor) | |
{ | |
yield return new WaitForEndOfFrame(); | |
Interactable.Grab(interactor); | |
swapRoutine = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment