예제 #1
0
        protected override ObjectValue[] GetChildrenSafe(ObjectPath path, int index, int count, IEvaluationOptions options)
        {
            // Get the children of the current game object, as seen in the Hierarchy view in the Unity Editor
            // These are the children of the current game object's transform property
            var transform = GetMember(myGameObject, "transform");

            if (transform == null)
            {
                return(EmptyArray <ObjectValue> .Instance);
            }

            var childCountValue = (PrimitiveValue)GetMember(transform, "childCount");

            if (childCountValue == null)
            {
                return(EmptyArray <ObjectValue> .Instance);
            }

            var childCount = (int)childCountValue.Value;

            var objectValues = new List <ObjectValue>(childCount);

            for (var i = 0; i < childCount; i++)
            {
                var currentIndex = Adaptor.CreateValue(Context, i);
                var child        = InvokeInstanceMethod(transform, "GetChild", currentIndex);
                if (child != null)
                {
                    var childGameObject = GetMember(child, "gameObject");
                    if (childGameObject != null)
                    {
                        var name = (GetMember(childGameObject, "name") as StringMirror)?.Value ?? "GameObject";
                        objectValues.Add(CreateObjectValue(name, childGameObject, options));
                    }
                }
            }

            return(objectValues.ToArray());
        }
        protected override ObjectValue[] GetChildrenSafe(ObjectPath path, int index, int count,
                                                         IEvaluationOptions options)
        {
            var allocatorTempObject     = Evaluate("global::Unity.Collections.Allocator.Temp");
            var componentDataType       = GetType("Unity.Entities.IComponentData").NotNull();
            var sharedComponentDataType = GetType("Unity.Entities.ISharedComponentData").NotNull();

            var entityManagerGetComponentTypesMethod      = myEntityManagerType.GetMethod("GetComponentTypes");
            var entityManagerGetSharedComponentDataMethod = myEntityManagerType.GetMethod("GetSharedComponentData");
            var entityManagerGetComponentDataMethod       = myEntityManagerType.GetMethod("GetComponentData");

            var componentTypesArray = Invoke(entityManagerGetComponentTypesMethod, myEntityManagerType,
                                             myEntityManagerObject, myEntityObject, allocatorTempObject.Value).NotNull();
            var componentTypesArrayLength =
                (PrimitiveValue)Adaptor.GetMember(Context, null, componentTypesArray, "Length").Value;

            var numberOfComponentTypes = (int)componentTypesArrayLength.Value;

            var objectValues = new List <ObjectValue>();

            for (var currentIndex = 0; currentIndex < numberOfComponentTypes; currentIndex++)
            {
                try
                {
                    var currentIndexValue = Adaptor.CreateValue(Context, currentIndex);
                    var currentComponent  = Adaptor
                                            .GetIndexerReference(Context, componentTypesArray, new[] { currentIndexValue }).Value;
                    var currentComponentType = currentComponent.Type;

                    var getManagedTypeMethod    = currentComponentType.GetMethod("GetManagedType");
                    var dataManagedType         = Invoke(getManagedTypeMethod, currentComponentType, currentComponent);
                    var dataManagedTypeFullName =
                        ((StringMirror)Adaptor.GetMember(Context, null, dataManagedType, "FullName").Value).Value;
                    var dataManagedTypeShortName =
                        ((StringMirror)Adaptor.GetMember(Context, null, dataManagedType, "Name").Value).Value;
                    var dataType = GetType(dataManagedTypeFullName);

                    MethodMirror getComponentDataMethod;
                    if (componentDataType.IsAssignableFrom(dataType))
                    {
                        getComponentDataMethod = entityManagerGetComponentDataMethod;
                    }
                    else if (sharedComponentDataType.IsAssignableFrom(dataType))
                    {
                        getComponentDataMethod = entityManagerGetSharedComponentDataMethod;
                    }
                    else
                    {
                        ourLogger.Warn("Unknown type of component data: {0}", dataManagedTypeFullName);
                        continue;
                    }

                    var getComponentDataMethodWithTypeArgs =
                        getComponentDataMethod.MakeGenericMethod(new[] { dataType });
                    var result = Invoke(getComponentDataMethodWithTypeArgs, myEntityManagerType,
                                        myEntityManagerObject, myEntityObject);
                    objectValues.Add(LiteralValueReference
                                     .CreateTargetObjectLiteral(Adaptor, Context, dataManagedTypeShortName, result)
                                     .CreateObjectValue(options));
                }
                catch (Exception e)
                {
                    ourLogger.Error(e, "Failed to fetch parameter {0} of entity {1}", currentIndex, myEntityObject);
                }
            }

            return(objectValues.ToArray());
        }