オブジェクトいろいろ
コンポーネントを取得
GameObject obj;
Text txt;
//Component取得
txt = obj.GetComponent<Text>();
//Object取得
obj = GameObject.Find("obj");
オブジェクトを生成する(Instantiate)
[serializeField] public GameObject prefabs;//複製するオブジェクト
[serializeField] private GameObject parent;//親オブジェクト
GameObject obj;//生成されるオブジェクトの器
//指定の場所にオブジェクトを生成
Instantiate(prefabs, new Vector3(0, 0, 0), Quaternion.identity);
//親を指定してオブジェクトを生成
Instantiate(prefabs, parent.transform, false);
//生成したオブジェクトに変更を加える
obj = Instantiate(prefabs, new Vector3(0, 0, 0), Quaternion.identity);
obj.name = "newObj"; //名前を変更
obj.transform.parent = this.transform; //親を設定(thisのところに任意のオブジェクト)
obj.transform.position = new Vector3(1, 1, 1); //座標を指定し場所を移動
オブジェクトの色を変更
using UnityEngine.UI;//追加
using TMPro;//追加
Image image;
Text text;
TextMeshProUGUI tmp;
void Start()
{
Color32 color = new Color32(255,255,255,255);//任意の色を設定
image.color = color;//イメージの色を変化
text.color = color;//テキストの色を変化
tmp.color = color;//テキストメッシュプロの色を変化
}
オブジェクト・テキストを透過させ消えたら削除する
using UnityEngine.UI;//追加
private Image image;
private int alpha = 255;
public float fadeSpeed = 100;
void Start ()
{
image = GetComponent<Image>();
}
void Update () {
alpha -= (int)(Time.deltaTime * fadeSpeed);
Color32 color = new Color32(0, 13, 255, alpha);// 任意の色
image.color = color;
if (alpha <= 5) Destroy(gameObject);
}
オブジェクト・テキストを点滅させる
using UnityEngine.UI;//追加
private Image image;
private int alpha = 255;
private int fadeSpeed = 320;
private bool isFade = true;
void Start()
{
image = GetComponent<Image>();
}
void Update()
{
if(isFade)alpha -= (int)(Time.deltaTime * fadeSpeed);
else alpha += Time.deltaTime * fadeSpeed;
Color32 color = new Color32(0, 13, 255, alpha);// 任意の色
image.color = color;
if (alpha <= 40) isFade = false;
else if(alpha >= 240) isFade = true;
}
オブジェクト・テキストを点滅させる(点灯時少し停止)
using UnityEngine.UI;//追加
private Image image;
private float alpha = 1;
private float fadeSpeed = 1.0f;
private bool isFade = true;
void Start()
{
image = GetComponent<Image>();
}
void Update()
{
if(isFade)alpha -= Time.deltaTime * fadeSpeed;
else alpha += Time.deltaTime * fadeSpeed;
Color color = new Color(0.05f, 1, 0, alpha);// 任意の色
image.color = color;
if (alpha <= 0.2f) isFade = false;
else if(alpha >= 1.1f) isFade = true;
}
オブジェクトを綺麗に揃える
綺麗に揃えたいオブジェクトの親オブジェクトに
Grid Layout Group(子要素の大きさと並べ方を矯正)を追加する。
オブジェクトがアクティブかの確認
GameObject obj;
void Start()
{
if(obj.activeSelf) Debug.Log("オブジェクトはアクティブです!");
else Debug.Log("オブジェクトは非アクティブです");
}
コメント