using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
[System.Serializable]
public class TransformCollection : MonoBehaviour {
[SerializeField]
[System.Serializable]
public class CollectionItem
{
public CollectionItem(string key,Transform value)
{
this.key = key;
this.value = value;
this.nextIndex = -1;
}
[SerializeField]
public string key;
[SerializeField]
public Transform value;
[SerializeField]
public int nextIndex;
}
[HideInInspector]
[SerializeField]
private CollectionItem[] _collectionItem = null;
[HideInInspector]
[SerializeField]
private CollectionItem[] _collisionItem = null;
public int Length
{
get
{
if (null != _collectionItem)
return _collectionItem.Length;
return 0;
}
}
public void Generate(Dictionary<string,Transform> nameList)
{
if (null == nameList || 0 == nameList.Count)
return;
_collectionItem = new CollectionItem[nameList.Count];
_collisionItem = new CollectionItem[nameList.Count - 1];
for (int i = 0,j = _collectionItem.Length;i<j;i++)
{
_collectionItem[i] = null;
if (i < _collisionItem.Length)
_collisionItem[i] = null;
}
int colIndex = 0;
foreach(KeyValuePair<string,Transform> kv in nameList)
{
int hCode = kv.Key.GetHashCode();
int index = Mathf.Abs(hCode) % nameList.Count;
CollectionItem itemNew = new CollectionItem(kv.Key, kv.Value);
if (null == _collectionItem[index])
_collectionItem[index] = itemNew;
else
{
CollectionItem item = _collectionItem[index];
_collectionItem[index] = itemNew;
itemNew.nextIndex = colIndex;
_collisionItem[colIndex] = item;
colIndex++;
}
}
CollectionItem[] temArr = _collisionItem;
_collisionItem = new CollectionItem[colIndex];
for (int i = 0;i < colIndex;i++)
{
_collisionItem[i] = temArr[i];
}
}
public T GetComponent<T>(string key) where T : Component
{
if (null == _collectionItem ||
string.IsNullOrEmpty(key) ||
0 == _collectionItem.Length)
return null;
int hCode = key.GetHashCode();
int length = _collectionItem.Length;
int index = Mathf.Abs(hCode) % length;
CollectionItem item = _collectionItem[index];
Transform Trans = null;
while(true)
{
if (null == item)
break;
if(item.key == key)
{
Trans = item.value;
break;
}
item = _collisionItem[item.nextIndex];
}
if(null != Trans)
{
return Trans.GetComponent<T>();
}
return null;
}
}
[MenuItem("Tools/TransformCollection")]
static void SetTransformCollection()
{
UnityEngine.Object[] _go = Selection.GetFiltered(typeof(UnityEngine.GameObject), SelectionMode.TopLevel & SelectionMode.Editable & ~SelectionMode.ExcludePrefab);
if (null == _go)
return;
if (_go.Length != 1)
return;
GameObject selection = _go[0] as GameObject;
UnityEngine.Object pref = PrefabUtility.GetPrefabParent(selection);
if (null == pref)
pref = selection;
selection = GameObject.Instantiate(pref) as GameObject;
TransformCollection tcl = selection.GetComponent<TransformCollection>();
if (null == tcl)
tcl = selection.AddComponent<TransformCollection>();
Transform[] trans = selection.GetComponentsInChildren<Transform>(true);
Debug.LogError("trans: " + trans.Length);
Dictionary<string, Transform> dic = new Dictionary<string, Transform>();
for (int i = 0,j = trans.Length;i<j;i++)
{
if(trans[i].name.StartsWith("(") &&
trans[i].name.Contains(")"))
{
if (!dic.ContainsKey(trans[i].name))
dic.Add(trans[i].name, trans[i]);
else
Debug.LogError("有相同的key:" + trans[i].name);
}
}
Debug.LogError("========== " + dic.Count);
tcl.Generate(dic);
Debug.LogError("collection: " + tcl.Length);
PrefabUtility.ReplacePrefab(selection, pref,ReplacePrefabOptions.Default);
GameObject.DestroyImmediate(selection);
AssetDatabase.Refresh();
}