Example #1
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;
        }