Guest User

Inventory Manager Script

a guest
Aug 2nd, 2026
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using AC;
  5.  
  6. public class InventoryObjectHider : MonoBehaviour
  7. {
  8. [System.Serializable]
  9. public class Entry
  10. {
  11. [Tooltip("Inventory Item ID.")]
  12. public int inventoryID = -1;
  13.  
  14. [Tooltip("If this is >= 0, check that first")]
  15. public int IndexOfBooleanValueToCheck = -1;
  16.  
  17. [Tooltip("Objects to hide.")]
  18. public GameObject[] objects;
  19.  
  20. [Tooltip("Inverted logic.")]
  21. public bool invert = false;
  22. }
  23.  
  24. public List<Entry> entries = new List<Entry>();
  25.  
  26. public float initialDelay = 0.1f;
  27.  
  28. [Tooltip("Fade duration")]
  29. public float fadeDuration = 0.5f;
  30.  
  31. [Tooltip("Use fade toggle")]
  32. public bool useFadeEffect = true;
  33.  
  34. private HashSet<GameObject> currentlyFading = new HashSet<GameObject>();
  35.  
  36. // ------------------------------------------------------------------
  37.  
  38. void OnEnable()
  39. {
  40. EventManager.OnInventoryAdd += OnInventoryChanged;
  41. EventManager.OnInventoryRemove += OnInventoryChanged;
  42. EventManager.OnFinishLoading += OnFinishLoading;
  43. }
  44.  
  45. void OnDisable()
  46. {
  47. EventManager.OnInventoryAdd -= OnInventoryChanged;
  48. EventManager.OnInventoryRemove -= OnInventoryChanged;
  49. EventManager.OnFinishLoading -= OnFinishLoading;
  50. }
  51.  
  52. void Start()
  53. {
  54. StartCoroutine(InitialRefresh());
  55. }
  56.  
  57. IEnumerator InitialRefresh()
  58. {
  59. while (KickStarter.runtimeInventory == null)
  60. yield return null;
  61.  
  62. if (initialDelay > 0f)
  63. yield return new WaitForSeconds(initialDelay);
  64.  
  65. Refresh(immediate: true);
  66. }
  67.  
  68. void OnInventoryChanged(InvItem item, int amount)
  69. {
  70. Refresh(immediate: false);
  71. }
  72.  
  73. void OnFinishLoading(int saveID)
  74. {
  75. Refresh(immediate: true);
  76. }
  77.  
  78. // ------------------------------------------------------------------
  79.  
  80. [ContextMenu("Refresh Now")]
  81. public void Refresh()
  82. {
  83. Refresh(immediate: false);
  84. }
  85.  
  86. public void Refresh(bool immediate)
  87. {
  88. if (KickStarter.runtimeInventory == null)
  89. return;
  90.  
  91. foreach (Entry e in entries)
  92. {
  93. if (e == null || e.inventoryID < 0 || e.objects == null)
  94. continue;
  95.  
  96. bool carrying = IsCarrying(e.inventoryID);
  97. bool shouldBeActive = e.invert ? carrying : !carrying;
  98. if (e.IndexOfBooleanValueToCheck >= 0)
  99. {
  100. bool boolValue = AC.GlobalVariables.GetBooleanValue(e.IndexOfBooleanValueToCheck);
  101. shouldBeActive = e.invert ? !boolValue : boolValue;
  102. }
  103.  
  104. foreach (GameObject go in e.objects)
  105. {
  106. if (go != null)
  107. {
  108. bool isCurrentlyActive = go.activeSelf;
  109.  
  110. if (isCurrentlyActive != shouldBeActive)
  111. {
  112. if (useFadeEffect && !immediate)
  113. {
  114. StartCoroutine(FadeAndToggle(go, shouldBeActive));
  115. }
  116. else
  117. {
  118. go.SetActive(shouldBeActive);
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125.  
  126. IEnumerator FadeAndToggle(GameObject go, bool targetActive)
  127. {
  128. if (currentlyFading.Contains(go))
  129. yield break;
  130.  
  131. currentlyFading.Add(go);
  132.  
  133. Renderer[] renderers = go.GetComponentsInChildren<Renderer>();
  134.  
  135. if (renderers.Length == 0)
  136. {
  137. currentlyFading.Remove(go);
  138. yield break;
  139. }
  140.  
  141. // Fade Out (πριν το deactivate)
  142. if (!targetActive)
  143. {
  144. yield return StartCoroutine(FadeRenderers(renderers, 1f, 0f));
  145. }
  146. // Fade In (μετά το activate)
  147. else
  148. {
  149. yield return StartCoroutine(FadeRenderers(renderers, 0f, 1f));
  150. }
  151.  
  152.  
  153. // If this is also a hotspot, disable
  154. if (go.TryGetComponent<AC.Hotspot>(out var hotspot))
  155. {
  156. hotspot.enabled = targetActive;
  157. }
  158.  
  159.  
  160. currentlyFading.Remove(go);
  161. }
  162.  
  163. IEnumerator FadeRenderers(Renderer[] renderers, float startAlpha, float endAlpha)
  164. {
  165. Dictionary<Material, Color> originalColors = new Dictionary<Material, Color>();
  166. Dictionary<Material, int> originalRenderQueue = new Dictionary<Material, int>();
  167.  
  168. foreach (Renderer r in renderers)
  169. {
  170. if (r == null) continue;
  171.  
  172. foreach (Material mat in r.materials)
  173. {
  174. if (mat == null) continue;
  175.  
  176. if (!originalColors.ContainsKey(mat))
  177. {
  178. originalColors[mat] = mat.color;
  179. originalRenderQueue[mat] = mat.renderQueue;
  180.  
  181. if (mat.HasProperty("_Mode"))
  182. {
  183. mat.SetFloat("_Mode", 3); // Transparent
  184. mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  185. mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  186. mat.SetInt("_ZWrite", 0);
  187. mat.DisableKeyword("_ALPHATEST_ON");
  188. mat.EnableKeyword("_ALPHABLEND_ON");
  189. mat.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  190. mat.renderQueue = 3000;
  191. }
  192. }
  193. }
  194. }
  195.  
  196. // Fade animation
  197. float elapsed = 0f;
  198. while (elapsed < fadeDuration)
  199. {
  200. elapsed += Time.deltaTime;
  201. float t = Mathf.Clamp01(elapsed / fadeDuration);
  202. float currentAlpha = Mathf.Lerp(startAlpha, endAlpha, t);
  203.  
  204. foreach (Renderer r in renderers)
  205. {
  206. if (r == null) continue;
  207.  
  208. foreach (Material mat in r.materials)
  209. {
  210. if (mat == null || !originalColors.ContainsKey(mat)) continue;
  211.  
  212. Color col = originalColors[mat];
  213. col.a = currentAlpha;
  214. mat.color = col;
  215. }
  216. }
  217.  
  218. yield return null;
  219. }
  220.  
  221. foreach (Renderer r in renderers)
  222. {
  223. if (r == null) continue;
  224.  
  225. foreach (Material mat in r.materials)
  226. {
  227. if (mat == null || !originalColors.ContainsKey(mat)) continue;
  228.  
  229. Color col = originalColors[mat];
  230. col.a = endAlpha;
  231. mat.color = col;
  232.  
  233. // mat.renderQueue = originalRenderQueue[mat];
  234. }
  235. }
  236. }
  237.  
  238. bool IsCarrying(int id)
  239. {
  240. return KickStarter.runtimeInventory.GetCount(id) > 0;
  241. }
  242. }
  243.  
Advertisement
Add Comment
Please, Sign In to add comment