UIS: How to assign Item to Inventory in Editor Mode via script?

Kirshor

Member
I can set item to an Inventory in Play Mode via script but I can not do that in the Editor mode.
I would like to create a tool in Editor that auto set item to a prefab so I need a solution for that.
Help me please
 

Attachments

  • Screen Shot 2021-04-16 at 20.12.20.png
    Screen Shot 2021-04-16 at 20.12.20.png
    20.4 KB · Views: 5
Sorry I missed your question last time.

Could you explain in more detail what you are trying to achieve I'm not sure what you mean.

What do you mean by not being able to set an item in editor mode? You should be able to click on the ItemAmount within the list and then press the "change" button to set the item.
1619079995431.png

What do you mean by setting an Item Prefab?
You can set prefabs on an Item by creating an Attribute within the Inventory Manager Editor for that Item Category and then set a prefab on it.
If you haven't done so yet, you should have a look at some of those video tutorials:
Especially the Equipping Clothes & Weapons one, I assume that's what you are trying to do.

Also you might want to read through the documentation too:
 
I meant set an item in Editor mode via script:
var myItem = InventorySystemManager.CreateItem(m_PartType);

var inv = m_GO_Pickup.GetComponent<Inventory>();
if (inv == null)
Debug.LogError("Inventory NULL");

Debug.Log("GetItemCollectionCount=" + inv.GetItemCollectionCount());

var collection = inv.GetItemCollection(ItemCollectionPurpose.Main);
collection.AddItem(myItem);

I got errors:

NullReferenceException: Object reference not set to an instance of an object


Opsive.UltimateInventorySystem.Core.InventorySystemManager.CreateItem (Opsive.UltimateInventorySystem.Core.ItemDefinition itemDefinition, System.Collections.Generic.IReadOnlyList`1[T] attributes) (at Assets/Opsive/UltimateInventorySystem/Scripts/Core/InventorySystemManager.cs:109)


KCombineItemHelper.AddItemToInventory () (at Assets/KienAsset/AllLibs/KLib4/CraftOnHand/KCombineItemHelper_CreatePick.cs:117)


System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <eae584ce26bc40229c1b1aa476bfa589>:0)


Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.


System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <eae584ce26bc40229c1b1aa476bfa589>:0)


System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <eae584ce26bc40229c1b1aa476bfa589>:0)


Sirenix.OdinInspector.Editor.Drawers.DefaultMethodDrawer.InvokeMethodInfo (System.Reflection.MethodInfo methodInfo) (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/Misc Drawers/DefaultMethodDrawer.cs:514)


UnityEngine.Debug:LogException(Exception)


Sirenix.OdinInspector.Editor.Drawers.DefaultMethodDrawer:InvokeMethodInfo(MethodInfo) (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/Misc Drawers/DefaultMethodDrawer.cs:539)


Sirenix.OdinInspector.Editor.Drawers.DefaultMethodDrawer:InvokeButton() (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/Misc Drawers/DefaultMethodDrawer.cs:415)


Sirenix.OdinInspector.Editor.Drawers.DefaultMethodDrawer:DrawNormalButton() (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/Misc Drawers/DefaultMethodDrawer.cs:272)


Sirenix.OdinInspector.Editor.Drawers.DefaultMethodDrawer:DrawPropertyLayout(GUIContent) (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/Misc Drawers/DefaultMethodDrawer.cs:184)


Sirenix.OdinInspector.Editor.OdinDrawer:DrawProperty(GUIContent) (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/OdinDrawer.cs:109)


Sirenix.OdinInspector.Editor.InspectorProperty:Draw(GUIContent) (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/InspectorProperty.cs:770)


Sirenix.OdinInspector.Editor.Drawers.TabGroupAttributeDrawer:DrawPropertyLayout(GUIContent) (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/Group Drawers/TabGroupAttributeDrawer.cs:154)


Sirenix.OdinInspector.Editor.OdinDrawer:DrawProperty(GUIContent) (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/OdinDrawer.cs:109)


Sirenix.OdinInspector.Editor.InspectorProperty:Draw(GUIContent) (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/InspectorProperty.cs:770)


Sirenix.OdinInspector.Editor.InspectorProperty:Draw() (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/InspectorProperty.cs:658)


Sirenix.OdinInspector.Editor.Drawers.UnityObjectRootDrawer`1:DrawPropertyLayout(GUIContent) (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/Value Drawers/UnityObjectRootDrawer.cs:25)


Sirenix.OdinInspector.Editor.OdinDrawer:DrawProperty(GUIContent) (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/OdinDrawer.cs:109)


Sirenix.OdinInspector.Editor.InspectorProperty:Draw(GUIContent) (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/InspectorProperty.cs:770)


Sirenix.OdinInspector.Editor.PropertyTree:DrawProperties() (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/PropertyTree.cs:485)


Sirenix.OdinInspector.Editor.PropertyTree:Draw(Boolean) (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/PropertyTree.cs:389)


Sirenix.OdinInspector.Editor.OdinEditor:DrawTree() (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/OdinEditor.cs:93)


Sirenix.OdinInspector.Editor.OdinEditor:DrawOdinInspector() (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/OdinEditor.cs:216)


Sirenix.OdinInspector.Editor.OdinEditor:OnInspectorGUI() (at X:/Repositories/sirenix-development/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/OdinEditor.cs:85)


UnityEngine.GUIUtility:processEvent(Int32, IntPtr) (at /Users/bokken/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:197)
 
Ah I see that makes more sense.

So the Inventory System Manager is meant to be used at runtime to manage all existing Items and other Inventory Objects that exist.
It's not meant to be used at edit time.

The second issue is that the Inventory serializes the ItemCollection with a custom serializer that we made, so you must deserialize the data before you may start adding items. And you'll need to make sure to serialize the data manually and dirtying the object that the inventory component is attached to.

So you'll need to start with something like this:
Code:
if (!Application.isPlaying) { m_Inventory.Initialize(true); }
That will deserialize the ItemCollections so you'll be able to edit them.

And you'll want to end with a
Code:
m_Inventory.Serialize();
Shared.Editor.Utility.EditorUtility.SetDirty(m_Inventory);

To Create the Item you'll need to know the ItemDefinition. The complication is that all Item.Create functions are internal. We do this to prevent people from creating items without the Inventory System Manager knowing at runtime.

So the only way to create Items at Edit Time without changing the source code is to use the Editor script called "ItemField" static "CreateItem" function.
Code:
var newItem = Opsive.UltimateInventorySystem.Editor.VisualElements.ItemField.CreateItem(itemDefinition);

Make sure to use the assembly definitions to have access to the Editor classes and functions.

Let me know if that helps, I can add more editor static functions to create items with different parameters if you wish for a future version
I hope that helps
 
Static methob: ItemField.CreateItem was not found in source code.
So I try as below: No error but Item was not added to the collection

var inv = GetComponent<Inventory>();
inv.Initialize(true);
//++++++++++++
var newItem = new Opsive.UltimateInventorySystem.Editor.
VisualElements.ItemField(m_Data);
var item = newItem.Value;

var collection = inv.GetItemCollection(ItemCollectionPurpose.Main);
collection.AddItem(item);
//++++++++
inv.Serialize();
Opsive.Shared.Editor.Utility.EditorUtility.SetDirty(inv);
 
Make sure you have the latest UIS v1.1.7 Check the version on our Editor Manager in the bottom left of the window.
The Create Item function should exist in the latest version. It is at the very top of the ItemField script:

Code:
public static Item CreateItem(ItemDefinition definition)
{
    return Item.Create(definition);
}

The reason the item isn't being added with the code you have above is because itemField.Value is null by default. ItemCollection do not allow null items to be added usually.
 
I found ItemField.CreateItem after update the latest UIS
Now my code is:

var inv = GetComponent<Inventory>();
inv.Initialize(true);
//++++++++++++
var item= Opsive.UltimateInventorySystem.Editor.
VisualElements.ItemField.CreateItem(m_ItemDef);

var collection = inv.GetItemCollection(ItemCollectionPurpose.Main);
collection.AddItem(item);// => The error occurred here

//++++++++
inv.Serialize();
Opsive.Shared.Editor.Utility.EditorUtility.SetDirty(inv);
I Got error m_ItemCollectionRestrictions=null in:

public virtual ItemInfo? AddCondition(ItemInfo itemInfo, ItemCollection receivingCollection)
{
for (int i = 0; i < m_ItemCollectionRestrictions.Count; i++) {
var result = m_ItemCollectionRestrictions.AddCondition(itemInfo, receivingCollection);
if (result.HasValue == false) { return null; }


Log:
NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateInventorySystem.Core.InventoryCollections.Inventory.AddCondition (Opsive.UltimateInventorySystem.Core.DataStructures.ItemInfo itemInfo, Opsive.UltimateInventorySystem.Core.InventoryCollections.ItemCollection receivingCollection) (at Assets/Opsive/UltimateInventorySystem/Scripts/Core/InventoryCollections/Inventory.cs:288)
Opsive.UltimateInventorySystem.Core.InventoryCollections.ItemCollection.AddItemCondition (Opsive.UltimateInventorySystem.Core.DataStructures.ItemInfo itemInfo) (at Assets/Opsive/UltimateInventorySystem/Scripts/Core/InventoryCollections/ItemCollection.cs:476)
 
Ah that's a good point, I'm sorry I forgot to mention that the ItemCollection stores the Item amounts inside the "DefaultLoadout" field at edit time.
The collection.AddItem(item) function is meant to be used only at runtime.

To set items at edit time you can do that instead:

Code untested
Code:
if(collection.DefaultLoadout == null){
collection.DefaultLoadout = new ItemAmounts();
}

collection.DefaultLoadout.Add(itemAmount)

Check out the ItemCollectionField and ItemCollection script to get an idea of how the DefaultLoadout field is used.
 
It worked.
There is 1 small question. Is there any function that forces inventory collection repaint after adding an item to it? Currently, I must deselect the current inventory object, then select it again to able to view the added item.
Thank you very much.
 
Top