美文网首页
可序列化的Transform采集器

可序列化的Transform采集器

作者: 光棍狗没有可持续发展 | 来源:发表于2018-08-02 20:12 被阅读0次

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();
}

相关文章

  • 可序列化的Transform采集器

    using UnityEngine;using System.Collections;using System.C...

  • 动画的分类和实现

    一、Transform动画 1.利用Transform的CGAffineTransformTranslate,可实...

  • 序列化

    JavaDoc文档描述 类可以通过继承序列化接口拥有序列化的能力.所有可序列化类的子类都是可序列化的.序列化接口没...

  • Android 序列化 Parcelable VS Seria

    Android 的序列化方式 Parcelable Parcel 介绍:Parcel 内部包装了可序列化的数据,可...

  • 28.transform

    28.transform 缩放可影响boder边线

  • Java之序列化

    如何声明一个可序列化的类? 一个类可序列化的前提:实现Serializable接口 一个属性不想序列化的方式:增加...

  • 序列化与反序列化

    基本概念 什么是序列化,什么是反序列化? 序列化:变量从内存中变成可存储或传输的过程称之为序列化反序列化:变量内容...

  • CSS3转换、过渡与动画

    CSS转换 CSS3 Transform(让元素在一个坐标系统中变形,可移动、旋转和缩放元素)transform ...

  • Android开发篇之对象序列化

    Android开发篇之对象序列化 什么是序列化?序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是...

  • 无线数据采集器快速实时地准确地将数据传输到服务器

    无线数据采集器基本介绍 无线数据采集器在市面上可以分为多种:无线条码数据采集器、无线IC卡数据采集器等等。无线数据...

网友评论

      本文标题:可序列化的Transform采集器

      本文链接:https://www.haomeiwen.com/subject/tawsvftx.html