I want to update an Inventory Item's main graphic at runtime. The item is a Flask, and it may be empty or full of water. I want to show a different icon when it's empty and when it's full. I've been able to change the texture variable by calling:KickStarter.inventoryManager.GetItem(1).tex = textureFull;
However, I'm using Unity UI and the UI does not update the image. I've tried lots of things, even tried calling UISlot.SetImage and that did not even work.
How can I update an Inventory Item's Main Graphic variable at runtime and also update the Unity UI to reflect the change?
Comments
Replace "inventoryManager" with "runtimeInventory" and it should work.
However, it may be easier to split it into two distinct "full" and "empty" items. You can use the runtimeInventory script's Replace method (or Inventory: Add or remove Action) to swap them around as needed.
However, with UI there is another step involved - apologies for missing it before. Since inventory textures are converted to sprites in UI mode, they are cached in memory and only reset when necessary. This can be forced manually, however, with:
AC.PlayerMenus.ResetInventoryBoxes ();
Alternatively, you may wish to look into using Render Textures. An item's texture can be set to be a render texture, so you could also concievably have a separate camera view a plane with the new texture on it.
Two things:
1) You don't need to deal with sprites - lines 14 and 15 can be removed
2) The photo only runs if snapPhoto is 1, but is 0 by default - are you setting it to 1 somewhere else?
I tested your code without the above and it worked fine for me - both with Unity UI and AC-based Inventory menus. What are your AC and Unity versions?
It could be a platform/Unity issue in that case, since the code is solid so far as I can tell. If it only works for you with saved asset files, it might be worth saving the texture to disk before assinging it to the inventory - a step necessary anyway if you want the change to be reflected in save-game files.
When loading a save game, you'll then need to load in this same texture and re-apply it to the inventory. Likely you should also set a global Boolean variable too so that you can set whether or not this new texture should be loaded.
To run code after loading a save, hook into the OnFinishLoading custom event. This event runs after a succesful load, so this is when to run the code that checks the bool's value, retrieves the file and applies it to the item:
http://pasteall.org/773831/csharp
(Where "3" is the ID number of the global boolean, which must be true for the code to run)
Hi, I know this is an old post but it covers closely what I would like to do. Unfortunately the pasteall screenshots are not there anymore.
I want the player to take screenshots and then to be able to scroll through these using thumbnails. (Would be a fantastic function in AC!) I've chosen to use Documents (without text) for this.
When clicking on the Mobile Phone in the lower left, Camera shutter button and an inventory box with Document (textures) appear. See link below.
The wanted result is that when clicking on the Camera Shutter, a screenshot will be taken, which will be assigned to the first Document item without a texture, and appear last in the inventory box with thumbnails.
The Camera Shutter button is called through a custom action (Takescreenshot.cs), using the AC Actiontemplate. Screenshot of the short code in link below.
To take the screenshot, I have copied the code from SaveSystem.cs line 985-1050 and put this before Run() in the custom action script.
When clicking on the Camera shutter, the game freezes and two error messages appear, see link below.
In my setup, I want to put this as a Document texture, but with the ickStarter.runtimeInventory.GetItem(1).tex = ScreenShot code it may end up as an Inventory item texture?
If there is a better way to treat ingame screenshots please let me know. For now I am tearing my hair to get this working...
Thanks for any help I can get.
https://imgur.com/a/Il6yI25
Welcome to the community, @Magnus. There are a couple of issues to tackle, here.
Screenshot-taking
The message that appears is a Unity error, and will display if you attempt to read the contents of the screen before the end of the frame. This is handled in a coroutine, after calling WaitForEndOfFrame. You can find an example of this in the SaveSystem script's PrepareSaveCoroutine function, just before GetScreenshotTexture is called.
Actions aren't MonoBehaviours, however, so you'll need to move your code to a regular C# script, and not rely on a custom Action for this.
Place your script's GetScreenshotTexture inside a new C# script, and then also paste in the following:
To run this code when the ActionList is run, attach your script to a new empty GameObject in the scene, prefab it, and then remove the original from the scene. Then, use AC's Object: Call event Action to call its CreateTexture function. Be sure to have the Action reference the prefab, and not the original script asset.
If successful, you'll get a new message appear in the Console reporting so.
Icon updating
The code you're currently running does indeed affect the Inventory - GetItem returns an item that the Player is currently carrying.
However, I'd recommend using Inventory items over Documents, as these are better equipped to handle such customisation.
If you want to display photos in a separate Inventory menu, you can do so by defining a pair of categories in the Inventory Manager's Categories tab (e.g. "Normal" and "Photos"). Move your "regular" items to the Normal category, and then create some placeholder items in the Photos category.
In your various InventoryBox menu elements, you can then check Limit by category? to limit their display to items in specific categories.
Since this thread began, AC's been updated to make it much easier to override an item's texture through script - an InvInstance class's Tex property can now be set. I can assist with handling this, but see how you go with the above first, as we'll need to be sure everything else is working beforehand.
Superthanks for the rapid reply!!
I did, I think, what you described under Screenshot-taking although I get the error message Coroutine couldn't be started because the the game object 'Take Screenshot' is inactive!
https://imgur.com/a/fAtK53N
Will use Inventory items. I already have the two categories but were not sure what I could do with them.
Screenshot-taking
Ah, quite right. Sorry - the script will need to be in the scene for it to work after all.
Attach a Constant ID component to the prefab and check Retain in prefab?. Then, place the prefab back in the scene, and use the Object: Send message Action instead. Use this Action to reference the scene object, set the Message to send to Custom, and type in "CreateTexture" to run that function.
This object will need to be in each scene of your game - you can either drop the prefab in manually, or attach it to a persistent object such as your Player prefab.
Icon updating
If you have dummy items set up with a dedicated category in the Inventory Manager, you can attach this code to the end of your CreateTextureCo function:
This will go through the Inventory Manager, and find the first item in the category that isn't currently held by the Player. It'll then add it to the Player's Inventory, overriding its texture with the screenshot.