4.Unity中c#代码学习(消息发布+物体获取+父子结点获取创建)

编程知识 行业动态 更新时间:2024-06-13 00:20:04

public GameObject OnNode; 首先将变量加入到节点中 反射机制

OnNode.SendMessage("DoRoate"); 向节点中发送消息,执行DoRoate函数 注意函数名调用

void DoRoate()                        函数执行
    {
        Debug.Log("接收到消息!");
        rotateSpeed = 180f;
    }

游戏物品GameObject ——节点

GameObject node = GameObject.find("物体名称")通过名称  可以找到物体 可以调用节点中的内容

GameObject node = GameObject.find("父节点名称/子节点名称")可以通过路径找到

 GameObject node = GameObject.Find("控制机翼旋转");              找到节点  
 Revolve audio = node.GetComponent<Revolve>();        调用节点中的Revolve变量                         audio.DoRoate();                                                              调用变量中的函数

public GameObject  rad1Node;                                                                将节点放入

Revolve audio = rad1Node.GetComponent<Revolve>();                       再调用Revolve

audio.DoRoate();                                                                                        再调用DoRoate函数     

Transform parent = this.transform.parent;                                             获取父结点的transform
GameObject nodeParent = this.transform.parent.gameObject;            获取父结点的gameObject

foreach (Transform Child in transform)                                                找到父节点下所有子节点
 {        Child.name = Child.gameObject.name           已经对Child.gameObject.name 进行了封装
        Debug.Log("子节点名称:" + Child.name);        
 }

Transform Child1 = this.transform.GetChild(0);             找到父节点下第一个子节点的transform
Debug.Log("子节点名称:" + Child1.name);

Transform Child2 = this.transform.Find("控制机翼旋转");                       通过Find找一级子节点
 if (Child2 != null)
  {
       Debug.Log("子节点名称:" + Child2.name);

   }
  else
   {
        Debug.Log("未找到子节点");
   }

Transform Child3 = this.transform.Find("控制机翼旋转/旋翼");                通过路径查找子节点

Transform name1 = this.transform.Find("/小车和鼠标位置");                       找到节点
this.transform.SetParent(name1);                                                                将this放到节点下

this.transform.SetParent(null);                                                                     将this放到一级节点

Child1.gameObject.activeSelf                                                      判断物品当前状态打开还是关闭

Child1.gameObject.SetActive(false/true);                                          将物品显示关闭/显示打开

int num = this.transform.childCount;                                                  节点中一级子节点的个数

更多推荐

4.Unity中c#代码学习(消息发布+物体获取+父子结点获取创建)

本文发布于:2023-03-29 10:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/eb13325ee392cccc0edcaee85e2f9edf.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:结点   物体   父子   消息   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!