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("").GetComponent<Test2>();
test2.SayTest2();

Unity3D 获得GameObject组件的方法的更多相关文章

  1. React-Native 组件开发方法

    前言 React Native的开发思路是通过组合各种组件来组织整个App,在大部分情况下通过组合View.Image等几个基础的组件,可以非常方便的实现各种复杂的跨平台组件,不过在需要原生功能支持. ...

  2. WPF中实例化Com组件,调用组件的方法时报System.Windows.Forms.AxHost+InvalidActiveXStateException的异常

    WPF中实例化Com组件,调用组件的方法时报System.Windows.Forms.AxHost+InvalidActiveXStateException的异常 在wpf中封装Com组件时,调用组件 ...

  3. 查看 activex 组件的方法

    查看 activex 组件的方法 可以使用的工具COMRaider 直接安装 并选择对应的类型即可查看相关的信息,比OLE/COM Object Viewer 简洁方便. 具体的操作如下: 随意选择一 ...

  4. 解决 window server2008 r2 没有注册Ofiice组件的方法

    解决 window server2008  r2 没有注册Ofiice组件的方法   .NET下在用Microsoft.Office.Interop.Excel及word 操作Excel和Word时, ...

  5. VC中调用COM组件的方法(转载)

    原文参考:http://hi.baidu.com/mingyueye/item/53ebecd44da76917d80e4449 总结一下在VC中调用COM组件的方法 准备及条件: COM服务器为进程 ...

  6. Unity3D与iOS消息交互方法(1)--iOS接收Unity3D发出的消息

    跨平台这种事情不管多NB, 总要有些与原生系统交互的方法, 比如  Unity3D与iOS消息交互方法. 一: 建立一个空的Unity工程. File -->  New Project 二: 编 ...

  7. vc中调用Com组件的方法详解

    vc中调用Com组件的方法详解 转载自:网络,来源未知,如有知晓者请告知我.需求:1.创建myCom.dll,该COM只有一个组件,两个接口:   IGetRes--方法Hello(),   IGet ...

  8. 在javascript中使用com组件的方法

    转载自:  http://dhailin.blog.163.com/blog/static/230738322011128102043880/ 首先创建一个COM组件,插入一个双接口Itest,在此接 ...

  9. 在JS中使用COM组件的方法

    首先创建一个COM组件,插入一个双接口Itest,在此接口上实现以下三个方法: STDMETHODIMP Ctest::test(void) //无输入输出参数 { // TODO: 在此添加实现代码 ...

随机推荐

  1. delphi非IE内核浏览器控件TEmbeddedChrome下载|TEmbeddedChrome代码

    下载地址: 点击下载 代码示例: 在TForm的oncreate方法中写入一些代码 procedure TForm1.FormCreate(Sender: TObject); begin Chromi ...

  2. javascript时间、随机数

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 开源(免费)三维 GIS(地形,游戏) 续1

    转自:http://www.cnblogs.com/xiexiaokui/archive/2009/04/02/1428525.html 转自 三维数字地球发布平台探索--几款开源软件介绍 http: ...

  4. [LeetCode] 234. Palindrome Linked List 解题思路

    Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...

  5. 关于vim打开中文文件出现乱码问题

    中文字符编码问题. 在你的vi配置文件(~/.vimrc)里面添加一行: set fileencodings=ucs-bom,utf-8,cp936,gb18030,latin1 意思是让vim从 这 ...

  6. canvas 时钟

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Windows版 mysql 5.7.16安装

    MySQL 5.7版本安装教程-踩坑总结 下载 MySQL下载地址选择下载64位(看自己电脑是32位还是64位) 点击下载之后,它会让你登录,没有Oracle账户,跟着它的步骤注册一个就好了. 安装 ...

  8. Expression Blend 4 激活码

    Expression Blend 4 激活码: 6WDDQ-K7D4F-GQGF4-2VYBJ-8K6MB

  9. Windows操作系统的历史

    30 years ago Windows was first released, see how much it has changed回顾了Windows操作系统的历史. 1985, Windows ...

  10. 【web开发学习笔记】ibatis学习总结

    ibatis学习总结 ibatis数据库配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCT ...