//------------------------------------------------- private void RemoveMatchingItemsFromHandStack(ItemPackage package, Hand hand) { for (int i = 0; i < hand.AttachedObjects.Count; i++) { ItemPackageReference packageReference = hand.AttachedObjects[i].attachedObject.GetComponent <ItemPackageReference>(); if (packageReference != null) { ItemPackage attachedObjectItemPackage = packageReference.itemPackage; if ((attachedObjectItemPackage != null) && (attachedObjectItemPackage == package)) { GameObject detachedItem = hand.AttachedObjects[i].attachedObject; hand.DetachObject(detachedItem); } } } }
//------------------------------------------------- private ItemPackage GetAttachedItemPackage(Hand hand) { GameObject currentAttachedObject = hand.currentAttachedObject; if (currentAttachedObject == null) // verify the hand is holding something { return(null); } ItemPackageReference packageReference = hand.currentAttachedObject.GetComponent <ItemPackageReference>(); if (packageReference == null) // verify the item in the hand is matchable { return(null); } ItemPackage attachedItemPackage = packageReference.itemPackage; // return the ItemPackage reference we find. return(attachedItemPackage); }
//------------------------------------------------- private void OnHandHoverBegin(Hand hand) { ItemPackage currentAttachedItemPackage = GetAttachedItemPackage(hand); if (currentAttachedItemPackage == itemPackage) // the item at the top of the hand's stack has an associated ItemPackage { if (takeBackItem && !requireTriggerPressToReturn) // if we want to take back matching items and aren't waiting for a trigger press { TakeBackItem(hand); } } if (!requireTriggerPressToTake) // we don't require trigger press for pickup. Spawn and attach object. { SpawnAndAttachObject(hand); } if (requireTriggerPressToTake && showTriggerHint) { ControllerButtonHints.ShowTextHint(hand, EVRButtonId.k_EButton_SteamVR_Trigger, "PickUp"); } }
//------------------------------------------------- private void SpawnAndAttachObject(Hand hand) { if (hand.otherHand != null) { //If the other hand has this item package, take it back from the other hand ItemPackage otherHandItemPackage = GetAttachedItemPackage(hand.otherHand); if (otherHandItemPackage == itemPackage) { TakeBackItem(hand.otherHand); } } if (showTriggerHint) { ControllerButtonHints.HideTextHint(hand, EVRButtonId.k_EButton_SteamVR_Trigger); } if (itemPackage.otherHandItemPrefab != null) { if (hand.otherHand.hoverLocked) { //Debug.Log( "Not attaching objects because other hand is hoverlocked and we can't deliver both items." ); return; } } // if we're trying to spawn a one-handed item, remove one and two-handed items from this hand and two-handed items from both hands if (itemPackage.packageType == ItemPackage.ItemPackageType.OneHanded) { RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.OneHanded, hand); RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.TwoHanded, hand); RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.TwoHanded, hand.otherHand); } // if we're trying to spawn a two-handed item, remove one and two-handed items from both hands if (itemPackage.packageType == ItemPackage.ItemPackageType.TwoHanded) { RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.OneHanded, hand); RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.OneHanded, hand.otherHand); RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.TwoHanded, hand); RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.TwoHanded, hand.otherHand); } spawnedItem = GameObject.Instantiate(itemPackage.itemPrefab); spawnedItem.SetActive(true); hand.AttachObject(spawnedItem, attachmentFlags, attachmentPoint); if ((itemPackage.otherHandItemPrefab != null) && (hand.otherHand.controller != null)) { GameObject otherHandObjectToAttach = GameObject.Instantiate(itemPackage.otherHandItemPrefab); otherHandObjectToAttach.SetActive(true); hand.otherHand.AttachObject(otherHandObjectToAttach, attachmentFlags); } itemIsSpawned = true; justPickedUpItem = true; if (takeBackItem) { useFadedPreview = true; pickupEvent.Invoke(); CreatePreviewObject(); } }