Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using AC;
- public class InventoryObjectHider : MonoBehaviour
- {
- [System.Serializable]
- public class Entry
- {
- [Tooltip("Inventory Item ID.")]
- public int inventoryID = -1;
- [Tooltip("If this is >= 0, check that first")]
- public int IndexOfBooleanValueToCheck = -1;
- [Tooltip("Objects to hide.")]
- public GameObject[] objects;
- [Tooltip("Inverted logic.")]
- public bool invert = false;
- }
- public List<Entry> entries = new List<Entry>();
- public float initialDelay = 0.1f;
- [Tooltip("Fade duration")]
- public float fadeDuration = 0.5f;
- [Tooltip("Use fade toggle")]
- public bool useFadeEffect = true;
- private HashSet<GameObject> currentlyFading = new HashSet<GameObject>();
- // ------------------------------------------------------------------
- void OnEnable()
- {
- EventManager.OnInventoryAdd += OnInventoryChanged;
- EventManager.OnInventoryRemove += OnInventoryChanged;
- EventManager.OnFinishLoading += OnFinishLoading;
- }
- void OnDisable()
- {
- EventManager.OnInventoryAdd -= OnInventoryChanged;
- EventManager.OnInventoryRemove -= OnInventoryChanged;
- EventManager.OnFinishLoading -= OnFinishLoading;
- }
- void Start()
- {
- StartCoroutine(InitialRefresh());
- }
- IEnumerator InitialRefresh()
- {
- while (KickStarter.runtimeInventory == null)
- yield return null;
- if (initialDelay > 0f)
- yield return new WaitForSeconds(initialDelay);
- Refresh(immediate: true);
- }
- void OnInventoryChanged(InvItem item, int amount)
- {
- Refresh(immediate: false);
- }
- void OnFinishLoading(int saveID)
- {
- Refresh(immediate: true);
- }
- // ------------------------------------------------------------------
- [ContextMenu("Refresh Now")]
- public void Refresh()
- {
- Refresh(immediate: false);
- }
- public void Refresh(bool immediate)
- {
- if (KickStarter.runtimeInventory == null)
- return;
- foreach (Entry e in entries)
- {
- if (e == null || e.inventoryID < 0 || e.objects == null)
- continue;
- bool carrying = IsCarrying(e.inventoryID);
- bool shouldBeActive = e.invert ? carrying : !carrying;
- if (e.IndexOfBooleanValueToCheck >= 0)
- {
- bool boolValue = AC.GlobalVariables.GetBooleanValue(e.IndexOfBooleanValueToCheck);
- shouldBeActive = e.invert ? !boolValue : boolValue;
- }
- foreach (GameObject go in e.objects)
- {
- if (go != null)
- {
- bool isCurrentlyActive = go.activeSelf;
- if (isCurrentlyActive != shouldBeActive)
- {
- if (useFadeEffect && !immediate)
- {
- StartCoroutine(FadeAndToggle(go, shouldBeActive));
- }
- else
- {
- go.SetActive(shouldBeActive);
- }
- }
- }
- }
- }
- }
- IEnumerator FadeAndToggle(GameObject go, bool targetActive)
- {
- if (currentlyFading.Contains(go))
- yield break;
- currentlyFading.Add(go);
- Renderer[] renderers = go.GetComponentsInChildren<Renderer>();
- if (renderers.Length == 0)
- {
- currentlyFading.Remove(go);
- yield break;
- }
- // Fade Out (πριν το deactivate)
- if (!targetActive)
- {
- yield return StartCoroutine(FadeRenderers(renderers, 1f, 0f));
- }
- // Fade In (μετά το activate)
- else
- {
- yield return StartCoroutine(FadeRenderers(renderers, 0f, 1f));
- }
- // If this is also a hotspot, disable
- if (go.TryGetComponent<AC.Hotspot>(out var hotspot))
- {
- hotspot.enabled = targetActive;
- }
- currentlyFading.Remove(go);
- }
- IEnumerator FadeRenderers(Renderer[] renderers, float startAlpha, float endAlpha)
- {
- Dictionary<Material, Color> originalColors = new Dictionary<Material, Color>();
- Dictionary<Material, int> originalRenderQueue = new Dictionary<Material, int>();
- foreach (Renderer r in renderers)
- {
- if (r == null) continue;
- foreach (Material mat in r.materials)
- {
- if (mat == null) continue;
- if (!originalColors.ContainsKey(mat))
- {
- originalColors[mat] = mat.color;
- originalRenderQueue[mat] = mat.renderQueue;
- if (mat.HasProperty("_Mode"))
- {
- mat.SetFloat("_Mode", 3); // Transparent
- mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
- mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
- mat.SetInt("_ZWrite", 0);
- mat.DisableKeyword("_ALPHATEST_ON");
- mat.EnableKeyword("_ALPHABLEND_ON");
- mat.DisableKeyword("_ALPHAPREMULTIPLY_ON");
- mat.renderQueue = 3000;
- }
- }
- }
- }
- // Fade animation
- float elapsed = 0f;
- while (elapsed < fadeDuration)
- {
- elapsed += Time.deltaTime;
- float t = Mathf.Clamp01(elapsed / fadeDuration);
- float currentAlpha = Mathf.Lerp(startAlpha, endAlpha, t);
- foreach (Renderer r in renderers)
- {
- if (r == null) continue;
- foreach (Material mat in r.materials)
- {
- if (mat == null || !originalColors.ContainsKey(mat)) continue;
- Color col = originalColors[mat];
- col.a = currentAlpha;
- mat.color = col;
- }
- }
- yield return null;
- }
- foreach (Renderer r in renderers)
- {
- if (r == null) continue;
- foreach (Material mat in r.materials)
- {
- if (mat == null || !originalColors.ContainsKey(mat)) continue;
- Color col = originalColors[mat];
- col.a = endAlpha;
- mat.color = col;
- // mat.renderQueue = originalRenderQueue[mat];
- }
- }
- }
- bool IsCarrying(int id)
- {
- return KickStarter.runtimeInventory.GetCount(id) > 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment