誰も教えてくれないblog

お絵描きとかグラブルとか

えもふりでVtuberの真似事をする⑦ キー入力で表情を変える2

前回までの状態だとウィンドウが非アクティブになると表情が動かせなくなってしまう。

今回は改善を行っていこう。

f:id:GinMei182:20200522182347p:plain

https://github.com/Elringus/UnityRawInput

からUnityRawInputを落としてAsets内へ入れる。

これで準備が整った。

 

あとはtest3内をこう書き換える。

 

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

public class Test3 : MonoBehaviour
{
public EmotePlayer targetPlayer;
public float time;

 


// Start is called before the first frame update
void Start()
{
if (targetPlayer == null)
targetPlayer = this.GetComponent(typeof(EmotePlayer)) as EmotePlayer;
time=600;

}

private void OnEnable()
{
//バックグラウンドの時もキー入力を受け付ける
RawKeyInput.Start( true );
RawKeyInput.OnKeyUp += OnKeyUp ;
RawKeyInput.OnKeyDown += OnKeyDown;
}


// キーが離された時の動作
private void OnKeyUp( RawKey key )
{
Debug.Log( "OnKeyUp: " + key );

}

// キーが押された時の動作
private void OnKeyDown( RawKey key )
{
Debug.Log( "OnKeyDown: " + key );

if (RawKeyInput.IsKeyDown(RawKey.A)) {targetPlayer.mainTimelineLabel="sample_驚00";
time=240;
}
if (RawKeyInput.IsKeyDown(RawKey.S)) {targetPlayer.mainTimelineLabel="sample_楽00";
time=240;
}
if (RawKeyInput.IsKeyDown(RawKey.D)) {targetPlayer.mainTimelineLabel="微笑";
time=240;
}
if (RawKeyInput.IsKeyDown(RawKey.W)) {targetPlayer.mainTimelineLabel="考える";
time=240;
}

}

// Update is called once per frame
void Update()
{
if (time>1) time--;
else targetPlayer.mainTimelineLabel="動作リセット";

}
}

 

動いたからこれでよし。なんか色々変な気はするけど。