Unity 使用脚本获取组件,代码生成预制体

本文详细介绍了如何在Unity中创建和操作组件,包括Transform、Collider、AudioSource等。还讲解了如何关联预制体、获取和设置组件状态,以及通过名称、标签和数组方式查找GameObject。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码获取组件

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

// 必须要继承 MonoBehaviour 才是一个组件
// 类名必要与文件名一致

public class c1 : MonoBehaviour
{
    // 使用 public 初始变量时,使用命名用英语,Unity界面会将该变量翻译为中文并且可以随时调整数值
    // public float num = 1;

    // 关联预设体(将预制体拖动到脚本的 "预制体" 上, 通过代码实例化预制体)
    public GameObject prefab;  // prefab-预制体

    // 最早调用
    private void Awake()
    {
        Debug.Log("Awake 被调用了");
    }
    // Start在第一帧更新之前被调用
    void Start()
    {
        Debug.Log("Start 被调用了");
        
        // 获取脚本挂载的游戏物体
        GameObject go = this.gameObject;
        // 打印游戏物体名称
        Debug.Log(go.name);
        // 标签
        Debug.Log(go.tag);
        // 层
        Debug.Log(go.layer);
        
        // 获取激活状态
        // 真实激活状态
        Debug.Log(go.activeInHierarchy);
        // 自己组件的激活状态
        Debug.Log(go.activeSelf);
        // 设置激活状态
        // go.SetActive(true);
        

        // 获取组件
        // 1.获取Transform组件
        Transform trans = this.transform;

        // 2.获取其他组件 (碰撞组件-Collider)
        Collider collider = this.GetComponent<Collider>();
        // Debug.Log(collider);
        // 将(碰撞组件-Collider)失效 类似激活状态设置为关闭
        collider.enabled = false;

        // 3.获取父物体组件 (碰撞组件-BoxCollider)
        BoxCollider bc = this.GetComponentInParent<BoxCollider>();
        // Debug.Log(bc);

        // 4.获取子物体组件 ()
        CapsuleCollider cc = this.GetComponentInChildren<CapsuleCollider>();
        Debug.Log(cc.name);
        
        // 动态添加组件 (声音组件-AudioSource)
        // this.AddComponent<AudioSource>();
        
        // 通过物体名称找到物体 (物体名称-Cube)
        GameObject cube = GameObject.Find("Cube");
        // Debug.Log(cube.name);
        // 修改物体名称
        // cube.name = "123";


        // 通过标签找到物体 (标签名称-p1)添加新标签p1
        // GameObject cube2 = GameObject.FindWithTag("p1");
        // cube2.name = "new cube2";

        // 通过标签找到物体 (返回数组)
        // GameObject[] cube2 = GameObject.FindGameObjectsWithTag("p1");
        // Debug.Log(cube2);

        // 实例化预制体(需要先关联预制体)
        // 参数:(预制体、空间坐标、旋转)
        GameObject S1 = Instantiate(prefab, Vector3.zero, Quaternion.identity);

    }


    // 每帧调用一次Update
    void Update()
    {
         // Debug.Log("Update 被调用了");
    }

    // 在 Update 后调用
    private void LateUpdate()
    {
        // Debug.Log("LateUpdate 被调用了");
    }

    /*
    // 定时调用 在
    private void FixedUpdate()
    {
        // Debug.Log("FixedUpdate 被调用了");
    }

    // 脚本启动
    private void OnEnable()
    {
        Debug.Log("OnEnable 被调用了 脚本开启");
        // Debug.LogWarning("这是警告");
        // Debug.LogError("这是报错");
    }

    // 脚本失效
    private void OnDisable()
    {
        Debug.Log("OnDisable 被调用了 脚本关闭");
    }

    // 整个程序结束时调用,用于销毁资源
    private void OnDestroy()
    {
        Debug.Log("OnDestroy 被调用了 程序关闭了");
    }
    */
}

关联预制件,先选中物体,将预制件拖动到脚本的"预制件"上面。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

默执_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值