首页 > 其他分享 >Unity 脚本基础

Unity 脚本基础

时间:2022-12-12 19:45:28浏览次数:73  
标签:脚本 code script GameObject 基础 GameObjects Unity class

Unity 官方手册是最好的教程书,只是偶尔限于冗长

Scripting concepts

Although Unity uses an implementation of the standard Mono runtime for scripting, it still has its own practices and techniques for accessing the engine from scripts. This section explains how objects created in the Unity editor are controlled from scripts, and details the relationship between Unity’s gameplay features and the Mono runtime.

Creating and Using Scripts(场景中的游戏对象,GameObjects)

The behavior of GameObjects is controlled by the Components that are attached to them. Although Unity’s built-in Components can be very versatile, you will soon find you need to go beyond what they can provide to implement your own gameplay features. Unity allows you to create your own Components using scripts. These allow you to trigger game events, modify Component properties over time and respond to user input in any way you like.

A script makes its connection with the internal workings of Unity by implementing a class which derives from the built-in class called MonoBehaviour. You can think of a class as a kind of blueprint for creating a new Component type that can be attached to GameObjects. Each time you attach a script component to a GameObject, it creates a new instance of the object defined by the blueprint. The name of the class is taken from the name you supplied when the file was created. The class name and file name must be the same to enable the script component to be attached to a GameObject.

The main things to note, however, are the two functions defined inside the class. The Update function is the place to put code that will handle the frame update for the GameObject. This might include movement, triggering actions and responding to user input, basically anything that needs to be handled over time during gameplay. To enable the Update function to do its work, it is often useful to be able to set up variables, read preferences and make connections with other GameObjects before any game action takes place. The Start function will be called by Unity before gameplay begins (ie, before the Update function is called for the first time) and is an ideal place to do any initialization.

As noted above, a script only defines a blueprint for a Component and so none of its code will be activated until an instance of the script is attached to a GameObject. You can attach a script by dragging the script asset to a GameObject in the hierarchy panel or to the inspector of the GameObject that is currently selected. There is also a Scripts submenu on the Component menu which will contain all the scripts available in the project, including those you have created yourself.

Instantiating Prefabs at run time(类/蓝图,预制件Prefabs)

Prefabs come in very handy when you want to instantiate complicated GameObjects or collections of GameObjects at run time. Compared with creating GameObjects from scratch using code, instantiating Prefabs using code has many advantages because you can:

  • Instantiate a Prefab using one line of code. Creating equivalent GameObjects from scratch requires many more lines of code.
  • Set up, test, and modify the Prefab quickly and easily using the Scene view, Hierarchy and Inspector
  • Change which Prefab is instantiated without changing the code. You can make a simple rocket into a super-charged rocket, without any code changes.

Source

https://docs.unity3d.com/

接下来读
https://docs.unity3d.com/2020.3/Documentation/Manual/ExecutionOrder.html

标签:脚本,code,script,GameObject,基础,GameObjects,Unity,class
From: https://www.cnblogs.com/qianxinn/p/16976935.html

相关文章

  • UML 基础: 类图
     ​​UML类图关系大全​​1、关联双向关联:C1-C2:指双方都知道对方的存在,都可以调用对方的公共属性和方法。在GOF的设计模式书上是这样描述的:虽然在分析阶段这种关系是......
  • Python学习基础笔记四十五——包
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......
  • 基础算法学习笔记
    #笔记-基础算法快速排序将序列按从小到大或从大到小顺序排序。时间复杂度\(O(nlogn)\),不稳定。步骤确定分界点\(x\):\(q[l]\)、\(q[(l+r)\div2]\),\(q[r]\)、\(......
  • 算法基础课
    给定一个字符串SS,以及一个模式串PP,所有字符串中只包含大小写英文字母以及阿拉伯数字。模式串PP在字符串SS中多次作为子串出现。求出模式串PP在字符串SS中所有......
  • KingbaseES V8R3集群部署案例之---通用机无ssh环境脚本部署集群
    案例说明:在一些通用机的生产环境,不允许主机之间通过ssh通讯,或者不允许root用户建立ssh互信或登录。默认KingbaseESV8R3集群通用机环境部署需要建立数据库用户及root用户,......
  • JAVA基础
    JAVA基础注释注释不会被执行,是给写代码的人看的单行注释://多行注释:/**/(快捷键:选中->ctrl+shift+/;/*+回车)文档注释(JavaDoc)(快捷键:/**+回车)......
  • HarmonyOS实战一【JS基础组件】switch、chart等的使用
    写在前面嗯,有这样一个活动,所以搞了一个小Demo,顺便学习一个js的鸿蒙的开发方式,感兴趣的小伙伴积极参与,活动地址:HarmonyOS线上Codelabs系列挑战赛博文主要是一些前端组件使......
  • [SIGMOD 2022]DMCS Density Modularity based Community Search
    [SIGMOD2022]DMCS:DensityModularitybasedCommunitySearch介绍目标是找到含有查询点的社区。文章的切入点是高质量的社区一定也是高模块化的,及社区内密度高,且社......
  • 关于JAVA 反射 基础知识/编码经验的一些总结
    写在前面温习一下毕业以来学习的东西。准备做成一个系列。所以对于每一部分技术点进行一个笔记整理。更多详见​​java面试的一些总结​​笔记主要是以网上开源的一本​​......
  • 《JAVA8基础应用与开发》读书笔记
    第一章,JAVA技术体系架构JAVASE,(标准版),核心技术。小型桌面应用。 JAVAME,(微型版),嵌入式,电子产品程序。 JAVAEE,(企业版),web应用。涉及的技术面很广。计算机语言机制 ......