Unity 生成物体的几种方式

系列文章目录

unity工具


文章目录


前言

大家好,我是心疼你的一切,不定时更新Unity开发技巧,觉得有用记得一键三连哦。

在软件开发的时候,可能会遇到程序的物体生成,虽然比较简单,但还是小记录一下,如有需要可以看两眼


👉一、直接new的方式创建生成

1-1.代码如下

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Clone_1 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        GameObject gameObject = new GameObject();
        GameObject gameObject1 = new GameObject("物体名字");
    }

}

1-2. 效果图

从效果图可以看出,场景一运行就会生成两个物体,一个有名字一个没有名字,停止之后物体会自动销毁,生成物体的时候还可以添加其他组件(如碰撞盒...)

👉二、使用Instantiate创建生成(GameObject)

2-1.代码如下

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Clone_2 : MonoBehaviour
{
    public GameObject prefab1;
    public GameObject prefabFather;  
    // Start is called before the first frame update
    void Start()
    {
        Instantiate(prefab1);  //直接生成物体
        Instantiate(prefab1, prefabFather.transform);  //  生成物体的时候给物体设置父物体
        //生成物体并设置父物体的时候是否保持原始的世界位置  false不保留 true保留
        Instantiate(prefab1, prefabFather.transform,false);
        //Quaternion.identity  对象与世界轴或父轴完美对齐(没有旋转)
        Instantiate(prefab1, prefabFather.transform.position,Quaternion.identity);
        //Quaternion.identity  对象与世界轴或父轴完美对齐(没有旋转)同时设置父物体
        Instantiate(prefab1, prefabFather.transform.position,Quaternion.identity, prefabFather.transform);

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

2-2.效果如下图


👉三.系统CreatePrimitive创建生成

创建系统内置的基础物体,比如Cube(正方体),Plane(平面),Sphere(球体),胶囊体,

球体,圆柱体。

有时候不知道为什么,测试用的时候总是忘记系统生成的api这次就写一次,嘻嘻

3-1代码如下

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Clone_3 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        //生成正方体
        GameObject.CreatePrimitive(PrimitiveType.Cube);
        //生成球体
        GameObject.CreatePrimitive(PrimitiveType.Sphere);
        //生成胶囊体
        GameObject.CreatePrimitive(PrimitiveType.Capsule);
        //生成圆柱体
        GameObject.CreatePrimitive(PrimitiveType.Cylinder);
        //生成占用内存大的平面
        GameObject.CreatePrimitive(PrimitiveType.Plane);
        //生成占用内存小的平面
        GameObject.CreatePrimitive(PrimitiveType.Quad);
    }

   
}

3-2.效果如下图

👉四、使用Instantiate创建生成(Transform)

不管是Transform还是GameObject都是可以用Instantiate的因为他们两个都是继承的是Object

4-1.代码如下

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Clone_4 : MonoBehaviour
{
    public Transform prefab;
    public Transform fatherTransform;
    // Start is called before the first frame update
    void Start()
    {
        //用法和Clone_2脚本里面的一样,这里就不多介绍了
        Transform tran = Instantiate(prefab);
        Transform tran1 = Instantiate(prefab,fatherTransform);
        Transform tran2 = Instantiate(prefab,fatherTransform,false);
        Transform tran3 = Instantiate(prefab,fatherTransform.position,Quaternion.identity ,fatherTransform);
    }
}

4-2.效果图

👉五、使用组件创建生成

1.新建一个脚本组件CubePrefab.cs挂载到生成的cube上面,制作成预制体

2.预制体效果如下

3.编辑刚刚创建的脚本,新增一个bool属性(isbool)

4.新增完之后的预制体效果图,就会多出一个勾选框

这时候就可以实例化了

5.代码如下

cubePrefab拖刚刚制作的预制体就ok了

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Clone_5 : MonoBehaviour
{
    public CubePrefab cubePrefab;
    // Start is called before the first frame update
    void Start()
    {
        for (int i = 0; i < 5; i++)
        {
            CubePrefab obj = Instantiate(cubePrefab);
            obj.isbool = true;
        }
    }

   
}

6.效果图如下

全选这几个cube你就会发现上面的bool属性全是勾选状态,测试结束

其他需要请自行扩展

👉六、壁纸分享


👉总结

以上就是讲了几种创建生成的方法,

不定时更新Unity开发技巧,觉得有用记得一键三连哦。感谢

相关推荐
暴走约伯19 小时前
【VFX工具链源码拆解 09】终极工业化闭环:基于 ID 规则驱动的全自动场景建档、骨骼挂载与男转女场景迁移
unity·c#·游戏引擎·材质
寂寞的船2 天前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua
寂寞的船3 天前
【Unity Bug挑战】第12期 Awaitable 二次 await 死锁之谜 —— 为什么同一个异步操作等两次就卡死?
unity·c#·游戏引擎·bug
探求虚无4 天前
用 AI 在 Unity 里做一款 RTS:最难的不是生成代码,而是证明它真的能玩
人工智能·unity·游戏引擎
17岁的勇气5 天前
Unity Shader unity文档学习笔记(二十二):shader变体
笔记·学习·unity
寂寞的船6 天前
【Unity知识速递】第10期 Unity GC 零分配实战:从 UnityEngine.Pool 到手动 GC 控制的全链路优化
jvm·unity·c#·游戏引擎·lucene
Martina_03216 天前
AI生成3D场景的霓虹灯一开就糊成光团?用6步检查Emissive、Bloom与曝光
人工智能·3d·ai·自然语言处理·游戏引擎·材质·贴图
寂寞的船8 天前
【Unity知识速递】第8期 IL2CPP 托管堆内存碎片与原生堆交互机制深度解析
unity·游戏引擎·交互
寂寞的船9 天前
【Unity知识速递】第5期 Unity 6.2 深度解析:AI Assistant 登场,World Space UI 与 Sentis 全面升级
人工智能·ui·unity
寂寞的船9 天前
【Unity知识速递】第4期 MCP for Unity:用 AI 自然语言控制编辑器
人工智能·unity·编辑器