/// <summary>
        /// Create a new ISharingServiceTarget from a given Azure Remote Rendering Entity object that is child of the
        /// given SharingTargetRoot.
        /// </summary>
        public static ISharingServiceTarget CreateTarget(SharingTargetRoot root, Entity child)
        {
            if (root == null || child == null || !child.Valid)
            {
                return(null);
            }

            RemoteEntitySyncObject rootEntitySync = root.GetComponentInChildren <RemoteEntitySyncObject>();

            if (rootEntitySync == null || !rootEntitySync.IsEntityValid)
            {
                return(null);
            }

            return(root.InnerTarget.AddChild(CreateAddress(rootEntitySync.Entity, child)));
        }
        /// <summary>
        /// Resolve an ISharingServiceTarget to an Azure Remote Rendering Entity object. If a 'SharingTargetRoot' is
        /// provided, this will search the 'rootHint' for the remote Entity. If 'SharingTargetRoot' is not provided,
        /// this will first search the 'target' for the nearest 'SharingTargetRoot', and then search for the Entity.
        /// </summary>
        public static Entity ResolveTarget(ISharingServiceTarget target, SharingTargetRoot rootHint = null)
        {
            if (target == null)
            {
                return(null);
            }

            if ((rootHint == null) ||
                (rootHint.InnerTarget == null) ||
                (rootHint.InnerTarget != target.Root && rootHint.InnerTarget != target))
            {
                rootHint = FindRootTarget(target);
            }

            return(FindEntity(rootHint, target));
        }
        /// <summary>
        /// Resolve an ISharingServiceTarget to an Azure Remote Rendering Entity object. This will search the 'root' for the remote Entity.
        /// </summary>
        private static Entity FindEntity(SharingTargetRoot root, ISharingServiceTarget target)
        {
            RemoteEntitySyncObject rootEntitySync = root?.GetComponentInChildren <RemoteEntitySyncObject>();

            if (rootEntitySync == null || !rootEntitySync.IsEntityValid)
            {
                Debug.LogError($"Can't find sharing target off of '{root?.name ?? "NULL"}'. Can't find a valid root RemoteEntitySyncObject.");
                return(null);
            }

            Entity parentEntity = rootEntitySync.Entity;
            Entity resultEntity = parentEntity;

            int[] childIndices      = target.Address;
            int   childIndicesCount = childIndices?.Length ?? 0;

            for (int i = 0; i < childIndicesCount; i++)
            {
                if (parentEntity == null)
                {
                    Debug.LogError($"Can't find sharing target off of '{root.name}'. The hierarchy was too shallow.");
                    resultEntity = null;
                    break;
                }

                int index = childIndices[i];
                if (parentEntity.Children.Count <= index)
                {
                    Debug.LogError($"Can't find sharing target off of '{root.name}'. The a parent didn't have enough children. Was excepted a child at index '{index}'");
                    resultEntity = null;
                    break;
                }

                resultEntity = parentEntity.Children[index];
                parentEntity = resultEntity;
            }

            return(resultEntity);
        }
        /// <summary>
        /// Given an ISharingServiceTarget, find the nearest SharingTargetRoot that has the target as a child.
        /// </summary>
        private static SharingTargetRoot FindRootTarget(ISharingServiceTarget target)
        {
            if (target == null)
            {
                return(null);
            }

            SharingTargetRoot result = null;

            SharingTargetRoot[] roots = Component.FindObjectsOfType <SharingTargetRoot>();
            int rootsLength           = roots.Length;

            for (int i = 0; i < rootsLength; i++)
            {
                SharingTargetRoot current = roots[i];
                if ((current.InnerTarget != null) &&
                    (current.InnerTarget == target || current.InnerTarget == target.Root))
                {
                    result = current;
                    break;
                }
            }
            return(result);
        }
        /// <summary>
        /// Create a new ISharingServiceTarget from a given Azure Remote Rendering Entity object.
        /// </summary>
        public static ISharingServiceTarget CreateTarget(Entity child)
        {
            SharingTargetRoot root = child?.GetExistingParentGameObject()?.GetComponentInParent <SharingTargetRoot>();

            return(CreateTarget(root, child));
        }