Skip to content

Instantly share code, notes, and snippets.

@sandinist
Last active November 27, 2016 22:11
Show Gist options
  • Save sandinist/c631d426c88af22150ee6964e4774508 to your computer and use it in GitHub Desktop.
Save sandinist/c631d426c88af22150ee6964e4774508 to your computer and use it in GitHub Desktop.
SummonEquiFromVRDesktopExplorer
using UnityEngine;
using System.Collections;
using System;
using System.IO;
public class SummonEqui : MonoBehaviour {
private GameObject theta;
void Update ()
{
if (OVRInput.GetDown(OVRInput.Button.SecondaryThumbstick))
{
StartSummon();
}
}
void StartSummon()
{
// エクスプローラとデスクトップ以外は処理しない
var s = WinApi.GetForegroundProcessClass();
if (s.IndexOf("Progman") == 0 && s.IndexOf("CabinetWClass") == 0) { return; }
// クリップボードにパスを取得
GUIUtility.systemCopyBuffer = "";
WinApi.ClipFilePath();
StartCoroutine(DelayMethod(0.1f, () =>
{
// パスが取れてたら画像取得(エラーは終了)
Texture2D tex;
try
{
string str = GUIUtility.systemCopyBuffer;
string str2 = str.Substring(1, str.Length - 2);
tex = GetImages(str2);
}
catch { return; }
// アスペクト比2:1以外は処理しない
if (tex.width / tex.height != 2) { return; }
// あとは煮るなり焼くなり
GameObject prefab = (GameObject)Resources.Load("Sphere100");
var t = Camera.main.transform;
var p = t.position + t.forward;
GameObject sphere = (GameObject)Instantiate(prefab, p, Quaternion.identity);
sphere.GetComponent<Renderer>().material.mainTexture = tex;
StartCoroutine(ShowSphere(sphere));
}));
}
private IEnumerator DelayMethod(float waitTime, Action action)
{
yield return new WaitForSeconds(waitTime);
action();
}
private IEnumerator ShowSphere(GameObject sphere)
{
var currentScale = 1;
var maxScale = 300;
if (theta != null) { Destroy(theta); }
theta = sphere;
while (currentScale < maxScale)
{
currentScale++;
sphere.transform.localScale = Vector3.one * currentScale;
yield return new WaitForSeconds(0.01f);
}
}
Texture2D GetImages(string path)
{
Texture2D tex = new Texture2D(0, 0);
tex.LoadImage(LoadBytes(path));
return tex;
}
byte[] LoadBytes(string path)
{
Debug.Log(path);
FileStream fs = new FileStream(path, FileMode.Open);
BinaryReader bin = new BinaryReader(fs);
byte[] result = bin.ReadBytes((int)bin.BaseStream.Length);
bin.Close();
return result;
}
}
using UnityEngine;
using System.Runtime.InteropServices;
public class WinAPI : MonoBehaviour {
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
public static string GetForegroundProcessClass()
{
IntPtr hwnd = GetForegroundWindow();
return GetClassName(hwnd);
}
public static string GetClassNameFrom(IntPtr hwnd)
{
int textLen = GetWindowTextLength(hwnd);
if (0 == textLen) return "";
StringBuilder csb = new StringBuilder(256);
GetClassName(hwnd, csb, csb.Capacity);
return csb.ToString();
}
[DllImport("USER32.dll")]
static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
[DllImport("user32.dll")]
public static extern uint keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
private const int MOUSEEVENTF_LEFTUP = 0x0004;
private const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
private const int MOUSEEVENTF_RIGHTUP = 0x0010;
private const int VK_SHIFT = 0x10; //SHIFT key
private const int VK_LSHIFT = 0xA0; //SHIFT key Left
private const int VK_RSHIFT = 0xA1; //SHIFT key Right
private const int VK_A = 0x41; //A key
private const int KEYEVENTF_EXTENDEDKEY = 0x0001;
private const int KEYEVENTF_KEYUP = 0x0002;
public static void ClipFilePath()
{
// Shift+右クリック -> A でパスコピー
keybd_event(VK_LSHIFT, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
keybd_event(VK_LSHIFT, 0x45, KEYEVENTF_KEYUP, 0);
keybd_event(VK_A, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_A, 0x45, KEYEVENTF_KEYUP, 0);
// Shift押したままになる対策
keybd_event(VK_RSHIFT, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_RSHIFT, 0x45, KEYEVENTF_KEYUP, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment