Skip to content

Commit

Permalink
Merge pull request #2 from chiepomme/gif
Browse files Browse the repository at this point in the history
Gif に対応した
  • Loading branch information
chiepomme authored Jul 24, 2017
2 parents 4b4ec1f + 4d30327 commit e4608e3
Show file tree
Hide file tree
Showing 61 changed files with 3,402 additions and 4 deletions.
41 changes: 38 additions & 3 deletions Assets/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class Controller : MonoBehaviour

string rootDirectory;

GifPlayer gifPlayer;

string GetFilePath(string relativePathFromRoot)
{
if (relativePathFromRoot.StartsWith("./"))
Expand All @@ -41,8 +43,6 @@ IEnumerator Start()
useWebMInsteadOfMp4 = true;
}

Screen.SetResolution(512, 512, false);

if (Application.platform == RuntimePlatform.OSXPlayer)
{
// OSX の場合にはアプリが別のパスに隔離されてしまうので問い合わせが必要
Expand All @@ -58,6 +58,7 @@ IEnumerator Start()
PlayerPrefs.SetString("RootDirectory", rootDirectory);
}


print("rootDir:" + rootDirectory);

if (File.Exists(GetFilePath("background.png")))
Expand All @@ -66,21 +67,37 @@ IEnumerator Start()
var imageWWW = new WWW("file://" + GetFilePath("background.png"));
yield return imageWWW;
image.texture = imageWWW.texture;
SetResolution(imageWWW.texture.width, imageWWW.texture.height);
}
else if (File.Exists(GetFilePath("background.jpg")))
{
print(GetFilePath("background.jpg"));
var imageWWW = new WWW("file://" + GetFilePath("background.jpg"));
yield return imageWWW;
image.texture = imageWWW.texture;
SetResolution(imageWWW.texture.width, imageWWW.texture.height);
}
else if (File.Exists(GetFilePath("background.gif")))
{
print(GetFilePath("background.gif"));
var imageWWW = new WWW("file://" + GetFilePath("background.gif"));
yield return imageWWW;
yield return StartCoroutine(UniGif.GetTextureListCoroutine(imageWWW.bytes, (gifTexList, loopCount, width, height) =>
{
SetResolution(width, height);
gifPlayer = GifPlayer.Create(image, gifTexList);
}));
}
else
{
SetResolution(720, 304);
}

print(GetFilePath("audio.wav"));
var audiosource = gameObject.AddComponent<AudioSource>();
var audioWWW = new WWW("file://" + GetFilePath("audio.wav"));
yield return audioWWW;
audiosource.clip = audioWWW.GetAudioClip(false);
audiosource.Play();

var recorder = mp4Recorder;

Expand All @@ -92,6 +109,11 @@ IEnumerator Start()
recorder.outputDir = new DataPath(GetFilePath("Movie"));

recorder.BeginRecording();
if (gifPlayer != null)
{
gifPlayer.PlaySyncWith(audiosource);
}
audiosource.Play();
print(LastPath);
yield return new WaitWhile(() => audiosource.isPlaying);
recorder.EndRecording();
Expand All @@ -110,4 +132,17 @@ IEnumerator Start()

Application.Quit();
}

void SetResolution(int width, int height)
{
const int MinimumResolution = 720;
if (width < MinimumResolution || height < MinimumResolution)
{
var scale = (float)MinimumResolution / Mathf.Min(width, height);
width = Mathf.RoundToInt(width * scale);
height = Mathf.RoundToInt(height * scale);
}

Screen.SetResolution(width, height, false);
}
}
61 changes: 61 additions & 0 deletions Assets/GifPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using UTJ.FrameCapturer;

public class GifPlayer : MonoBehaviour
{
public static GifPlayer Create(RawImage image, List<UniGif.GifTexture> textures)
{
var player = new GameObject("GifPlayer").AddComponent<GifPlayer>();
player.Initialize(image, textures);
return player;
}

class SequenceData
{
public readonly Texture2D texture;
public readonly float cumulativeSec;

public SequenceData(Texture2D texture, float cumulativeSec)
{
this.texture = texture;
this.cumulativeSec = cumulativeSec;
}
}

RawImage image;

float lengthSec;
List<SequenceData> sequence = new List<SequenceData>();
MovieRecorder recorder;

AudioSource audioSource;

void Initialize(RawImage image, List<UniGif.GifTexture> textures)
{
var cumulativeSec = 0f;
foreach (var texture in textures)
{
sequence.Add(new SequenceData(texture.m_texture2d, cumulativeSec));
cumulativeSec += texture.m_delaySec;
}
lengthSec = cumulativeSec;

this.image = image;
this.image.texture = sequence[0].texture;
}

public void PlaySyncWith(AudioSource audioSource)
{
this.audioSource = audioSource;
}

void Update()
{
if (sequence.Count == 1 || audioSource == null) return;
var currentSec = audioSource.time % lengthSec;
image.texture = sequence.Last(s => s.cumulativeSec <= currentSec).texture;
}
}
12 changes: 12 additions & 0 deletions Assets/GifPlayer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Assets/Main.unity
Binary file not shown.
9 changes: 9 additions & 0 deletions Assets/UniGif.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions Assets/UniGif/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# ---------------[ Unity generated ]------------------ #
[Aa]pps/
[Ll]ibrary/
[Tt]emp/
[Oo]bj/
[Ss]treamingAssets/
[Ss]treamingAssets.meta
UnityGenerated/

# ----[ Visual Studio / MonoDevelop generated ]------- #

ExportedObj/
*.svd
*.userprefs
*.csproj
*.pidb
*.suo
*.sln
*.user
*.unityproj
*.booproj
[Uu]nityVS/
[Uu]nityVS.meta

# -------------[ OS generated ]------------------------ #
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
Icon?
ehthumbs.db
Thumbs.db
9 changes: 9 additions & 0 deletions Assets/UniGif/Assets.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Assets/UniGif/Assets/UniGif.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions Assets/UniGif/Assets/UniGif/UniGif.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
UniGif
Copyright (c) 2015 WestHillApps (Hironari Nishioka)
This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
*/

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static partial class UniGif
{
/// <summary>
/// Get GIF texture list Coroutine
/// </summary>
/// <param name="bytes">GIF file byte data</param>
/// <param name="callback">Callback method(param is GIF texture list, Animation loop count, GIF image width (px), GIF image height (px))</param>
/// <param name="filterMode">Textures filter mode</param>
/// <param name="wrapMode">Textures wrap mode</param>
/// <param name="debugLog">Debug Log Flag</param>
/// <returns>IEnumerator</returns>
public static IEnumerator GetTextureListCoroutine(
byte[] bytes,
Action<List<GifTexture>, int, int, int> callback,
FilterMode filterMode = FilterMode.Bilinear,
TextureWrapMode wrapMode = TextureWrapMode.Clamp,
bool debugLog = false)
{
int loopCount = -1;
int width = 0;
int height = 0;

// Set GIF data
var gifData = new GifData();
if (SetGifData(bytes, ref gifData, debugLog) == false)
{
Debug.LogError("GIF file data set error.");
if (callback != null)
{
callback(null, loopCount, width, height);
}
yield break;
}

// Decode to textures from GIF data
List<GifTexture> gifTexList = null;
yield return DecodeTextureCoroutine(gifData, result => gifTexList = result, filterMode, wrapMode);

if (gifTexList == null || gifTexList.Count <= 0)
{
Debug.LogError("GIF texture decode error.");
if (callback != null)
{
callback(null, loopCount, width, height);
}
yield break;
}

loopCount = gifData.m_appEx.loopCount;
width = gifData.m_logicalScreenWidth;
height = gifData.m_logicalScreenHeight;

if (callback != null)
{
callback(gifTexList, loopCount, width, height);
}

yield break;
}
}
8 changes: 8 additions & 0 deletions Assets/UniGif/Assets/UniGif/UniGif.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e4608e3

Please sign in to comment.