Skip to content

Instantly share code, notes, and snippets.

@divide-by-zero
Last active July 21, 2017 05:02
Show Gist options
  • Save divide-by-zero/cf819ff141f00452c847c6a3a587b803 to your computer and use it in GitHub Desktop.
Save divide-by-zero/cf819ff141f00452c847c6a3a587b803 to your computer and use it in GitHub Desktop.
Unity2017で始めるTask(async~await) ref: http://qiita.com/divideby_zero/items/b6bbcc35aef08e1d7d3e
void Start ()
{
button?.onClick.AddListener(async() => {
var btex = await BuildTexture.CreateBlurTextureAsync(spriteRenderer.sprite.texture, 32.0f, false);
spriteRenderer.sprite = Sprite.Create(btex, spriteRenderer.sprite.rect, new Vector2(0.5f, 0.5f));
});
}
void Start ()
{
button?.onClick.AddListener(() => {
var btex = await BuildTexture.CreateBlurTextureAsync(spriteRenderer.sprite.texture, 32.0f, false);
spriteRenderer.sprite = Sprite.Create(btex, spriteRenderer.sprite.rect, new Vector2(0.5f, 0.5f));
});
}
using System;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
public class DotNet46Test : MonoBehaviour
{
public Button btn;//InspectorからuGUIのButtonをセットしておく
private void Start()
{
btn.onClick.AddListener(()=>Func1());
}
private void Func1()
{
LogOutput("---Start---");
for (var i = 0; i < 10; ++i)
{
Task.Delay(1000);
LogOutput($"Count:{i}");
}
LogOutput("---End---");
}
private void LogOutput(string message)
{
Debug.Log(DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss") + "\t" + message);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;
public static class BuildTexture
{
private static Dictionary<Texture2D, Texture2D> _textureCache = new Dictionary<Texture2D, Texture2D>();
public static Texture2D CreateBlurTexture(Texture2D tex, float sig, bool isCache = true)
{
if (isCache && _textureCache.ContainsKey(tex)) return _textureCache[tex];
int W = tex.width;
int H = tex.height;
int Wm = (int)(Mathf.Ceil(3.0f * sig) * 2 + 1);
int Rm = (Wm - 1) / 2;
//フィルタ
float[] msk = new float[Wm];
sig = 2 * sig * sig;
float div = Mathf.Sqrt(sig * Mathf.PI);
//フィルタの作成
for (int x = 0; x < Wm; x++)
{
int p = (x - Rm) * (x - Rm);
msk[x] = Mathf.Exp(-p / sig) / div;
}
var src = tex.GetPixels(0).Select(x => x.a).ToArray();
var tmp = new float[src.Length];
var dst = new Color[src.Length];
//垂直方向
for (int x = 0; x < W; x++)
{
for (int y = 0; y < H; y++)
{
float sum = 0;
for (int i = 0; i < Wm; i++)
{
int p = y + i - Rm;
if (p < 0 || p >= H) continue;
sum += msk[i] * src[x + p * W];
}
tmp[x + y * W] = sum;
}
}
//水平方向
for (int x = 0; x < W; x++)
{
for (int y = 0; y < H; y++)
{
float sum = 0;
for (int i = 0; i < Wm; i++)
{
int p = x + i - Rm;
if (p < 0 || p >= W) continue;
sum += msk[i] * tmp[p + y * W];
}
dst[x + y * W] = new Color(1, 1, 1, sum);
}
}
var createTexture = new Texture2D(W, H);
createTexture.SetPixels(dst);
createTexture.Apply();
if (isCache)
{
_textureCache.Add(tex, createTexture);
}
return createTexture;
}
public static void Release()
{
_textureCache.Clear();
}
}
using System;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
public class BlurTest : MonoBehaviour
{
public Button button;
public SpriteRenderer spriteRenderer;
void Start ()
{
button?.onClick.AddListener(() => {
var btex = BuildTexture.CreateBlurTexture(spriteRenderer.sprite.texture, 32.0f, false);
spriteRenderer.sprite = Sprite.Create(btex, spriteRenderer.sprite.rect, new Vector2(0.5f, 0.5f));
});
}
}
Task.Run(() => {
//垂直方向
//<省略>
//水平方向
//<省略>
});
await Task.Run(() => {
//垂直方向
//<省略>
//水平方向
//<省略>
});
public static async Texture2D CreateBlurTexture(Texture2D tex, float sig, bool isCache = true)
public static async Task<Texture2D> CreateBlurTextureAsync(Texture2D tex, float sig, bool isCache = true)
private void Start()
{
// btn.onClick.AddListener(()=>Func1());
btn.onClick.AddListener(()=>Func2());
}
private void Func2()
{
LogOutput("---Start---");
for (var i = 0; i < 10; ++i)
{
Task.Delay(1000).ContinueWith(t => LogOutput($"Count:{i}"));
}
LogOutput("---End---");
}
Task.Run(()=>Debug.Log(transform.position));
Task.Run(()=>Debug.Log(transform.position));
private IEnumerable CountIterator()
{
LogOutput("---Start---");
for (var i = 0; i < 10; ++i)
{
yield return new WaitForSeconds(1);
LogOutput($"Count:{i}");
}
LogOutput("---End---");
}
private void Func3()
{
LogOutput("---Start---");
for (var i = 0; i < 10; ++i)
{
await Task.Delay(1000);
LogOutput($"Count:{i}");
}
LogOutput("---End---");
}
private async void Func3()
{
LogOutput("---Start---");
for (var i = 0; i < 10; ++i)
{
await Task.Delay(1000);
LogOutput($"Count:{i}");
}
LogOutput("---End---");
}
private void Start()
{
//btn.onClick.AddListener(()=>Func1());
//btn.onClick.AddListener(()=>Func2());
btn.onClick.AddListener(()=>Func3());
}
private async Task Func3()
{
LogOutput("---Start---");
for (var i = 0; i < 10; ++i)
{
await Task.Delay(1000);
LogOutput($"Count:{i}");
}
LogOutput("---End---");
}
using System;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
public class DotNet46Test : MonoBehaviour {
public Button btn;//InspectorからuGUIのButtonをセットしておく
private void Start()
{
LogOutput("MainThreadID");
btn.onClick.AddListener(() => Func3());
}
private async Task Func3()
{
LogOutput("---Start---");
for (var i = 0; i < 10; ++i)
{
await Task.Delay(1000).ContinueWith(t=>LogOutput($"ContinueWith:{i}"));
LogOutput($"Count:{i}");
}
LogOutput("---End---");
}
private void LogOutput(string message)
{
Debug.Log(DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss") + "\t" + message + ":" + Thread.CurrentThread.ManagedThreadId);
}
}
void Start ()
{
button?.onClick.AddListener(() => {
var btex = BuildTexture.CreateBlurTexture(spriteRenderer.sprite.texture, 32.0f, false);
spriteRenderer.sprite = Sprite.Create(btex, spriteRenderer.sprite.rect, new Vector2(0.5f, 0.5f));
});
}
2017/07/18 10:58:35 MainThreadID:1
2017/07/18 10:58:38 ---Start---:1
2017/07/18 10:58:39 ContinueWith:0:83
2017/07/18 10:58:40 Count:0:1
2017/07/18 10:58:41 ContinueWith:1:82
2017/07/18 10:58:41 Count:1:1
2017/07/18 10:58:49 ContinueWith:9:43
2017/07/18 10:58:49 Count:9:1
2017/07/18 10:58:49 ---End---:1
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
public class ContextTest : MonoBehaviour
{
void Start()
{
var context = SynchronizationContext.Current;
Task.Run(async () => {
await Task.Delay(1000);
//Debug.Log(transform.position); //ここだと別スレッドなのでエラー
context.Post(state => Debug.Log(transform.position), null);
});
}
}
void Start()
{
var context = SynchronizationContext.Current;
Task.Run(async () => {
SynchronizationContext.SetSynchronizationContext(context);
//Debug.Log(transform.position); //ここだとawait前なのでエラー
await Task.Delay(1000);
Debug.Log(transform.position); //ここだとOK
});
}
//垂直方向
for (int x = 0; x < W; x++)
{
for (int y = 0; y < H; y++)
{
float sum = 0;
for (int i = 0; i < Wm; i++)
{
int p = y + i - Rm;
if (p < 0 || p >= H) continue;
sum += msk[i] * src[x + p * W];
}
tmp[x + y * W] = sum;
}
}
//水平方向
for (int x = 0; x < W; x++)
{
for (int y = 0; y < H; y++)
{
float sum = 0;
for (int i = 0; i < Wm; i++)
{
int p = x + i - Rm;
if (p < 0 || p >= W) continue;
sum += msk[i] * tmp[p + y * W];
}
dst[x + y * W] = new Color(1, 1, 1, sum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment