装备系统做了很多
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using Bansonic;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
public class equipBag : MonoBehaviour
|
||||
{
|
||||
[Header("prefab")]
|
||||
public GameObject eqpmtItemPrefab;
|
||||
public Transform eqpmtItemParent;
|
||||
|
||||
[Header("source paths")]
|
||||
public string runtimeEquipmentFolder = "so/uEquip";
|
||||
public string editorEquipmentFolder = "Assets/Resources/so/uEquip";
|
||||
|
||||
private readonly List<GameObject> spawnedItems = new List<GameObject>();
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Rebuild();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
Rebuild();
|
||||
}
|
||||
|
||||
public void Rebuild()
|
||||
{
|
||||
ClearSpawnedItems();
|
||||
|
||||
if (eqpmtItemPrefab == null || eqpmtItemParent == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
equipmentSO[] equipments = LoadEquipments();
|
||||
if (equipments == null || equipments.Length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
equipments = equipments
|
||||
.Where(equipment => equipment != null
|
||||
&& !equipSmelt.IsEquipmentAssignedToSmeltPool(equipment)
|
||||
&& !equipSmelt.IsEquipmentConsumedBySmelt(equipment))
|
||||
.ToArray();
|
||||
|
||||
Array.Sort(equipments, CompareEquipments);
|
||||
|
||||
for (int i = 0; i < equipments.Length; i++)
|
||||
{
|
||||
equipmentSO equipment = equipments[i];
|
||||
if (equipment == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
GameObject instance = Instantiate(eqpmtItemPrefab, eqpmtItemParent);
|
||||
instance.name = string.IsNullOrWhiteSpace(equipment.GetCurrentTierDisplayName())
|
||||
? equipment.name
|
||||
: equipment.GetCurrentTierDisplayName();
|
||||
|
||||
equipItemPrefab item = instance.GetComponent<equipItemPrefab>();
|
||||
if (item != null)
|
||||
{
|
||||
item.Bind(equipment, HandleEquipmentClicked);
|
||||
}
|
||||
|
||||
spawnedItems.Add(instance);
|
||||
}
|
||||
}
|
||||
|
||||
private equipmentSO[] LoadEquipments()
|
||||
{
|
||||
var results = new List<equipmentSO>();
|
||||
#if UNITY_EDITOR
|
||||
if (!Application.isPlaying)
|
||||
{
|
||||
results.AddRange(LoadEditorEquipments());
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
results.AddRange(Resources.LoadAll<equipmentSO>(runtimeEquipmentFolder)
|
||||
.Where(equipment => equipment != null));
|
||||
}
|
||||
|
||||
IReadOnlyList<equipmentSO> runtimeGenerated = equipmentGenerator.GetRuntimeGeneratedEquipments();
|
||||
if (runtimeGenerated != null)
|
||||
{
|
||||
for (int i = 0; i < runtimeGenerated.Count; i++)
|
||||
{
|
||||
equipmentSO equipment = runtimeGenerated[i];
|
||||
if (equipment != null && !results.Contains(equipment))
|
||||
{
|
||||
results.Add(equipment);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return results
|
||||
.Where(equipment => equipment != null)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private equipmentSO[] LoadEditorEquipments()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(editorEquipmentFolder) || !AssetDatabase.IsValidFolder(editorEquipmentFolder))
|
||||
{
|
||||
return Array.Empty<equipmentSO>();
|
||||
}
|
||||
|
||||
string[] guids = AssetDatabase.FindAssets("t:equipmentSO", new[] { editorEquipmentFolder });
|
||||
var result = new List<equipmentSO>(guids.Length);
|
||||
|
||||
for (int i = 0; i < guids.Length; i++)
|
||||
{
|
||||
string path = AssetDatabase.GUIDToAssetPath(guids[i]);
|
||||
equipmentSO equipment = AssetDatabase.LoadAssetAtPath<equipmentSO>(path);
|
||||
if (equipment != null)
|
||||
{
|
||||
result.Add(equipment);
|
||||
}
|
||||
}
|
||||
|
||||
return result.ToArray();
|
||||
}
|
||||
#endif
|
||||
|
||||
private void HandleEquipmentClicked(equipmentSO equipment)
|
||||
{
|
||||
if (equipment == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void ClearSpawnedItems()
|
||||
{
|
||||
if (eqpmtItemParent != null)
|
||||
{
|
||||
for (int i = eqpmtItemParent.childCount - 1; i >= 0; i--)
|
||||
{
|
||||
Transform child = eqpmtItemParent.GetChild(i);
|
||||
if (child == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
if (!Application.isPlaying)
|
||||
{
|
||||
DestroyImmediate(child.gameObject);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
Destroy(child.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
spawnedItems.Clear();
|
||||
}
|
||||
|
||||
private static int CompareEquipments(equipmentSO left, equipmentSO right)
|
||||
{
|
||||
if (left == right)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (left == null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (right == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int levelCompare = right.level.CompareTo(left.level);
|
||||
if (levelCompare != 0)
|
||||
{
|
||||
return levelCompare;
|
||||
}
|
||||
|
||||
string leftName = left.GetCurrentTierDisplayName();
|
||||
string rightName = right.GetCurrentTierDisplayName();
|
||||
return string.Compare(leftName, rightName, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user