//tips
//textと表示アイコンの動きを管理する
表示されるtextの内容に伴ってアイコンの表示タイミングをtextManagerを作成し、管理する。
これはcountでクリックを数え、そのタイミングで表示される文言の内容の動作を行うオブジェクトをactiveにするものである。
今回はボタン枠線とゴール枠線をtest,test1のオブジェクトにヒエラルキーからアタッチした。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TextManager : MonoBehaviour
{
[SerializeField] GameObject test;
[SerializeField] GameObject test1;
int count = 0;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
count++;
}
if (count==1)
{
//ゲームオブジェクト表示→非表示
test.SetActive(true);
}
if (count == 2)
{
//ゲームオブジェクト表示→非表示
test1.SetActive(true);
}
}
}
指のアイコンも追加し、seconds += Time.deltaTime;も使って時間制限も設ける。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TextManager : MonoBehaviour
{
[SerializeField] GameObject test;
[SerializeField] GameObject test1;
[SerializeField] GameObject test2;
int count = 0;
float seconds;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
count++;
}
if (count==1)
{
//ゲームオブジェクト表示→非表示
test.SetActive(true);
}
if (count == 2)
{
//ゲームオブジェクト表示→非表示
test1.SetActive(true);
}
if (count == 3)
{
//ゲームオブジェクト表示→非表示
test2.SetActive(true);
seconds += Time.deltaTime;
if (seconds >= 3)
{
test2.SetActive(false);
}
}
}
}
//タイトル画面編集
タイトル画面をおしゃれにするためにフリーの画像ジェネレーターで、タイトルロゴを作成する。
https://ja.cooltext.com/
タイトル画像の背景は透過させたいので背景透過サイトで編集を行う。
https://www.peko-step.com/tool/alphachannel.html
Canvaに透過ロゴをアップロードするなどして編集。
https://www.canva.com/
//マウスへの画像追従
マウスの位置座標を取得し、さらにスクリーン座標からワールド座標に変換したものをtransform.position を使用してオブジェクトの座標に代入する。
今回はcanvas上のimageにその動きをさせてみた。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MouseFollow2D : MonoBehaviour
{
private Vector3 position;
private Vector3 screenToWorldPointPosition;
private void Update()
{
// Vector3でマウス位置座標を取得する
position = Input.mousePosition;
position.z = 30f;
// マウス位置座標をスクリーン座標からワールド座標に変換する
screenToWorldPointPosition = Camera.main.ScreenToWorldPoint(position);
// ワールド座標に変換されたマウス座標を代入
gameObject.transform.position = screenToWorldPointPosition;
}
}
//マウスが上に乗ったことをトリガーにし、オブジェクトにアクションさせる
マウスが上に乗ったcubeの色を変えてみる。
使用するのはOnMouseEnter()。raycastをわざわざ使う必要がないので大変便利。
コライダーをアタッチするのを忘れない。
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public Renderer rend;
void Start()
{
rend = GetComponent<Renderer>();
}
// The mesh goes red when the mouse is over it...
void OnMouseEnter()
{
rend.material.color = Color.red;
}
// ...the red fades out to cyan as the mouse is held over...
void OnMouseOver()
{
rend.material.color -= new Color(0.1F, 0, 0) * Time.deltaTime;
}
// ...and the mesh finally turns white when the mouse moves away.
void OnMouseExit()
{
rend.material.color = Color.white;
}
}
https://nopitech.com/2018/07/03/post-704/
https://docs.unity3d.com/ja/current/ScriptReference/MonoBehaviour.OnMouseEnter.html
Canvas上のImageに切り替えても同様のことが行える。
ただ、コライダーの大きさはwidth,height分の大きさにする必要がある。
なので今回は100×100×100サイズのboxコライダーをつけている。
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ExampleClass : MonoBehaviour
{
public Color myColor;
private Image image;
void Start()
{
image = GetComponent<Image>();
}
// The mesh goes red when the mouse is over it...
void OnMouseEnter()
{
image.color = Color.red;
}
// ...the red fades out to cyan as the mouse is held over...
void OnMouseOver()
{
image.color -= new Color(0.1F, 0, 0) * Time.deltaTime;
}
// ...and the mesh finally turns white when the mouse moves away.
void OnMouseExit()
{
image.color = Color.white;
}
}