Пример #1
0
        /// <summary>
        /// Create a proxy <see cref="EvergineEntity"/> for a remote <see cref="ARREntity"/>.
        /// <para>
        ///
        /// When created, the path from the remote <see cref="ARREntity"/> to the remote scene root will have
        /// proxy entities created for it.
        /// These proxy entities must be created in order to appropriately set the
        /// <see cref="EvergineEntity.Parent"/> for the proxy entity.
        ///
        /// As a side effect, this means that, given a Remote hierarchy:
        ///
        /// ARR.Parent
        ///     ARR.Child
        ///
        /// Calling <see cref="CreateProxyEntity"/> on ARR.Child will also create a proxy entity for ARR.Parent.
        /// </para>
        /// </summary>
        /// <param name="entityManager">The entity manager where the proxy entity is included.</param>
        /// <param name="remoteEntity">The remote entity.</param>
        /// <param name="mode">Whether the proxy components will be created.</param>
        /// <param name="recursive">Whether to create proxy entities for children of the remote entity.</param>
        /// <returns>A proxy <see cref="EvergineEntity"/> for a remote <see cref="ARREntity"/>.</returns>
        public static EvergineEntity CreateProxyEntity(this EntityManager entityManager, ARREntity remoteEntity, ARRCreationMode mode, bool recursive = false)
        {
            if (!remoteEntity.Valid)
            {
                return(null);
            }

            if (ARREntitySync.TryGetSyncComponent(remoteEntity, out _))
            {
                throw new Exception("A proxy entity for this remote entity already exists!");
            }

            var proxyEntity = new EvergineEntity();

            proxyEntity.AddComponent(new Transform3D());

            var remoteParentEntity = remoteEntity.Parent;

            if (remoteParentEntity != null)
            {
                var proxyParentEntity = GetExistingProxyEntity(remoteParentEntity);
                if (proxyParentEntity == null)
                {
                    proxyParentEntity = entityManager.CreateProxyEntity(remoteParentEntity, ARRCreationMode.DoNotCreateProxyComponents, false);
                }

                proxyParentEntity.AddChild(proxyEntity);
            }
            else
            {
                entityManager.Add(proxyEntity);
            }

            var sync = proxyEntity.FindComponent <ARREntitySync>();

            if (sync == null)
            {
                sync = new ARREntitySync();
                proxyEntity.AddComponent(sync);
            }

            sync.Bind(remoteEntity, true);

            if (mode == ARRCreationMode.CreateProxyComponents)
            {
                proxyEntity.CreateARRComponentsFromRemoteEntity(remoteEntity);
            }

            if (recursive)
            {
                foreach (var remoteChild in remoteEntity.Children)
                {
                    entityManager.CreateProxyEntity(remoteChild, mode, true);
                }
            }

            return(proxyEntity);
        }
Пример #2
0
        /// <summary>
        /// Get an existing proxy <see cref="EvergineEntity"/> for an remote <see cref="ARREntity"/>.
        /// If no proxy entity has been mapped to this remote entity then null.
        /// </summary>
        /// <param name="remoteEntity">The remote <see cref="ARREntity"/>.</param>
        /// <returns>An proxy <see cref="EvergineEntity"/> if exists; otherwise, <c>null</c>.</returns>
        public static EvergineEntity GetExistingProxyEntity(this ARREntity remoteEntity)
        {
            if (ARREntitySync.TryGetSyncComponent(remoteEntity, out var syncComponent))
            {
                return(syncComponent.Owner);
            }

            return(null);
        }