Unity3D数字孪生笔记——Unity常用API篇

本文详细介绍了Unity3D中的常用API,包括Component、Transform、GameObject、Object和Time的属性和方法。此外,还探讨了Prefab的使用及其优点,以及动画的创建、录制和编辑,包括Animation组件属性和各种动画控制函数的运用。
摘要由CSDN通过智能技术生成

一、常用API

在这里插入图片描述

1、Component

Component类提供了查找(在当前物体、后代、先辈)组件的功能。

  • 常用属性
    gameObject、transform、collider、renderer…
  • 常用方法
  • GetComponent、GetComponentInChild、GetComponentInParent…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// Component类提供了查找(在当前物体、后代、先辈)组件的功能。
/// </summary>
public class ComponentDemo : MonoBehaviour
{
   
    private void OnGUI()
    {
   
        if (GUILayout.Button("transform"))
        {
   
            this.transform.position = new Vector3(0, 0, 10);
        }
        if (GUILayout.Button("GetComponent"))
        {
   
            this.GetComponent<MeshRenderer>().material.color = Color.red;
        }
        if(GUILayout.Button("GetComponents"))
        {
   
            //获取当前物体所有组件
            var allComponent = this.GetComponents<Component>();
            foreach (var item in allComponent)
            {
   
                Debug.Log(item.GetType());
            }

        }

        if(GUILayout.Button("GetComponentsInChildren"))
        {
   
            //获取后代物体的指定类型组件(从自身开始,从上往下)
            var allComponent = this.GetComponentsInChildren<MeshRenderer>();
            foreach (var item in allComponent)
            {
   
                item.material.color = Color.red;
            }
        }

        if (GUILayout.Button("GetComponentsInParent"))
        {
   
            //获取先辈物体的指定类型组件(从自身开始,从下往上)
            var allComponent = this.GetComponentsInParent<MeshRenderer>();
            foreach (var item in allComponent)
            {
   
                item.material.color = Color.red;
            }
        }

    }
}

2、Transform

Transform类 提供了 查找(父、根、子(索引、名称))变换组件、改变位置、角度、大小功能。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// Transform类 提供了 查找(父、根、子(索引、名称))变换组件、改变位置、角度、大小功能
/// </summary>
public class TransformDemo : MonoBehaviour
{
   
    //***********查找变换组件****************//
    public Transform tf;
    private void OnGUI()
    {
   
        if(GUILayout.Button("foreach--transform"))
        {
   
            foreach (Transform child in this.transform)
            {
   //child为 每个子物体的变换组件
                print(child.name);
            }
        }
        if(GUILayout
  • 4
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

聪 ~smart

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值