zoukankan      html  css  js  c++  java
  • Unity3D 获得GameObject组件的方法

    Unity3D 获得GameObject组件的方法有几种,这里进行说明一下:

    组件:

    要获得这些组件,进行操作的话,绑定一个Movescipt 的C#组件,里面的获取方法为

        void Update () {
    
            Debug.LogError("sprite=" + gameObject.GetComponent<SpriteRenderer>().sprite);
            Debug.LogError("sortingOrder=" + gameObject.GetComponent<SpriteRenderer>().sortingOrder);
            Debug.LogError("color=" + gameObject.GetComponent<SpriteRenderer>().color);
    
            Debug.LogError("position=" + gameObject.GetComponent<Transform>().position);
            Debug.LogError("rotation=" + gameObject.GetComponent<Transform>().rotation);
            Debug.LogError("localScale=" + gameObject.GetComponent<Transform>().localScale);
    
    
            Debug.LogError("position=" + gameObject.transform.position);
            Debug.LogError("rotation=" + gameObject.transform.rotation);
            Debug.LogError("localScale=" + gameObject.transform.localScale);
    
        }

    这样通过获得组件GetComponent<>方法,能够获得一些需要的属性。

    需要说明一下,

    GetComponent<Transform>() 和 gameObject.transform 都能够获得组件的形态对象,只是写法不同罢了,推荐第一张写法,后面的方法估计以后也就是会废弃。

    获得同一对象下面的其他组件也是同样的方法。
    当要获得游戏对象下面的字对象的时候,用
     shootscript shoot= gameObject.GetComponentInChildren<shootscript>();

    就可以得到子对象,也可以用集合的方式得到子对象的集合,进行操作,反之,可以从子对象得到父对象

      movescript ms = gameObject.GetComponentInParent<movescript>();

    当然,能够拿到子对象或者父对象的组件了,也可以顺带得到该对象,对该对象进行处理了

            GameObject move =    gameObject.GetComponentInParent<movescript>().gameObject;
            Debug.LogError("prant -----move  = " + move.transform.position);

     当两个游戏对象是平级的时候,如果要获得另一个游戏对象的属性

    Find 是获得的对象名字

            Test2 test2 = GameObject.Find("1wwww").GetComponent<Test2>();
            test2.SayTest2();
    FindGameObjectWithTag 是获得的游戏对象的Tag,这个是可以自己去定义的,同时,当然可以进行获得到一个集合了
    Test2 test2 = GameObject.FindGameObjectWithTag("Player").GetComponent<Test2>();
            test2.SayTest2();

    也是同样的获得Tag

     Test2 test2 = GameObject.FindWithTag("555555").GetComponent<Test2>();
            test2.SayTest2();
  • 相关阅读:
    gifplayer-可控制GIF动画图片播放和暂停jQuery插件
    ERROR 2003 (HY000): Can't connect to MySQL server on
    linux下重启nginx
    查看MySQL的当前存储引擎
    MySQL查看表占用空间大小
    使用Sequel Pro通过ssh远程连接MySQL出错:The SSH Tunnel has unexpectedly closed
    textview 内容从头开始显示
    SQL优化
    运营平台框架整理(内部用)
    线程的取消和关闭
  • 原文地址:https://www.cnblogs.com/sunxun/p/5060791.html
Copyright © 2011-2022 走看看