code ソーシャル

Unityアプリcodetips(40)

スポンサーリンク

//tips

//Unityのパフォーマンスを知るためにprofilerを確認する

Unityではscene移行時に固まることがしばしばあるようだが、固まる要因は知っておいた方が良く、それにはprofilerを用いる。

unity上部のHelpの検索窓にprofilerと打ち込み検索。

Unityの再生ボタンを押すことでprofilerの各指標が動き始める。

下記参照。

https://answers.unity.com/questions/1693625/semaphore-wait-for-signal-accounts-for-80-of-total.html

//シーン遷移のかたまりをフェードアウトにより自然に見せる

シーン遷移のかたまりは発生してしまうとのことなので、硬直発生時に自然に見せるようにフェードアウトをシーン遷移の前に入れるようにする。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Fadeover : MonoBehaviour
{
public float time;
public float speed = 1.0f;
Image image;
void Start()
{
image = this.GetComponent<Image>();
}

void Update()
{

 

if (image.color.a >= 0)
{
image.color = GetAlphaColor(image.color);
}
}

Color GetAlphaColor(Color color)
{
time += Time.deltaTime * 30.0f * speed;
// color.a = Mathf.Sin(time) * 0.1f + 0.5f;
color.a = time / 100 + 0.01f;
return color;
}
}

このfadeのスクリプトをinvokeを発生させるenemyのスクリプトに付加し、invokeの発動時間を若干遅らせる。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Enemykyle : MonoBehaviour
{
[SerializeField] GameObject Fade;

GameObject Player;
Animator anim;

Vector3 MOVEX = new Vector3(1.0f, 0, 0);
Vector3 MOVEY = new Vector3(0, 1.0f, 0);
Vector3 MOVEZ = new Vector3(0, 0, 1.0f);

private GameObject mainCamera; //メインカメラ格納用
private GameObject subCamera; //サブカメラ格納用

 

Vector3 target; // 入力受付時、移動後の位置を算出して保存

private int count = 0;

void Start()
{
target = transform.position;

this.Player = GameObject.Find("Player");
mainCamera = GameObject.Find("MainCamera");
subCamera = GameObject.Find("SubCamera");

}

void Update()
{
if (Input.anyKeyDown)
{
count++;
anim = GetComponent<Animator>();
this.anim.SetTrigger("walk");

if (count == 1)
{
target = transform.position - MOVEX;

transform.rotation = Quaternion.Euler(0, 270, 0);

}

if (count == 2)
{
target = transform.position - MOVEX;

}

if (count == 3)
{
target = transform.position + MOVEX;

transform.rotation = Quaternion.Euler(0, 90, 0);

}
if (count == 4)
{
target = transform.position + MOVEX;
count = 0;

}

}

 

Move();

Vector3 p1 = transform.position;
Vector3 p2 = this.Player.transform.position;

 

if (p1 == p2)
{
Fadeover();

if (Player != null)
{
//Camera.main.transform.SetParent(null);
//subCamera.transform.SetParent(null);
//Player.transform.DetachChildren();

//this.Player.SetActive(false);

//Destroy(Player);
Invoke("Over", 2);

}
}

}
void Move()
{
this.transform.position = target;
}
void Over()
{
SceneManager.LoadScene("GameOver");
}
void Fadeover()
{
Fade.SetActive(true);
}

}

//ボタンのimageを変更したくなった場合

ある条件でボタンを変更したくなった場合、スクリプトから変更できる。

ただ、変更後のspriteは事前にスクリプトがアタッチされたオブジェクトインスペクターにドラッグしておく必要がある。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Buttonchange : MonoBehaviour
{
// 画像を動的に変えたいボタンの宣言
Image btnImage;

// inspectorで直接画像のスプライトを張り付ける
public Sprite Asprite;
public Sprite Bsprite;
public Sprite Csprite;

void Start()
{
// Imageを所得
btnImage = this.GetComponent<Image>();
}

void Update()
{
// フラグによってそれに合った画像に差し替える
if (Input.anyKeyDown)
{
btnImage.sprite = Asprite;
}

}
}

//ボタンを半透明にして使えなくする場合はinteractableを操作する

未攻略のstageボタンを半透明にして押せなくする場合、buttonのインスペクター内にあるinteractableを利用する。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ButtonStatus : MonoBehaviour
{
// Start is called before the first frame update
Button buttonA;

// Use this for initialization
void Start()
{
buttonA = GameObject.Find("Canvas/Buttonsam").GetComponent<Button>();

}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("space"))
{
//buttonA.enabled = false;
buttonA.interactable = false;
}

if (Input.GetKeyDown("a"))
{
//buttonA.enabled = true;
buttonA.interactable = true;
}

}
}

//PlayerPrefsを使用してstageselect時に前に選択したstageを記録させる

Stageselect画面で前回プレイしたステージに忍者をおくために、PlayerPrefsを利用する。

今回は座標の設定ではなく、変わりとなるオブジェクトを全ボタン位置に配置し、非表示にし、PlayerPrefsの保存記録を元に記録ステージ番号の忍者のみを表示させるようなスクリプトにした。

playstage=Noではなく、setintを忘れずに記述する。

スクリプトは空オブジェクトにアタッチした。

ボタンインスペクターのon clickには忘れずに、各PlayStage(int No)の設定を行う。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Ninpos : MonoBehaviour
{
[SerializeField] GameObject nin1;
[SerializeField] GameObject nin2;
[SerializeField] GameObject nin3;
[SerializeField] GameObject nin4;
[SerializeField] GameObject nin5;
[SerializeField] GameObject nin6;

public int playstage;

void Start()
{
int playstage = PlayerPrefs.GetInt("playstage", 1);
Debug.Log("1");

if (playstage == 1)
{
nin1.SetActive(true);
Debug.Log("3");
}
if (playstage == 2)
{
nin2.SetActive(true);
Debug.Log("4");
}
if (playstage == 3)
{
nin3.SetActive(true);
}
if (playstage == 4)
{
nin4.SetActive(true);
}
if (playstage == 5)
{
nin5.SetActive(true);
}
if (playstage == 6)
{
nin6.SetActive(true);
}

}

// Update is called once per frame
void Update()
{

}

public void PlayStage(int No)
{

PlayerPrefs.SetInt("playstage", No);
Debug.Log("2");
}
}

人気の記事

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.