code ソーシャル

Unityアプリcodetips(35)

スポンサーリンク

//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;
}
}

人気の記事

1

皆さん、ついに、エアラインでも、サブスクリプションが始まったのはご存じですか? まだ実験段階ですが、ANAが、定額全国住み放題サービスを提供する「ADDress」と組んで、国内線を4回まで定額利用可能 ...

2

無料でネットショップを開けるアプリとして多くの人に驚きを与えたBASE株式会社が、2019年10月25日東証マザーズに上場しました。2020年2月時点で90万店を超えるショップを抱えるまでに成長してい ...

3

2011年にサービスを開始してから圧倒的な成長率を誇るインテリア通販サイト 【FLYMEe/フライミー】を皆さんご存じでしょうか。 「自分のイメージするインテリア、本当に欲しいインテリアがどこにあるの ...

4

ついに、noteの月間アクティブユーザー数が4400万人(2020年3月時点)に到達しました。 そもそも、「note」とは、クリエイターが、文章やマンガ、写真、音声を投稿することができ、ユーザーはその ...

5

ボードゲームカフェが1日2回転で儲かるという記事をみつけたので興味を持ち、調べてみました。 まずは、需要がどれくらいあるのか、市場のようすからみていきましょう。 世界最大のボードゲーム市場はドイツで、 ...

-code, ソーシャル
-,

Copyright© BUSINESS HUNTER , 2023 All Rights Reserved Powered by AFFINGER5.