//tips
//Mathf.Clamp01とは
float Clamp01 (float value)はvalueが負の場合は0、0 と 1 の間の値はその値、1より大きい場合は1が返される。
//プロパティの使い方
プロパティを使うメリットは、条件付きで代入・取得時をおこなったり、代入・取得時にデバッグ出力できることがある。
プロパティは
private int score;
public int Score{
get{
return score;
}
set{
score=value;
}
}
このように示されるが、これは一般的に書き直すことができる。
private int score;
//スコアのゲッター
public int GetScore()
{
return score;
}
//スコアのセッター
public void SetScore(int value)
{
score=value;
}
プロパティを使用した方が一つのメソッド内でより簡易的に取り扱うことができることがわかる。
また、プロパティを使用することで下記のように他のクラスのprivate変数にアクセスすることもできる。
public class MyClass :MonoBehaviour
{
void Start()
{
Hoge hoge=new Hoge();
hoge.Score=10;
Debug.Log(hoge.Score);
}
}
public class Hoge
{
private int score;
public int Score{
get{
return score;
}
set{
score=value;
}
}
}
先に述べたように条件付きエラーメッセージを出すことも可能となる。
public class MyClass :MonoBehaviour
{
void Start()
{
Hoge hoge=new Hoge();
hoge.Score=-10;
Debug.Log(hoge.Score);
}
}
public class Hoge
{
[SerializeField]
private int score;
public int Score{
get{
return score;
}
set{
if(value<=0){
Debug.Log("マイナスの値は入力できません");
return;
}
score=value;
}
}
}
インスペクターに値を表示しない場合はプロパティのコードを
public class Hoge
{
public int score{get; set;}
}
までシンプルにできる。
このため、private変数をboolメソッドに組み込む場合に、
public bool IsCompleteDisplayText
{
get { return Time.time > timeElapsed + timeUntilDisplay; }
}
とし、
if( IsCompleteDisplayText ){
if(currentLine < scenarios.Length && Input.GetMouseButtonDown(0)){
SetNextLine();
}
}else{
// 完了してないなら文字をすべて表示する
if(Input.GetMouseButtonDown(0)){
timeUntilDisplay = 0;
}
}
Ifの条件として設定することができる。
//ゲームオブジェクトの表示・非表示
Tutorialで枠線などの部分表示を行うためオブジェクトの表示・非表示を確認する。
SetActiveで非表示にすると下記のようになる。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TextDisplay : MonoBehaviour
{
void Update()
{
//[D]キーを押す
if (Input.GetKeyDown(KeyCode.D))
{
//ゲームオブジェクト表示→非表示
this.gameObject.SetActive(false);
}
}
}
次にオブジェクトを非表示から表示へ切り替る。
ゲームオブジェクトを非表示にしている状態では、スクリプト内でゲームオブジェクトを読み込むためのFindメソッドが使えないので、事前に、SerializeFieldを使用して、非アクティブなゲームオブジェクトをInspector内で読み込んでおく必要がある。
空のゲームオブジェクトを用意し、スクリプトをアタッチ、非表示のゲームオブジェクトを子オブジェクトにし、インスペクターのtestオブジェクトにはめ込む。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TextDisplay : MonoBehaviour
{
[SerializeField] GameObject test;
void Update()
{
//[D]キーを押す
if (Input.GetKeyDown(KeyCode.D))
{
//ゲームオブジェクト非表示→表示
test.SetActive(true);
}
}
}
さらに非表示から表示された後に点滅させるために今度はimageの方に点滅用のスクリプトをアタッチする。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Flashing : MonoBehaviour
{
public Color myColor;
public float speed = 1.0f;
private Image image;
private float time;
// Start is called before the first frame update
void Start()
{
image = this.gameObject.GetComponent<Image>();
}
void Update()
{
//オブジェクトのAlpha値を更新
image.color = GetAlphaColor(image.color);
}
Color GetAlphaColor(Color color)
{
time += Time.deltaTime * 5.0f * speed;
color.a = Mathf.Sin(time) * 0.5f + 0.5f;
return color;
}
}
またcanvasのimageで制御するとプレイヤーの移動に沿って動いてしまうためゴール地点の印をspriteのワールド座標を使用した点滅で表示する。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlashingSprite : MonoBehaviour
{
public Color myColor;
public float speed = 1.0f;
private SpriteRenderer image;
private float time;
// Start is called before the first frame update
void Start()
{
image = this.gameObject.GetComponent<SpriteRenderer>();
}
void Update()
{
//オブジェクトのAlpha値を更新
image.color = GetAlphaColor(image.color);
}
Color GetAlphaColor(Color color)
{
time += Time.deltaTime * 5.0f * speed;
color.a = Mathf.Sin(time) * 0.5f + 0.5f;
return color;
}
}
Canvas上のimageは画面に合わせて動くが、オープン座標のspriteはワールドスペースに固定され画面の移動に影響されない。