Last active
May 5, 2021 09:56
-
-
Save shivaduke28/94f7bd1a7dcbbc94fa15ce819d24a3b4 to your computer and use it in GitHub Desktop.
IResourceProvider実装
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System; | |
using UnityEngine.ResourceManagement.ResourceProviders; | |
using UnityEngine.ResourceManagement.ResourceLocations; | |
using UnityEngine.Video; | |
using Cysharp.Threading.Tasks; | |
namespace Hoge | |
{ | |
public class DiskVideoPlayerProvider : MonoBehaviour, IResourceProvider | |
{ | |
#region IResourceProvider | |
public static readonly string Id = typeof(VideoPlayer).FullName; | |
public string ProviderId => VideoPlayerProvider.Id; | |
public ProviderBehaviourFlags BehaviourFlags => ProviderBehaviourFlags.None; | |
public bool CanProvide(Type type, IResourceLocation location) => GetDefaultType(location).IsAssignableFrom(type); | |
public Type GetDefaultType(IResourceLocation location) => typeof(VideoPlayer); | |
public void Provide(ProvideHandle provideHandle) | |
{ | |
InternalProvideAsync(provideHandle).Forget(); | |
} | |
public void Release(IResourceLocation location, object asset) | |
{ | |
if (asset is VideoPlayer videoPlayer) | |
{ | |
Destroy(videoPlayer.targetTexture); | |
Destroy(videoPlayer.gameObject); | |
return; | |
} | |
} | |
#endregion | |
private const int TextureSize = 1024; | |
private async UniTaskVoid InternalProvideAsync(ProvideHandle provideHandle) | |
{ | |
var location = provideHandle.Location; | |
var go = new GameObject(location.PrimaryKey); | |
go.transform.SetParent(transform); | |
var videoPlayer = go.AddComponent<VideoPlayer>(); | |
var renderTexture = new RenderTexture(TextureSize, TextureSize, 0); | |
videoPlayer.renderMode = VideoRenderMode.RenderTexture; | |
videoPlayer.targetTexture = renderTexture; | |
videoPlayer.url = location.InternalId; | |
videoPlayer.isLooping = true; | |
videoPlayer.SetDirectAudioMute(0, true); | |
videoPlayer.Prepare(); | |
while (!videoPlayer.isPrepared) await UniTask.Yield(); | |
videoPlayer.Play(); | |
provideHandle.Complete(videoPlayer, true, null); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.ResourceManagement; | |
using UnityEngine.Video; | |
using UnityEngine.ResourceManagement.ResourceLocations; | |
using System; | |
namespace Hoge | |
{ | |
public class TestController : MonoBehaviour | |
{ | |
[SerializeField] private VideoPlayerProvider videoPlayerProvider; | |
private ResourceManager resourceManager; | |
private void Awake() | |
{ | |
resourceManager = new ResourceManager(); | |
resourceManager.ResourceProviders.Add(videoPlayerProvider); | |
var url = "Users/Hoge/hogehoge.mp4"; | |
var location = new ResourceLocationBase(url, url, VideoPlayerProvider.Id, typeof(VideoPlayer)); | |
var handle = resourceManager.ProvideResource<VideoPlayer>(location); | |
handle.Completed += (x) => { }; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment