Adventure Creator Wikia
Advertisement

By default, an inventory item will show the same texture when displayed in Menus, regardless of how many of that item are held by the Player. This script allows you to change the texture based on the item's amount, so that you can show e.g. a single coin if the Player has 1 Money item, or a bunch of coins if they have 10.

To use it:

  1. Copy/paste the code below into a C# script named MultipleItemGraphics.
  2. In the Settings Manager's "Inventory" panel, set Selected item's display to Show Selected Graphic.
  3. In the Inventory item's properties, check Can carry multiple?.
  4. Note the item's ID value, shown to the left of it's name in the "Inventory items" panel.
  5. Attach the new Multiple Item Graphics component to a GameObject in the scene, and set its Item ID value to match the ID in step 4.
  6. Set the Alternative Graphics array size to match the number of non-default textures you have, and assign the details of which texture to use, and the minimum amount necessary for it to show.

MultipleItemGraphics.cs:

using System;
using UnityEngine;
using AC;

public class MultipleItemGraphics : MonoBehaviour
{

	public int itemID;
	public AlternativeGraphic[] alternativeGraphics;

	private void OnEnable ()
	{
		Array.Sort (alternativeGraphics, delegate (AlternativeGraphic x, AlternativeGraphic y) { return y.minCount.CompareTo (x.minCount); });
	}

	private void Update ()
	{
		UpdateGraphics ();
	}

	private void UpdateGraphics ()
	{
		bool updateUI = false;
		foreach (InvInstance invInstance in KickStarter.runtimeInventory.PlayerInvCollection.InvInstances)
		{
			if (UpdateItem (invInstance, false))
			{
				updateUI = true;
			}
		}
		if (updateUI)
		{
			PlayerMenus.ResetInventoryBoxes ();
		}

		UpdateItem (KickStarter.runtimeInventory.SelectedInstance, true);
	}

	private bool UpdateItem (InvInstance invInstance, bool isSelected)
	{
		if (!InvInstance.IsValid (invInstance) || invInstance.ItemID != itemID) return false;
		int count = isSelected ? invInstance.TransferCount : invInstance.Count;

		if (!isSelected && KickStarter.runtimeInventory.SelectedInstance == invInstance && count != invInstance.TransferCount)
		{
			count -= invInstance.TransferCount;
		}

		Texture newTexture = null;

		foreach (AlternativeGraphic ag in alternativeGraphics)
		{
			if (count >= ag.minCount)
			{
				newTexture = ag.texture;
				break;
			}
		}

		Texture originalTexture = invInstance.Tex;
		if (invInstance.Tex != newTexture)
		{
			invInstance.Tex = newTexture;
			if (isSelected)
			{
				invInstance.SelectedTex = originalTexture;
			}
			return true;
		}
		if (isSelected)
		{
			invInstance.SelectedTex = null;
		}
		return false;
	}

	[System.Serializable]
	public class AlternativeGraphic
	{
		public int minCount;
		public Texture2D texture;
	}

}
Advertisement