可序列化的Transform采集器

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 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 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(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();
    }
    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();
if (null == tcl)
tcl = selection.AddComponent();
Transform[] trans = selection.GetComponentsInChildren(true);
Debug.LogError("trans: " + trans.Length);
Dictionary dic = new Dictionary();
for (int i = 0,j = trans.Length;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();
}

你可能感兴趣的:(可序列化的Transform采集器)