LaboratoryProtection/Assets/UnityTest/Common/Scripts/Object/ObjectItemSetter.cs

98 lines
2.2 KiB
C#

using Cysharp.Threading.Tasks;
using PMaker.Extension;
using Sirenix.OdinInspector;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Assertions.Must;
public class ObjectItemSetter : BaseBehaviour
{
#if UNITY_EDITOR
public ObjectItem[] items;
public Dictionary<ObjectItem, string[]> mappers;
public Dictionary<string, int> repeating;
[Button]
public void Init()
{
items = this.GetComponentsInChildren<ObjectItem>(true);
mappers = new Dictionary<ObjectItem, string[]>();
foreach (var item in items)
{
mappers.Add(item, item.objects.Select(_ => GetKey(_.transform)).ToArray());
}
}
[Button]
public void ResetAll()
{
foreach (var item in items)
{
item.objects = mappers[item].Select(_ => this.GetItem(_)).ToArray();
item.SetDirty();
}
}
[Button]
public void GetAll()
{
var all = root.GetComponentsInChildren<Transform>();
var query = all
.Select(_ => GetKey(_))
.GroupBy(x => x)
.Where(g => g.Count() > 1)
.ToDictionary(x => x.Key, y => y.Count());
repeating = query;
}
private void Reset()
{
root = GameObject.Find("SanWeiZiChan").transform;
}
public Transform root;
public string GetKey(Transform transform)
{
var current = transform;
var first = "";
while (current.parent != null)
{
first = current.parent.name + "/" + first;
current = current.parent;
}
return first + transform.name;
}
[Button]
public GameObject GetItem(string key)
{
var rootName = key.Split('/')[0];
var tempRoot = default(Transform);
if (rootName == this.root.name)
{
tempRoot = this.root;
}
else
{
tempRoot = this.transform;
}
Debug.Log(tempRoot);
var target = key.Replace(tempRoot.name + "/", "");
Debug.Log(target);
var go = tempRoot.Find(target).gameObject;
Debug.Log(go);
return go;
}
#endif
}