facebook twitter hatena line email

「Unity/Csharp/オブジェクト操作」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(クリックしたオブジェクト名取得(2D版))
 
(同じ利用者による、間の18版が非表示)
行1: 行1:
 +
 +
==オブジェクト取得==
 +
GameObject obj = transform.Find("mc1").gameObject;
 +
Vector3 pos = obj.transform.position;
 +
 +
一つ上の階層を指定
 +
GameObject obj = transform.Find ("../mc1").gameObject;
 +
 +
ルートからの階層で指定
 +
GameObject obj = transform.Find ("/Canvas/mc1").gameObject;
 +
 +
==自分のオブジェクトのComponent一覧を取得==
 +
GameObject canvasObj = GameObject.Find ("Canvas");
 +
Component[] objList = canvasObj. GetComponents(typeof(Component));
 +
foreach (Component component in objList) {
 +
    // オブジェクト名がmc_~のときのみ表示
 +
    if (component.name.IndexOf ("mc_") > -1) {
 +
        Debug.Log (component.name);
 +
    }
 +
}
 +
 +
==子オブジェクトのComponent一覧を取得==
 +
GameObject canvasObj = GameObject.Find ("Canvas");
 +
Component[] objList = canvasObj.GetComponentsInChildren(typeof(Component));
 +
foreach (Component component in objList) {
 +
    // オブジェクト名がmc_~のときのみ表示
 +
    if (component.name.IndexOf ("mc_") > -1) {
 +
        Debug.Log (component.name);
 +
    }
 +
}
 +
 +
==子オブジェクトを1つ取得==
 +
GameObject canvasObj = GameObject.Find ("Canvas");
 +
GameObject childObj = canvasObj.transform.GetChild(0).gameObject;
 +
 
==x方向に移動==
 
==x方向に移動==
 
  void Update () {
 
  void Update () {
行4: 行39:
 
   transform.position = new Vector3(pos.x + 0.01f, pos.y, pos.z);
 
   transform.position = new Vector3(pos.x + 0.01f, pos.y, pos.z);
 
  }
 
  }
 
==オブジェクト取得==
 
GameObject obj = transform.Find("block_8").gameObject;
 
Vector3 pos = obj.transform.position;
 
  
 
==オブジェクト非表示==
 
==オブジェクト非表示==
  obj.SetActive (false);
+
  GameObject gameObj = GameObject.Find ("mc1");
 +
gameObj.SetActive (false);
 +
注意:一度ActiveをfalseするとFindで再度検索できなくなるので透明にするとかx,yを画面外にするのがよいかも。それが使えなければ、一度gameObjをActiveをFalseする前にプロパティに保存しておくのが良い。
 +
 
 +
他にenabledやinteractableがある
 +
 
 +
参考:http://portaltan.hatenablog.com/entry/2016/05/24/144108
  
 
==クリックしたオブジェクト名取得(2D版)==
 
==クリックしたオブジェクト名取得(2D版)==
行23: 行60:
 
  }
 
  }
 
  }
 
  }
 +
 +
==回転値取得==
 +
GameObject.Find("/Canvas/hoge").transform.localEulerAngles.z;
 +
 +
==全オブジェクト取得==
 +
<pre>
 +
foreach (GameObject obj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject)))
 +
{
 +
    // シーン上のオブジェクトのみ
 +
    if (obj.activeInHierarchy)
 +
    {
 +
        Debug.Log(obj.name);
 +
    }
 +
}
 +
</pre>
 +
参考:http://buravo46.hatenablog.com/entry/2014/12/31/064658
 +
 +
==全ボタンのオブジェクト取得==
 +
<pre>
 +
foreach (GameObject obj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject)))
 +
{
 +
    // シーン上のオブジェクトのみ
 +
    if (obj.activeInHierarchy)
 +
    {
 +
        Button button = obj.GetComponent<Button>();
 +
        if (button != null)
 +
        {
 +
            Debug.Log(obj.name);
 +
        }
 +
    }
 +
}
 +
</pre>
 +
==全ボタンを透明度50%へ==
 +
<pre>
 +
foreach (GameObject obj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject)))
 +
{
 +
    // シーン上のオブジェクトのみ
 +
    if (obj.activeInHierarchy)
 +
    {
 +
        Button button = obj.GetComponent<Button>();
 +
        if (button != null)
 +
        {
 +
            Debug.Log(obj.name);
 +
            obj.GetComponent<Image>().color = new Color(1f, 1f, 1f, 0.5f);
 +
        }
 +
    }
 +
}
 +
</pre>
 +
 +
==全テキストを白へ==
 +
<pre>
 +
foreach (GameObject obj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject)))
 +
{
 +
    // シーン上のオブジェクトのみ
 +
    if (obj.activeInHierarchy)
 +
    {
 +
        Text text = obj.GetComponent<Text>();
 +
        if (text != null)
 +
        {
 +
            Debug.Log(obj.name);
 +
            // 親がDropdown,InputFieldのときは無視
 +
            if (textObj.transform.parent.gameObject.name.IndexOf("Dropdown") > -1
 +
                || textObj.transform.parent.gameObject.name.IndexOf("InputField") > -1)
 +
            {
 +
                continue;
 +
            }
 +
            text.color = new Color(1f, 1f, 1f, 1f);
 +
        }
 +
    }
 +
}
 +
</pre>

2022年9月5日 (月) 13:14時点における最新版

オブジェクト取得

GameObject obj = transform.Find("mc1").gameObject;
Vector3 pos = obj.transform.position;

一つ上の階層を指定

GameObject obj = transform.Find ("../mc1").gameObject;

ルートからの階層で指定

GameObject obj = transform.Find ("/Canvas/mc1").gameObject;

自分のオブジェクトのComponent一覧を取得

GameObject canvasObj = GameObject.Find ("Canvas");
Component[] objList = canvasObj. GetComponents(typeof(Component));
foreach (Component component in objList) {
    // オブジェクト名がmc_~のときのみ表示
    if (component.name.IndexOf ("mc_") > -1) {
       Debug.Log (component.name);
    }
}

子オブジェクトのComponent一覧を取得

GameObject canvasObj = GameObject.Find ("Canvas");
Component[] objList = canvasObj.GetComponentsInChildren(typeof(Component));
foreach (Component component in objList) {
    // オブジェクト名がmc_~のときのみ表示
    if (component.name.IndexOf ("mc_") > -1) {
       Debug.Log (component.name);
    }
}

子オブジェクトを1つ取得

GameObject canvasObj = GameObject.Find ("Canvas");
GameObject childObj = canvasObj.transform.GetChild(0).gameObject;

x方向に移動

void Update () {
  Vector3 pos = transform.position;
  transform.position = new Vector3(pos.x + 0.01f, pos.y, pos.z);
}

オブジェクト非表示

GameObject gameObj = GameObject.Find ("mc1");
gameObj.SetActive (false);

注意:一度ActiveをfalseするとFindで再度検索できなくなるので透明にするとかx,yを画面外にするのがよいかも。それが使えなければ、一度gameObjをActiveをFalseする前にプロパティに保存しておくのが良い。

他にenabledやinteractableがある

参考:http://portaltan.hatenablog.com/entry/2016/05/24/144108

クリックしたオブジェクト名取得(2D版)

カメラオブジェクトに設置

void Update () {
	if (Input.GetMouseButtonDown(0)) {
		Vector2 tap = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		Collider2D collition = Physics2D.OverlapPoint(tap);
		if (collition) {
			Debug.Log (collition.transform.gameObject.name);
		}
	}
}

回転値取得

GameObject.Find("/Canvas/hoge").transform.localEulerAngles.z;

全オブジェクト取得

foreach (GameObject obj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject)))
{
    // シーン上のオブジェクトのみ
    if (obj.activeInHierarchy)
    {
         Debug.Log(obj.name);
    }
}

参考:http://buravo46.hatenablog.com/entry/2014/12/31/064658

全ボタンのオブジェクト取得

foreach (GameObject obj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject)))
{
    // シーン上のオブジェクトのみ
    if (obj.activeInHierarchy)
    {
        Button button = obj.GetComponent<Button>();
        if (button != null)
        {
            Debug.Log(obj.name);
        }
    }
}

全ボタンを透明度50%へ

foreach (GameObject obj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject)))
{
    // シーン上のオブジェクトのみ
    if (obj.activeInHierarchy)
    {
        Button button = obj.GetComponent<Button>();
        if (button != null)
        {
            Debug.Log(obj.name);
            obj.GetComponent<Image>().color = new Color(1f, 1f, 1f, 0.5f);
        }
    }
}

全テキストを白へ

foreach (GameObject obj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject)))
{
    // シーン上のオブジェクトのみ
    if (obj.activeInHierarchy)
    {
        Text text = obj.GetComponent<Text>();
        if (text != null)
        {
            Debug.Log(obj.name);
            // 親がDropdown,InputFieldのときは無視
            if (textObj.transform.parent.gameObject.name.IndexOf("Dropdown") > -1
                || textObj.transform.parent.gameObject.name.IndexOf("InputField") > -1)
            {
                continue;
            }
            text.color = new Color(1f, 1f, 1f, 1f);
        }
    }
}