123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using System.Collections.Generic;
- using UnityEngine;
- namespace TriLibCore
- {
-
- public class AssetUnloader : MonoBehaviour
- {
-
-
-
- public List<Object> Allocations;
- private int _id;
-
- public int Id
- {
- get => _id;
- set
- {
- _id = value;
- Register();
- }
- }
- private static int _lastId;
- private static readonly Dictionary<int, int> AssetUnloaders = new Dictionary<int, int>();
-
-
- public static int GetNextId()
- {
- return _lastId++;
- }
- private void Register()
- {
- if (!AssetUnloaders.ContainsKey(_id))
- {
- AssetUnloaders[_id] = 0;
- }
- else
- {
- AssetUnloaders[_id]++;
- }
- }
- private void Start()
- {
- Register();
- ChangeLayerRecursively(transform);
- }
- public void ChangeLayerRecursively(Transform parentTransform)
- {
-
- parentTransform.gameObject.layer = LayerMask.NameToLayer("Arrow");
-
- for (int i = 0; i < parentTransform.childCount; i++)
- {
- Transform childTransform = parentTransform.GetChild(i);
- ChangeLayerRecursively(childTransform);
- }
- }
- private void OnDestroy()
- {
- if (AssetUnloaders.TryGetValue(_id, out var value))
- {
- if (--value <= 0)
- {
- foreach (var allocation in Allocations)
- {
- if (allocation == null)
- {
- continue;
- }
- Destroy(allocation);
- }
- AssetUnloaders.Remove(_id);
- }
- else
- {
- AssetUnloaders[_id] = value;
- }
- }
- }
- }
- }
|