Example #1
0
        private IEnumerator ShowAvatarByCode(string sCode)
        {
            // with known avatar code we can get TexturedMesh for head in order to show it further
            var avatarHeadRequest = avatarProvider.GetHeadMeshAsync(sCode, true);

            yield return(AwaitRoutine(avatarHeadRequest));

            TexturedMesh headTexturedMesh = avatarHeadRequest.Result;

            TexturedMesh haircutTexturedMesh = null;
            // get identities of all haircuts available for the generated avatar
            var haircutsIdRequest = avatarProvider.GetHaircutsIdAsync(sCode);

            yield return(AwaitRoutine(haircutsIdRequest));

            // randomly select a haircut
            var haircuts   = haircutsIdRequest.Result;
            var haircutIdx = 0;

            if (haircuts != null && haircuts.Length > 0)
            {
                haircutIdx = UnityEngine.Random.Range(0, haircuts.Length);
                var haircut = haircuts[haircutIdx];

                // load TexturedMesh for the chosen haircut
                var haircutRequest = avatarProvider.GetHaircutMeshAsync(sCode, haircut);
                yield return(AwaitRoutine(haircutRequest));

                haircutTexturedMesh = haircutRequest.Result;
            }

            var avatarInfo = CreateHead(sCode, headTexturedMesh, haircutTexturedMesh);

            avatarInfo.code                    = sCode;
            avatarInfo.name                    = ReadAvatarNameByCode(sCode);
            avatarInfo.haircuts                = haircuts;
            avatarInfo.selectedHairstyle       = haircutIdx;
            avatarInfo.transform.position      = Vector3.zero;
            avatarInfo.transform.localRotation = avatarInitialRotation;

            Selection.activeGameObject = avatarInfo.gameObject;

            SceneView sceneView = SceneView.lastActiveSceneView;

            if (sceneView == null)
            {
                Type GameViewType = System.Type.GetType("UnityEditor.GameView,UnityEditor");
                sceneView = EditorWindow.GetWindow <SceneView>(new Type[] { GameViewType });
            }

            if (sceneView != null)
            {
                Camera sceneCam = sceneView.camera;
                sceneView.pivot = Vector3.zero;
                sceneView.size  = cameraOffset;
                sceneView.LookAt(Vector3.zero, Quaternion.identity);
            }

            Tools.current = Tool.Rotate;
        }
        /// <summary>
        /// To make Getting Started sample as simple as possible all code required for creating and
        /// displaying an avatar is placed here in a single function. This function is also a good example of how to
        /// chain asynchronous requests, just like in traditional sequential code.
        /// </summary>
        protected virtual IEnumerator GenerateAndDisplayHead(byte[] photoBytes, PipelineType pipeline)
        {
            // Choose default set of resources to generate
            var resourcesRequest = avatarProvider.ResourceManager.GetResourcesAsync(AvatarResourcesSubset.DEFAULT, pipelineType);

            yield return(Await(resourcesRequest));

            // generate avatar from the photo and get its code in the Result of request
            var initializeRequest = avatarProvider.InitializeAvatarAsync(photoBytes, "name", "description", pipeline, resourcesRequest.Result);

            yield return(Await(initializeRequest));

            string avatarCode = initializeRequest.Result;

            StartCoroutine(SampleUtils.DisplayPhotoPreview(avatarCode, photoPreview));

            var calculateRequest = avatarProvider.StartAndAwaitAvatarCalculationAsync(avatarCode);

            yield return(Await(calculateRequest));

            // with known avatar code we can get TexturedMesh for head in order to show it further
            var avatarHeadRequest = avatarProvider.GetHeadMeshAsync(avatarCode, false);

            yield return(Await(avatarHeadRequest));

            TexturedMesh headTexturedMesh = avatarHeadRequest.Result;

            TexturedMesh haircutTexturedMesh = null;
            // get identities of all haircuts available for the generated avatar
            var haircutsIdRequest = avatarProvider.GetHaircutsIdAsync(avatarCode);

            yield return(Await(haircutsIdRequest));

            // randomly select a haircut
            var haircuts = haircutsIdRequest.Result;

            if (haircuts != null && haircuts.Length > 0)
            {
                var haircutIdx = UnityEngine.Random.Range(0, haircuts.Length);
                var haircut    = haircuts[haircutIdx];

                // load TexturedMesh for the chosen haircut
                var haircutRequest = avatarProvider.GetHaircutMeshAsync(avatarCode, haircut);
                yield return(Await(haircutRequest));

                haircutTexturedMesh = haircutRequest.Result;
            }

            DisplayHead(headTexturedMesh, haircutTexturedMesh);
        }
Example #3
0
        private IEnumerator GetHaircutsIdsFunc(string avatarCode, AsyncRequest <string[]> request)
        {
            string[] haircuts = null;
            if (cachedHaircuts.ContainsKey(avatarCode))
            {
                haircuts = cachedHaircuts[avatarCode];
            }
            else
            {
                var haircutsRequest = avatarProvider.GetHaircutsIdAsync(avatarCode);
                yield return(request.AwaitSubrequest(haircutsRequest, 1.0f));

                if (request.IsError)
                {
                    yield break;
                }

                haircuts = ReorderHaircutIds(haircutsRequest.Result);
                cachedHaircuts[avatarCode] = haircuts;
            }
            request.IsDone = true;
            request.Result = haircuts;
        }
Example #4
0
        private IEnumerator ShowAvatarByCode(string avatarCode)
        {
            // with known avatar code we can get TexturedMesh for head in order to show it further
            var avatarHeadRequest = avatarProvider.GetHeadMeshAsync(avatarCode, true);

            yield return(AwaitRoutine(avatarHeadRequest));

            TexturedMesh headTexturedMesh = avatarHeadRequest.Result;

            TexturedMesh haircutTexturedMesh = null;
            // get identities of all haircuts available for the generated avatar
            var haircutsIdRequest = avatarProvider.GetHaircutsIdAsync(avatarCode);

            yield return(AwaitRoutine(haircutsIdRequest));

            ModelInfo modelInfo = CoreTools.GetAvatarModelInfo(avatarCode);

            // select predicted haircut
            var haircuts   = haircutsIdRequest.Result.ToList();
            var haircutIdx = 0;

            if (haircuts != null && haircuts.Count > 0 && !string.IsNullOrEmpty(modelInfo.haircut_name))
            {
                haircutIdx = haircuts.FindIndex(h => h.Contains(modelInfo.haircut_name));

                if (haircutIdx >= 0)
                {
                    // load TexturedMesh for the chosen haircut
                    var haircutRequest = avatarProvider.GetHaircutMeshAsync(avatarCode, haircuts[haircutIdx]);
                    yield return(AwaitRoutine(haircutRequest));

                    haircutTexturedMesh = haircutRequest.Result;
                }
            }

            var avatarInfo = CreateHead(avatarCode, headTexturedMesh, haircutTexturedMesh, modelInfo);

            avatarInfo.code                    = avatarCode;
            avatarInfo.name                    = ReadAvatarNameByCode(avatarCode);
            avatarInfo.haircuts                = haircutsIdRequest.Result;
            avatarInfo.selectedHairstyle       = haircutIdx;
            avatarInfo.transform.position      = Vector3.zero;
            avatarInfo.transform.localRotation = Quaternion.identity;

            Selection.activeGameObject = avatarInfo.gameObject;

            SceneView sceneView = SceneView.lastActiveSceneView;

            if (sceneView == null)
            {
                Type GameViewType = System.Type.GetType("UnityEditor.GameView,UnityEditor");
                sceneView = EditorWindow.GetWindow <SceneView>(new Type[] { GameViewType });
            }

            if (sceneView != null)
            {
                Camera sceneCam = sceneView.camera;
                sceneView.pivot = Vector3.zero;
                sceneView.size  = cameraOffset;
                sceneView.LookAt(Vector3.zero, Quaternion.identity);
            }

            Tools.current = Tool.Rotate;
        }