Unity学習記(1) - オブジェクトのいろいろな操作

  1. スクリプトコンポーネントにマウントされているオブジェクトオブジェクトを取得します。

gameObject
  1. オブジェクトの表示/非表示を設定する

gameObject.SetActive(true);
gameObject.SetActive(false);
  1. オブジェクトの取得

  1. 名前でスクリプトを取得

GameObject.Find("Object");
//1.GameObject.Find(),无法找到未激活的物体
//2.GameObject.Find()需要遍历场景的所有物体,从性能上看是十分低效的。
//注:它是全局查找,所以如果要查找子对象的话,用 transform.Find 更方便。

transform.Find("object name");
//查找下一层次的子物体
//它只会查询Hierarchy中下一层次的物体,不会递归地向下查找
//如果需要获取物体根节点下指定路径的子物体,可以用以下方式:

gameObject.transform.Find("SelectedEffect/遮罩/渐变")
//获取子物体中的组件
  1. タグでオブジェクトを検索

//以通过物体的标签直接获得物体,使用GameObject.FindGameObjectWithTag()方法即可,可高效地查找物体
//获取第一个标签为Object的物体
GameObject ob = GameObject.FindGameObjectWithTag("Object");
//获取所有标签为Object的物体,返回值是一个数组
GameObject[] obs = GameObject.FindGameObjectsWithTag("Object");
  1. スクリプトを使用してコンポーネントを取得する

//获得其他物体object的刚体
GameObject ob = GameObject.Find("object");
Rigidbody obrb = ob.GetComponent<Rigidbody>();
  1. オブジェクトの移動、拡大縮小、回転

//1、移动
//物体向x方向移动1.5个单位
transform.Translate(1.5f,0,0);
//或
transform.position += new Vector3(1.5f,0,0);

//稳定帧率
transform.Translate(1.5f*Time.deltaTime,0,0);
//或
transform.position += new Vector3(1.5f*Time.deltaTime,0,0);

//2、缩放
//cube.transform.localScale *= 1.2f;

//3、旋转
//cube.transform.Rotate (new Vector3(0, 10, 0));
//围绕球体旋转物体
//cube.transform.RotateAround (cylinder.transform.position, Vector3.up, 10);
  1. キーボードでオブジェクトを移動する

float horizontal = Input.GetAxis("Horizontal");
float vetical = Input.GetAxis("Vetical");
 
transform.Translate(horizontal*speed*Time.deltaTime,vetical*speed*Time.deltaTime,0);
//或
transform.position += new Vector3(horizontal*speed*Time.deltaTime,vetical*speed*Time.deltaTime,0);

おすすめ

転載: blog.csdn.net/ximi2231/article/details/129517551