目录
父子物体
查询父子物体
查
查询父
查询子
更改父节点
设置可视性
注意
实例:在音乐未指定情况下,用脚本使对象播放音乐
实例:点一下换一首歌(资源数组)
实例:点一下换材质
父子物体
查询父子物体

查询父
{ Transform father = this.transform.parent; Debug.Log(this.name+"的父组件是:"+father.name);
讯享网
查询子
讯享网 foreach(Transform child in transform) { Debug.Log(this.name+"的子节点有:" + child.name); }
更改父节点
{ Transform x = this.transform.Find("x"); this.transform.SetParent(x);
第二行的x可以是空(null)
球绕着a转,改变,球绕着“x”转。
设置可视性

挂在“groud”
讯享网 void Start() { } // Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { active(); } } void active() { Transform x = this.transform.Find("zhuzi"); if (x.gameObject.activeSelf) { x.gameObject.SetActive(false); } else { x.gameObject.SetActive(true); } }
注意
脚本要挂在父组件上,然后隐藏子组件。
若挂在父组件隐藏父组件,没有后续的操作。
在音乐未指定情况下,用脚本使对象播放音乐
音乐资源播放器 播放 音频剪辑
public class music : MonoBehaviour { public AudioClip setmusic; private int ok = 0; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { AudioSource audio = GetComponent<AudioSource>(); if (Input.GetMouseButtonDown(0)) { if (ok == 0) { audio.PlayOneShot(setmusic); ok = 1 - ok; } else { audio.Stop(); ok = 1 - ok; } } } }
实例:点一下换一首歌(资源数组)
讯享网 public class songs : MonoBehaviour { public AudioClip[] songsclip; AudioSource audio; int index; // Start is called before the first frame update void Start() { audio = GetComponent<AudioSource>(); } // Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) next(); } void next() { index = Random.Range(0,songsclip.Length); audio.clip = songsclip[index]; audio.Play(); } }
实例:点一下换材质
public class colorchange : MonoBehaviour { public MeshRenderer meshRenderer; public Material []material_; int index=0; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if(Input.GetMouseButtonDown(0)) { change(); } } void change() { index++; if (index >= material_.Length) index = 0; Material sel = material_[index]; meshRenderer = GetComponent<MeshRenderer>(); meshRenderer.material = sel; } }


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/66999.html