※APIキーが見えるのであくまでも実験用です※PHPだからクライアントには見えないか,,
流れ
Google Cloud Natural LanguageのAPIキーを取得
(がんばってとってください)
- 作成するとこんな画面がでてきます

- HTTPリファラかIPアドレス制限(サーバのIPアドレス)を使用します

- PHPファイルを作成します(test2.php)
<?php
if(isset($_POST["comment"])){
$comment = $_POST["comment"];
$url = 'https://language.googleapis.com/v1/documents:analyzeSentiment?key=(ここに自分のAPIキーを入れる)';
$document = array('type' =>'PLAIN_TEXT','language' =>'ja','content' =>$comment);
$postdata = array('encodingType' => 'UTF8', 'document' => $document);
$json_post = json_encode($postdata);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_post);
$result = curl_exec($ch);
curl_close($ch);
$result_array = json_decode($result,true);
echo $result_array[documentSentiment][score];
}
?>
サーバにアップロードし,そのページまでのURLを確認します
Unit側のC#ファイルを作成します
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;//<--追加
//using System;
public class test2 : MonoBehaviour
{
public Text ResultText_;
public Text InputText_;
public string ServerAddress;//サーバアドレスをエディタで入力する
// Update is called once per frame
void Update()
{
}
void Start()
{
ResultText_.text = "";//画面表示を消す
}
//UIのボタンにアサインするスクリプト
public void SendSignal_Button_Push()
{
StartCoroutine("post_data");//ボタン押されたらコルーチンを開始
}
private IEnumerator post_data()
{
WWWForm form = new WWWForm();
form.AddField("comment", InputText_.text); //Input Field等から渡された変数をcommentのタグ付けして内容を送信
WWW post = new WWW(ServerAddress, form);
yield return post;//データの戻りを待つ
Debug.Log(post.text);//コンソールに表示(この出力内容はPHPで変更可能)
ResultText_.text = post.text;
}
}
Unityでの操作
- Unityで空のGameObjectを作成し,test2.csを貼り付けます
- PHPへのURLを入力します

- GameObject>UI>InputFiledとGameObject>UI>Textを作成し,下図のように割り当てます

- GameObject>UI>Buttonを作成し,Inspectorからボタンを押せるようにします

- InputFieldはMuli Line Newlineの設定します

- Unityを再生し,文字を入力すると,感情が帰ってきます

- 「課題が終わらない」は-0.2でした
スクリプトのなんとなく対応みたいなの
