Example #1
0
        private static IEnumerator CreateClip(MemoryStream ms, int offset, string name, GameObject target)
        {
            if (!WaveUtil.TryReadHeader(ms, out var waveHeader))
            {
                Debug.LogWarning("Cannot read wave header.");
                yield break;
            }

            AudioClip audioClip = null;

            yield return(AudioClipMaker.Create(
                             name,
                             ms.GetBuffer(),
                             offset + waveHeader.FormatChunkSize + 28,
                             waveHeader.BitPerSample,
                             waveHeader.DataChunkSize / waveHeader.BlockSize,
                             waveHeader.Channel,
                             waveHeader.SampleRate,
                             false,
                             clip => { audioClip = clip; }));

            if (audioClip == null)
            {
                yield break;
            }

            var audioSource = target.AddComponent <AudioSource>();

            audioSource.clip        = audioClip;
            audioSource.playOnAwake = false;
            audioSource.loop        = false;
        }
Example #2
0
        private static IEnumerator CreateClip(MemoryStream ms, int offset, string name, Action <AudioClip> callback)
        {
            if (!WaveUtil.TryReadHeader(ms, out var waveHeader))
            {
                Debug.LogWarning("Cannot read wave header.");
                yield break;
            }

            yield return(AudioClipMaker.Create(
                             name,
                             ms.GetBuffer(),
                             offset + waveHeader.FormatChunkSize + 28,
                             waveHeader.BitPerSample,
                             waveHeader.DataChunkSize / waveHeader.BlockSize,
                             waveHeader.Channel,
                             waveHeader.SampleRate,
                             false,
                             callback));
        }
Example #3
0
        public IEnumerator SetupCorutine()
        {
            VCIObject = Root.AddComponent <VCIObject>();
            yield return(ToUnity(meta => VCIObject.Meta = meta));

            // Script
            if (GLTF.extensions.VCAST_vci_embedded_script != null)
            {
                VCIObject.Scripts.AddRange(GLTF.extensions.VCAST_vci_embedded_script.scripts.Select(x =>
                {
                    var source = "";
                    try
                    {
                        var bytes = GLTF.GetViewBytes(x.source);
                        source    = Utf8String.Encoding.GetString(bytes.Array, bytes.Offset, bytes.Count);
                    }
                    catch (Exception)
                    {
                        // 握りつぶし
                    }

                    return(new VCIObject.Script
                    {
                        name = x.name,
                        mimeType = x.mimeType,
                        targetEngine = x.targetEngine,
                        source = source
                    });
                }));
            }

            // Audio
            if (GLTF.extensions.VCAST_vci_audios != null &&
                GLTF.extensions.VCAST_vci_audios.audios != null)
            {
                var root = Root;
                foreach (var audio in GLTF.extensions.VCAST_vci_audios.audios)
                {
#if ((NET_4_6 || NET_STANDARD_2_0) && UNITY_2017_1_OR_NEWER)
                    if (Application.isPlaying)
                    {
                        var bytes = GLTF.GetViewBytes(audio.bufferView);
                        WaveUtil.WaveHeaderData waveHeader;
                        var audioBuffer = new byte[bytes.Count];
                        Buffer.BlockCopy(bytes.Array, bytes.Offset, audioBuffer, 0, audioBuffer.Length);
                        if (WaveUtil.TryReadHeader(audioBuffer, out waveHeader))
                        {
                            AudioClip audioClip = null;
                            yield return(AudioClipMaker.Create(
                                             audio.name,
                                             audioBuffer,
                                             44,
                                             waveHeader.BitPerSample,
                                             waveHeader.DataChunkSize / waveHeader.BlockSize,
                                             waveHeader.Channel,
                                             waveHeader.SampleRate,
                                             false,
                                             (clip) => { audioClip = clip; }));

                            if (audioClip != null)
                            {
                                var source = root.AddComponent <AudioSource>();
                                source.clip        = audioClip;
                                source.playOnAwake = false;
                                source.loop        = false;
                            }
                        }
                    }
                    else
#endif
                    {
#if UNITY_EDITOR
                        var audioClip =
                            AssetDatabase.LoadAssetAtPath(AudioAssetPathList[audio.name], typeof(AudioClip)) as
                            AudioClip;
                        if (audioClip != null)
                        {
                            var source = root.AddComponent <AudioSource>();
                            source.clip = audioClip;
                        }
                        else
                        {
                            Debug.LogWarning("AudioFile NotFound: " + audio.name);
                        }
#endif
                    }
                }
            }


            // Animation
            var animation = Root.GetComponent <Animation>();
            if (animation != null)
            {
                animation.playAutomatically = false;
            }

            for (var i = 0; i < GLTF.nodes.Count; i++)
            {
                var node = GLTF.nodes[i];
                var go   = Nodes[i].gameObject;
                if (node.extensions != null)
                {
                    if (node.extensions.VCAST_vci_item != null)
                    {
                        var item = go.AddComponent <VCISubItem>();
                        item.Grabbable      = node.extensions.VCAST_vci_item.grabbable;
                        item.Scalable       = node.extensions.VCAST_vci_item.scalable;
                        item.UniformScaling = node.extensions.VCAST_vci_item.uniformScaling;
                        item.GroupId        = node.extensions.VCAST_vci_item.groupId;
                    }
                }
            }
        }