目录
查
父子物体
查询父子物体
查询父
{
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;
}
}