Exemplo n.º 1
0
        public List <ReferenceItem> GetReferenceItems()
        {
            var items = new List <ReferenceItem>();

            for (int i = 0; i < cacheItems.Count; i++)
            {
                var cacheItem = cacheItems[i];
                var item      = new ReferenceItem();
                item.name    = cacheItem.name;
                item.isArray = cacheItem.isArray;

                if (!string.IsNullOrEmpty(cacheItem.assembleName) && !string.IsNullOrEmpty(cacheItem.typeName))
                {
                    item.type = System.Reflection.Assembly.Load(cacheItem.assembleName).GetType(cacheItem.typeName);
                }

                if (item.isArray)
                {
                    item.referenceTargets = new List <Object>();
                    if (cacheItem.referenceInstenceIDs != null)
                    {
                        cacheItem.referenceInstenceIDs.ForEach(x =>
                        {
#if UNITY_EDITOR
                            item.referenceTargets.Add(UnityEditor.EditorUtility.InstanceIDToObject(x));
#endif
                        });
                    }
                    item.values = cacheItem.values;
                }
                else
                {
#if UNITY_EDITOR
                    item.referenceTarget = UnityEditor.EditorUtility.InstanceIDToObject(cacheItem.refereneceInstenceID);
#endif
                    item.value = cacheItem.value;
                }

                items.Add(item);
            }
            return(items);
        }
Exemplo n.º 2
0
        private void DrawHead(Rect rect)
        {
            var buttonRect = new Rect(rect.x + rect.width - 60, rect.y, 60, rect.height);

            if (GUI.Button(buttonRect, "apply"))
            {
                inEdit = false;
                ApplyChangesToScript();
            }

            EditorGUI.LabelField(rect, "数据及引用信息列表");

            if (Event.current.type == EventType.DragUpdated)
            {
                if (rect.Contains(Event.current.mousePosition))
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Move;
                }
            }
            else if (Event.current.type == EventType.DragPerform)
            {
                if (rect.Contains(Event.current.mousePosition))
                {
                    for (int i = 0; i < DragAndDrop.objectReferences.Length; i++)
                    {
                        Object obj = DragAndDrop.objectReferences[i];
                        if (obj is GameObject)
                        {
                            obj = SortObjectType(obj as GameObject);
                        }
                        var item = new ReferenceItem();
                        item.name            = obj.name;
                        item.type            = obj.GetType();
                        item.referenceTarget = obj;
                        referenceItems.Add(item);
                    }
                }
            }
        }
Exemplo n.º 3
0
        private void AnalysisItemFromScript()
        {
            var flags = System.Reflection.BindingFlags.GetField |
                        System.Reflection.BindingFlags.Instance |
                        System.Reflection.BindingFlags.NonPublic;
            var fieldsArray = new List <FieldInfo>();
            var innerFields = target.GetType().GetFields(flags | BindingFlags.DeclaredOnly);
            int innerCount  = innerFields.Length;

            fieldsArray.AddRange(innerFields);

            var    referenceField = target.GetType().BaseType.GetField("m_data", flags);
            object referenceValue = null;

            if (referenceField != null)
            {
                referenceValue = referenceField.GetValue(target);
                var referenceType = referenceField.FieldType;
                var dataFields    = referenceType.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetField);
                fieldsArray.AddRange(dataFields);
            }


            referenceItems.Clear();

            if (fieldsArray != null && fieldsArray.Count > 0)
            {
                for (int i = 0; i < fieldsArray.Count; i++)
                {
                    var fieldItem = fieldsArray[i];
                    var item      = new ReferenceItem();
                    item.name = typeof(UnityEngine.Object).IsAssignableFrom(fieldItem.FieldType) ? fieldItem.Name.Substring(2) : fieldItem.Name;
                    item.type = fieldItem.FieldType.IsArray ? fieldItem.FieldType.GetElementType() : fieldItem.FieldType;
                    var value      = fieldItem.GetValue(i >= innerCount? referenceValue: target);
                    var arrayValue = value as System.Array;
                    item.isArray = arrayValue != null;
                    if (value != null)
                    {
                        var refType = GetReferenceType(item.type, item.isArray);
                        switch (refType)
                        {
                        case ReferenceItemType.Reference:
                            item.referenceTarget = (UnityEngine.Object)value;
                            RegistReferenceObject(item.referenceTarget);
                            break;

                        case ReferenceItemType.ConventAble:
                            item.value = value.ToString();
                            break;

                        case ReferenceItemType.Struct:
                            item.value = JsonUtility.ToJson(value);
                            break;

                        case ReferenceItemType.ReferenceArray:
                        {
                            item.referenceTargets = new List <Object>();
                            var enumerator = arrayValue.GetEnumerator();
                            while (enumerator.MoveNext())
                            {
                                item.referenceTargets.Add((Object)enumerator.Current);
                            }
                            RegistReferenceObject(item.referenceTargets.ToArray());
                        }
                        break;

                        case ReferenceItemType.ConventAbleArray:
                        {
                            item.values = new List <string>();
                            var enumerator = arrayValue.GetEnumerator();
                            while (enumerator.MoveNext())
                            {
                                item.values.Add(enumerator.Current.ToString());
                            }
                        }
                        break;

                        case ReferenceItemType.StructArray:
                        {
                            item.values = new List <string>();
                            var enumerator = arrayValue.GetEnumerator();
                            while (enumerator.MoveNext())
                            {
                                item.values.Add(JsonUtility.ToJson(enumerator.Current));
                            }
                        }
                        break;

                        default:
                            break;
                        }
                    }
                    referenceItems.Add(item);
                }
            }
        }