OnScreenItem

v4.0

The OnScreenItem class manages visual items that are "held" by the player between inventories. This is how a player may transfer items from one inventory to another, or even put the item into the 3D world.

This class is attached to the Prefab Inventory Module Requires Scripts. Bring that into your project.

This is a static method, which you can utilize via this code at the top of your script:

using static InfinityPBR.Modules.Inventory.OnScreenItem;

Required fields

UICamera

This is the UI Camera which draws the UI and UI Inventory objects. The prefab is set up for you, and utilized in the demo scene. If you make changes, be sure to create your own original prefab first, so that you can reference the demo project later.

Player Transform

This is generally the transform of your player, and is used to instantiate UI Inventory objects into the 3D world, so a player can "drop" a held object into the world.

You are able to modify the code in order to instantiate the item in different locations, or have different logic than what is provided. For example, you may wish to allow a player to place a held object onto a specific location, rather than simply throwing it forward. For that functionality, some custom code would be required.

Held Object Parent Rect Transform

This is the UI parent for on screen held objects.

Z Position

This is the z position that UI Inventory objects will be instantiated at. Generally this can be left at default, though can be modified if needs be.

Drop Force

When the player "drops" the item into the world, some force is applied. You can modify the force here. You are also able to change the code itself to provide a different behavior when the object is dropped.

Public Properties & Variables

GameItemObject & GameObject

When an item is being held, there will be both a GameItemObject object, which holds all the data that makes up what the object is, and a GameObject, the physical object in the scene the player can see.

public GameItemObject itemHeld;
public GameObject itemHeldObject;

Item Details

These properties and variables provide more information about an item that is held -- or if one is being held at all.

public bool ItemIsHeld // True if there is a held item
public int ItemHeight // Inventory Grid Height -- # of grid spaces
public int ItemWidth // Inventory Grid Width -- # of grid spaces

public bool itemHeldIsQuestItem; // The "Quest Item" toggle in the ItemObject module

public bool canDropQuestItem = false; // If true, questItems can be dropped

Public methods

Drop Position Modifications

When the held item is "dropped", it will be put at the playerTransform.position + dropPositionModification + playerTransform.forward. Set this value of dropPositionModification in the Inspector.

When DropObject() is called, there are additional optional parameters to specify the position and "look at" another position. This is useful when you want the object to spawn elsewhere.

// Example use of "DropObject()" in it's default form
onScreenItem.DropObject(); // Drops the object in front of the playerTransform

// The Drop Object method
public bool DropObject(bool ignoreButtons = false, bool ignoreQuestItems = false
            , bool spawnAtCustomPosition = false
            , Vector3 customSpawnPosition = default
            , bool lookAtNewPosition = false
            , Vector3 dropLookAtPosition = default
            , bool customForce = false
            , float customForceAmount = 10f)
            
// Example use of "DropObject()" with a custom position
Transform otherTransform;
onScreenItem.DropObject(false, false
            , true, otherTransform.position
            , true, otherTransform.forward);

By default, the DropObject() method will not drop a GameItemObject which is marked as a questItem. You can set ignoreQuestItems = true to bypass this. Keep in mind, once it's thrown in the world, physics may make it go out of reach of the player!

The ignoreButtons bool is also false by default. This means that if the mouse is over a UI button, the DropObject() method will be ignored.

The player may have moused over a portrait of a character or other UI element that does something different when there is an item held.

if (EventSystem.current.IsPointerOverGameObject() && !ignoreButtons)
    return false;

Give Held Object

You likely will not use this method!

This is called by the Panel class to pass a held item to a specific GameItemObjectList which is enabled with Spots, at a specific spotRow and spotColumn. While you generally will not be using this, it can be used if you have a reason to do so.

// This version specifies the spot row/column. If it can't fit at that row/column, 
// it will return false. Generally you'd only call this version if you already knew
// the item would fit in that spot.
public bool GiveHeldObject(Spots spots
                , GameItemObjectList gameItemObjectList
                , int spotRow
                , int spotColumn)

// Version which does not specify the spots. If the held item can not be put into
// the inventory (i.e. there are no spots available), then this will return false
// and the heldItem will NOT be removed. Useful for passing an item to an object that
// has inventory, when you don't really care at all where it goes.
public bool GiveHeldObject(Spots spots
                , GameItemObjectList gameItemObjectList)
                
// Example use from Panel.cs
onScreenItem.GiveHeldObject(spots, InventoryItems, rowN, columnN);

Pickup an object

This method will pickup an object, but will return false if the system is unable to.

First, if there is already a held item, the system will return false if it can't place the existing item into the AcitvePanel, or if there is no active panel available.

The method will also return false if there is no prefabInventory object on the gameItemObject being picked up.

After that, the item will be picked up. The new itemHeld will be a Clone() of the gameItemObject passed in.

public bool Pickup(GameItemObject gameItemObject)

If there is a physical object that needs to be turned off, don't forget to do so after Pickup() returns true!

// Example from panel.cs
if (!onScreenItem.Pickup(gameItemObject)) return;
RemoveItemFromInventoryAndSpots(gameItemObject);

Last updated