Unity之API

来源:互联网 发布:如何关闭windows shell 编辑:程序博客网 时间:2024/05/20 22:27
1:场景切换很简单,调用一个函数就可以。
if(hasJumped&&state.IsName("Base Layer.idle"))//处于idle状态,就让场景复原        {            hasJumped = false;            Application.LoadLevel(0);        }

      也可以这样: 

   代码:

    using UnityEngine.SceneManagement;

    SceneManager.LoadScene("Game");

  只是要注意要在buildsetings中将要跳转的场景添加进来、

 

2:让 物体显示与隐藏

using UnityEngine;using System.Collections;public class test1 : MonoBehaviour {    private GameObject cube;    private GameObject cube1;void Start () {        cube = GameObject.Find("Cubettt");        cube1 = GameObject.Find("sd");}// Update is called once per framevoid Update () {        cube1.SetActive(true);//激活cube1,使其显示在场景中        bool active=cube1.activeSelf;//activeSelf返回一个bool值,表示cube1的激活状态        string act = active.ToString();//格式转换        Debug.Log(act);//往控制台打印信息}}

3:往控制台输出信息

   Debug.Log("string");


4:设置父物体

gameObject.GetComponent<Transform>().setparent(Transform);
gameObject就成了Transform的子物体。
 

5:获取子物体组件

            Transform.GetComponentInChildren<Transform>(),获得所有子物体的某种组件,当然组件可以有多种


            Transform[] abc = hit.collider.gameObject.transform.parent.GetComponentsInChildren<Transform>(); 调用GetComponentsInChildren方法返回的是所有子物体的某种组件。所以得用数组存储,这样就可以操作每一个物体


                   注意GetComponentsInChildren和GetComponentInChildren有区别。


6:销毁物体

      代码:

using UnityEngine;  using System.Collections;    public class Destroy : MonoBehaviour {        private Transform trans;      // Use this for initialization      void Start () {          trans = GetComponent<Transform>();              }      void Update()      {          if (trans.position.y < -1)              GameObject.Destroy(gameObject);      }  } 

GameObject类的一个成员方法,有两个重载形式,1:gameobject:2:gameobject,float t,t是时间,过t秒销毁 ,本例中的gameobject是脚本挂载的那个游戏对象。





0 0
原创粉丝点击