Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Windows APIでUnityでもできることを広げよう

Windows APIでUnityでもできることを広げよう

xR Tech Nagoya #2
2019/06/28

Avatar for Kirurobo

Kirurobo

June 28, 2019
Tweet

More Decks by Kirurobo

Other Decks in Programming

Transcript

  1. でも、C++ とかでプラグイン 別途ビルドが必要では?  Unity上でC#スクリプトを作るだけでも可  ※ 実際はC#の知識だけでは無理がありますが… 4 //

    WindowRotator.cs ウィンドウをぐるぐる回す using UnityEngine; using System; using System.Runtime.InteropServices; public class WindowRotator : MonoBehaviour { [DllImport("user32.dll")] static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWIA, int x, int y, int cx, int cy, uint uFlags); [DllImport("user32.dll")] static extern IntPtr GetActiveWindow(); IntPtr hWnd; void Awake() { hWnd = GetActiveWindow(); } void Update() { int x = (int)(200f + 100f * Mathf.Cos(Time.time)); int y = (int)(200f + 100f * Mathf.Sin(Time.time)); SetWindowPos(hWnd, IntPtr.Zero, x, y, 0, 0, 0x4015); } } 例: WindowRotator.cs このスクリプトを何かにアタッチ
  2. でも、C++ とかでプラグイン 別途ビルドが必要では?  Unity上でC#スクリプトを作るだけでも可  ※ 実際はC#の知識だけでは無理がありますが… 5 //

    WindowRotator.cs ウィンドウをぐるぐる回す using UnityEngine; using System; using System.Runtime.InteropServices; public class WindowRotator : MonoBehaviour { [DllImport("user32.dll")] static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWIA, int x, int y, int cx, int cy, uint uFlags); [DllImport("user32.dll")] static extern IntPtr GetActiveWindow(); IntPtr hWnd; void Awake() { hWnd = GetActiveWindow(); } void Update() { int x = (int)(200f + 100f * Mathf.Cos(Time.time)); int y = (int)(200f + 100f * Mathf.Sin(Time.time)); SetWindowPos(hWnd, IntPtr.Zero, x, y, 0, 0, 0x4015); } } ウィンドウが勝手に移動
  3. DLLはWindowsが 用意してくれている 6 [DllImport("user32.dll")] static extern bool SetWindowPos(IntPtr hWnd, IntPtr

    hWIA, int x, int y, int cx, int cy, uint uFlags); Pluginsフォルダに置かずに呼び出せる
  4. ゆにうぃなぴ UniWinApi  https://github.com/kirurobo/UniWinApi 本体はこっち  https://github.com/kirurobo/UniWinApiAsset  CC0 (パブリックドメイン)※本体について

     クレジット表示も不要で自由に使える  VRMファイルビューアのサンプルを用意 8 色々寄せ集めて 公開したもの
  5. UniWinApi の注意点  Unityエディタでは無効な機能がある  ウィンドウ透過はビルドしないと使えない  個人の実験的プロジェクト  これできるかな、と試しているもの

     質は保証できない  仕様が勝手に変わったりする  サブモジュールに分かれている  それぞれプルが必要 9 そのままアプリに使える 自信はないのでCC0なのです
  6. 公開した狙い  もっと良いライブラリを誰か作ってほしい  例 @satoya_123 さん Desktop Mascot Maker

    みたいな  新しめの Unity でも対応いただけると…  需要や、もっと良い実装を知りたい  schedul-xor さん  タッチ対応について  @baku_dreameater さん  ヒットテストの ReadPixels 遅いという話  スマートな半透明処理が参考になります! 10
  7. 12

  8. ファイルを開くダイアログ  UnityStandaloneFileBrowser  gkngkc さん  MIT ライセンス 

    Windows / Mac / Linux でネイティブのファイ ル選択ダイアログを出せる  WebGL でも出せる 16