unity日记5(父子物体,交互换材质、音乐)

目录

父子物体

查询父子物体

 查询父

 查询子

更改父节点

设置可视性

注意

 实例:在音乐未指定情况下,用脚本使对象播放音乐

实例:点一下换一首歌(资源数组)

实例:点一下换材质


父子物体

查询父子物体

 查询父

{
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; 
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值