Skip to content

Instantly share code, notes, and snippets.

@kidapu
Last active July 20, 2022 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kidapu/fa836fa3806aacf2989f487d1c99cda2 to your computer and use it in GitHub Desktop.
Save kidapu/fa836fa3806aacf2989f487d1c99cda2 to your computer and use it in GitHub Desktop.
DEAD 未来館で使ってる script
amespace dead
{
using System;
using BestHTTP;
using SimpleJSON;
using UnityEngine;
public class SlackReporter
{
// お知らせの種類
public enum SlackNotificationType
{
Error,
Status
};
// -------------------------------------------------------------------
// Instance
private static SlackReporter instance;
public static SlackReporter Instance()
{
if (instance == null) instance = new SlackReporter();
return instance;
}
// -------------------------------------------------------------------
// class
private string _prevMessage = "";
public SlackReporter()
{
}
public void Report(string message)
{
Report(message, SlackNotificationType.Status);
}
public void ErrorReport(string message)
{
// 1個前と同じメッセージだったら送らない
if (message == _prevMessage)
{
return;
}
_prevMessage = message;
Report(message, SlackNotificationType.Error);
}
private void Report(string message, SlackNotificationType slackType)
{
string username = "DEAD未来館-お知らせ";
string color = "info";
string emoji = ":zombie:";
switch (slackType)
{
case SlackNotificationType.Error:
color = "danger";
emoji = ":no_entry:";
break;
case SlackNotificationType.Status:
color = "good";
emoji = ":zombie:";
break;
}
var fields = new JSONArray();
fields.Add(NewField("Date", DateTimeOffset.Now.ToString("yyyy/MM/dd HH:mm:ss.fff")));
var attachments = new JSONArray();
var json = new JSONClass();
json["fields"] = fields;
json["color"] = color;
json["text"] = $"{message}\n "; // 最後に改行いれたいんだけどなんか文字ないと無視されるので全角スペースいれてる
attachments.Add(json);
var data = new JSONClass();
// data["channel"] = CHANNEL_NAME;
data["username"] = username;
data["icon_emoji"] = emoji;
data["attachments"] = attachments;
var payload = data.ToString();
// Debug.Log(payload);
var uri = new System.Uri(Config.Instance.SLACK_WEBHOOK_URL);
HTTPRequest request = new HTTPRequest(uri, HTTPMethods.Post, OnRequestFinished);
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddField("payload", payload);
request.Send();
}
void OnRequestFinished(HTTPRequest req, HTTPResponse res)
{
switch (req.State)
{
case HTTPRequestStates.Finished:
// サーバーからレスポンスが返ってきたらHTTPRequestStates.Finishedになります。
// response.StatusCodeにレスポンスのステータスコードが入ってるので、値に応じた処理を行います。
if (400 <= res.StatusCode)
{
// Debug.Log($"Slack Reporter : {res.DataAsText}");
}
break;
case HTTPRequestStates.Error:
case HTTPRequestStates.Aborted:
case HTTPRequestStates.ConnectionTimedOut:
case HTTPRequestStates.TimedOut:
// リクエストのタイムアウト
// 予期しないエラー
// Debug.Log($"Slack Reporter : {res.DataAsText}");
break;
}
}
JSONClass NewField(string title, string value, bool isShort = true)
{
var field = new JSONClass();
field["title"] = title;
field["value"] = value;
field["short"].AsBool = isShort;
return field;
}
}
}
Shader "Custom/Alpha Multiply Texture"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
// _Color ("Color", Color) = (1,1,1,1)
_Alpha ("Alpha", Range (0, 1)) = 1
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
}
LOD 100
Blend Zero SrcColor // 乗算
Cull Off
ZWrite Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
sampler2D _MainTex;
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
fixed4 color : COLOR;
};
struct v2f {
float4 vertex : SV_POSITION;
float2 texcoord : TEXCOORD0;
UNITY_FOG_COORDS(1)
fixed4 color : COLOR;
};
float4 _MainTex_ST;
float _Alpha;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.color = v.color;
o.color.a = _Alpha;
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
half4 prev = i.color * tex2D(_MainTex, i.texcoord);
fixed4 col = lerp(half4(1,1,1,1), prev, prev.a);
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment