使用C#脚本创建Unity 3D游戏(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/unity-d-game-creation-using-csharp-script-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 14 分钟阅读 - 6838 个词 阅读量 0使用C#脚本创建Unity 3D游戏(译文)
原文地址:https://www.codeproject.com/Articles/1016088/Unity-D-Game-Creation-using-Csharp-Script
原文作者:syed shanu
译文由本站 robot-v1.0 翻译
前言
In this article we can see how to create a simple Unity 3D game using C# Script.
在本文中,我们可以看到如何使用C#脚本创建简单的Unity 3D游戏.
- 下载shanuFirstUnity-noexe.zip-1.6 MB(Download shanuFirstUnity-noexe.zip - 1.6 MB)
- 下载shanuFirstUnity.zip-1.6 MB(Download shanuFirstUnity.zip - 1.6 MB)
介绍(Introduction)
在本文中,我们来看看如何(In this article let’s see how to)
- Unity 3D简介.(Introduction to Unity 3D.)
- 从Visual Studio 2015安装Unity.(Install Unity from Visual Studio 2015.)
- 创建您的第一个Unity 3D应用程序.(Create your first Unity 3D Application.)
- Unity 3D Framework简介(Introduction to Unity 3D Framework)
- 如何将C#脚本添加到Unity 3D应用程序.(How to add C# Script to your Unity 3D Application.)
- 如何将C#脚本添加到所选对象以产生结果.(How to add the C# Script to selected object to produce the result.)
- 如何将Unity 3d应用程序构建和运行到Web.(How to Build and Run the Unity 3d Application to web.) Unity 3D简介(Introduction to Unity 3D) Unity用于开发游戏应用程序.使用Unity我们可以创建2d和3d游戏.它还支持多平台,这意味着我们可以构建Unity程序并将其部署到WEB,Windows和MAC OS,iOS,Android,BlackBerry等.这很有趣,是的,这是Unity的主要优势,因为我们只需编写一次即可部署到多个平台. Unity的另一个优点是我们可以编写C#脚本对游戏对象执行一些操作,例如,我们可以编写C#脚本来旋转3d文本,多维数据集等.我们可以看到有关如何安装Unity以及如何工作和运行的更多详细信息.我们的第一个Unity游戏应用程序.(Unity is used to develop a game application .Using Unity we can create 2d and 3d game. It also supports multi platform means we can build and deploy our Unity program to WEB, Windows and MAC OS, iOS, Android, BlackBerry and etc. It’s interesting right, yes this is the main advantage of Unity as we write once and can deploy to multiple platform. Another advantage of Unity is we can write our C# script to perform some actions on Game Objects for example we can write C# Script to rotate 3d text ,Cube and etc .We can see more details of how to install Unity and how to work and run our first Unity Game application.) 您可以参考Unity3d网站以获取更多详细信息.(You can refer the Unity3d web site for more details) https://unity3d.com/learn/tutorials(https://unity3d.com/learn/tutorials) 对于不知道如何使用C#编写游戏应用程序的游戏爱好者来说,这是从Unity开始的好地方. Unity是一个非常好的平台,可以用来启动和编写自己的第一个游戏应用程序以及使用c#编写脚本.(For game lovers who don’t know how to write your game application using C# then it’s very good place to start with Unity. Unity is a very good platform to start and write your own first game application and write scripts using c#.) 对于Unity C#脚本教程,请参考此链接(For Unity C# script tutorials kindly refer this link) http://docs.unity3d.com/ScriptReference/(http://docs.unity3d.com/ScriptReference/) 您可以找到C#脚本API和JavaScript API.(.You can find both C# Script API and JavaScript API .) 从Visual Studio 2015安装Unity(Install Unity from Visual Studio 2015) 在这里,我使用了Visual Studio2015.您可以从此处下载Visual Studio 2015社区版.(Here I have used Visual Studio 2015 .You can download the Visual Studio 2015 Community Edition from here.) https://www.visualstudio.com/zh-CN/downloads/download-visual-studio-vs.aspx(https://www.visualstudio.com/en-us/downloads/download-visual-studio-vs.aspx) 安装Visual Studio 2015之后,单击开始->程序->单击Visual Studio 2015.(After installing the Visual Studio 2015 Click Start -> Programs-> Click Visual Studio 2015.) 单击新建->项目->选择游戏.如果您尚未在计算机中安装Unity,则可以从此处安装.选择安装Unity,然后单击确定.(Click New -> Project - > Select Game. If you didn’t installed Unity in your computer .You can install from here. Select Install Unity and click ok.)
单击安装以继续安装Unity和Visual Studio Tools for Unity.(Click Install to continue installation of Unity and Visual Studio Tools for Unity.)
选择Unity和Visual Studio Tools for Unity,然后单击"下一步",这将在您的计算机中安装Unity.等待几分钟以完成安装过程.(Select both Unity and Visual Studio Tools for Unity and click Next which will install Unity in your computer. Wait for few minutes to complete the installation process.)
Unity安装完成后,单击开始菜单->单击Unity文件夹,然后单击Unity.(Once the Unity installation was completed Click Start Menu -> Click Unity Folder and click Unity.)
当您从开始菜单中单击Unity时,它将第一次要求您使用Unity帐户登录.如果您没有Unity帐户,则可以创建一个新的Unity帐户,然后使用该帐户登录信息打开Unity框架.(When you click the Unity from Start menu .For the first time it will ask you to sign in using your Unity account .If you don’t have the Unity Account you can create a new unity account and using the account login to open your Unity framework.)
如果您拥有Processional Edition License,则可以提供许可证代码,否则选择Unity Personal Edition,然后单击Next.(If you have Processional Edition License you can give the license code otherwise select Unity Personal Edition and click Next.)
创建您的第一个Unity 3D应用程序(Create your first Unity 3D Application) 单击创建新项目以创建您的第一个Unity Game应用程序.(Click Create new project to create your first Unity Game application.)
选择项目文件夹并提供您的Unity应用程序名称,然后选择2d或3d.在此示例中,我使用了Unity 3d,因此选择了3D,然后单击创建项目.(Select your Project Folder and give your Unity application name and select 2d or 3d In this example I have used the Unity 3d so I have selected 3D and click Create Project.)
现在,您可以准备进行第一个Unity Game应用程序开发.(Now you are ready to work with your first Unity Game application development.) 使用代码(Using the code) Unity 3D Framework简介(Introduction to Unity 3D Framework) 创建新项目后,我们可以看到如下所示的Unity框架.在unity框架中,我们可以看到以下六个主要部分.(After creating a new project we can see Unity Framework as below .In unity framework we can see 6 main parts as below.)
-
层次窗口–(Hierarchy Window –)此窗口中将列出所有添加的游戏对象,即"场景"的"相机"和"灯光".我们可以通过右键单击"层次结构"窗口并根据需要选择2D,3D对象等到场景中(这里是Cube,Sphere3D文本等游戏对象).也可以从GameObject菜单中添加对象.(All the added game objects, Camera and Light for our Scene will be listed in this window. We can add 2D , 3D Object and etc to our Scene by right clicking the Hierarchy Window and select as per our need.(Here the game objects like Cube,Sphere3D text and etc).You can also add the objects from GameObject menu.)
-
项目窗口–(Project Window –)我们可以在此窗口中看到我们的整个项目文件.我们可以从此窗口添加C#脚本文件.(We can see our entire project file in this window. We can add C# Script file from this window.)
-
场景窗口-(Scene Window -)我们可以在此处看到添加到场景中的所有对象,例如,如果我们添加多维数据集和3D文本,我们可以在此"场景"窗口中看到,我们还可以(We can see all our Objects which has added to the scene here for example if we add Cube and 3D Text we can see in this Scene window we can also)
-
游戏窗口–(Game Window –)在这里,我们可以看到场景的最终输出.我们可以单击运行按钮以查看游戏输出.(Here we can see the final output of our scene. We can click the run button to see the game output.)
-
控制台窗口-(Console Window -)在此窗口中,如果脚本文件中有任何错误消息,我们将看到错误消息.(In this window we can see error message if any we have in our script file.)
-
物业窗口–(Property Window –)我们可以更改所选对象的字体颜色,对齐方式等.(We can change the selected object Font color, Alignment and etc.) 当我们在"层次结构"窗口中维护Unity程序时,默认情况下可以看到(When we care our Unity program in Hierarchy window we can see by default)
Main Camera
和(and)Directional Light
将被添加.(will be added.) 主相机–(Main Camera –)这将用于在放置时以任何角度显示对象.(This will be used to display our objects in any angle as we placed.)
在这里我们可以看到我在场景中添加了3d Capsule对象和3d Text对象,现在我们可以看到如何为每个选定的对象使用属性窗口.(Here we can see I have added 3d Capsule object and 3d Text object to the scene, now we can see how to use property window for each selected object.) 对象工具栏:(Object Toolbar:)我们可以在Unity Framework的顶部看到下面的工具栏.(We can see the below tool bar at the top of Unity Framework.)
- 第一个工具将用于拖动和调整游戏场景.(First tool will be used to Drag and adjust the Scene of our Game.)
- 第二个工具将用于移动所选对象并将其放置在我们所需的位置.当我们单击"移动"工具并从场景中选择某个对象并拖动该对象时,我们可以看到对象的X和Y位置将从属性窗口中更改.(The Second tool will be used to move the selected object and place it in our desired location. When we click on Move tool and select some object from the scene and drag the object we can see the X and Y position of object will be changing from the property window.)
- 第三个工具将用于旋转所选对象.当我们单击旋转工具并从场景中选择某个对象时,通过单击对象可以根据需要旋转对象,我们还可以看到所选对象的旋转X和Y值将会改变.要查看实际输出,请单击"游戏"窗口.(The Third tool will be used to rotate the selected object. When we click on rotate tool and select some object from scene, by clicking on the object we can rotate the object as we needed, we can also see the selected object rotation X and Y value will be changing. To see the actual output click on the Game window.)
- 第四个工具是缩放选定的对象,我们可以通过单击"缩放"工具来调整选定对象的大小.(The Fourth tool is to Scale the selected Object, We can resize the selected object by clicking the Scale tool.) 属性窗口(Property Window) 现在我们开始添加颜色和定向光.(Now we start with adding color and Directional Light.) 定向光对象将用于向我们的对象添加光效果,例如,在这里我们可以看到我选择了定向光并将其拖动并将其放置在Capsule对象附近.(The Directional Light object will be used to add light effect to our objects for example here we can see I have selected the Directional light and drag it and place it near the Capsule Object.) 现在在属性窗口中,我们可以看到我已为"定向光对象"选择了绿色,并且从属性窗口中增加了"光对象"的标识.(Now in property window we can see I have selected the green color for the Directional Light Object and I have increase the Identity of the Light Object from the property window.) 我们可以在Capsule对象上看到最终的光效果,该对象最初是白色的,这是由于为Capsule对象放置了光,现在它具有绿色的光效果.(The final light effect we can see it on the Capsule object which was initially white in color due to light placed for the Capsule Object now it has green color light effect on it.)
3D文字属性(3D Text Property) 在这里,我们可以看到像Visual Studio属性窗口一样的内容,3D文本对象具有诸如文本的属性(在这里我们可以将任何文本添加到选定的3D文本对象,例如,我给我命名为" Shanu").我们可以添加颜色可以设置为Text属性等.我们可以从"属性"窗口的顶部或"层次结构"窗口中为对象命名,也可以重命名对象名称.(Here we can see same like our Visual Studio Property window, the 3D Text object has properties like Text (Here we can add any text to the selected 3D Text object, for example I have given my name as “Shanu”).we can add color to the Text property and etc.We can give name for object from the top of the Property window or from the Hierarchy window we can rename our object names.)
如何将C#脚本添加到Unity 3D应用程序(How to add C# Script to your Unity 3D Application) 因此,由于我们已将基本设计添加到我们的游戏应用程序中,现在让我们看看如何通过编写简单的C#脚本来添加一些基本操作,例如旋转3D胶囊和3D文本对象.(So for we have added our basic design to our Game application, now let’s see how to add some basic actions like rotating our 3D Capsule and 3D Text Object by writing a simple C# Script.) 在我们的.Net Web或Winform应用程序中,我们可以看到代码隐藏(.CS)文件来添加C#代码部分,但是在Unity中,我们可以通过从项目窗口中添加C#脚本来添加C#代码.(In our .Net Web or Winform application we can see Code-Behind (.CS) file to add C# code part but in Unity we can add C# code by adding C# Script from the Project Window.) 要添加C#脚本文件,请在如下所示的"项目"窗口上单击鼠标右键,然后选择"创建",然后单击" C#脚本".(To add the C# Script file right click on the Project window as below and select Create and click on C# Script.)
添加C#脚本文件后,我们可以看到现在将添加C#文件.我们可以根据需要重命名C#脚本文件,例如,在这里我将C#脚本文件命名为" shanuBehaviour".(After we add the C# Script file we can see now C# File will be added .We can rename the C# Script file as per our need for example here I have named the C# Script file as “shanuBehaviour”.)
要在脚本文件中编写C#代码,请双击添加的C#脚本文件.我们可以看到C#脚本文件将在Mono Editor中打开,如下所示.(To write our C# code on the Script file double clicks on the C# Script file which we have added.We can see the C# Script file will be open in Mono Editor as below.) MonoDevelop是一个IDE,它是用于编写C#,JavaScript等脚本的默认编辑器.有关MonoDevelop的更多详细信息,请单击此处的链接.(MonoDevelop is a IDE which is default editor for writing the scripts like C#, JavaScript and etc. For more details on MonoDevelop refer the link here) http://docs.unity3d.com/Manual/MonoDevelop.html(http://docs.unity3d.com/Manual/MonoDevelop.html)
C#脚本文件代码部分:(C# Script file Code Part:)默认情况下,当我们打开C#代码文件时,它将类似于以下代码.(by default when we open the C# Code file it will be look like below code.)
我们可以看到2个默认名称空间将添加到我们的C#脚本文件中.(We can see 2 default namespace will be added in our C# Script file.)
我们整个C#script类都将继承(Our entire C #script class will inherit) MonoBehaviour
默认情况下为class.(class by default.)单性行为(MonoBehaviour)是所有脚本派生的基类,有关MonoBehaviour的更多详细信息,请参见链接(is base class of all the script derives from, for more details of MonoBehaviour kindly refer to the link) http://docs.unity3d.com/ScriptReference/MonoBehaviour.html(http://docs.unity3d.com/ScriptReference/MonoBehaviour.html) .(.)
当我们创建C#脚本文件时,我们可以看到2个默认方法为(When we create our C# Script file we can see 2 default method as) Start()
和(and) Update()
.(.)
Start()
方法:(Method:)的(The) Start()
方法类似于我们的Webform页面加载或Winform表单加载方法. Start方法仅在程序启动时运行一次.(method is similar to our Webform Page Load or Winform Form Load Method. The Start method will be run only once at the start of the program.)
Update()
方法:(Method:)的(The) Update()
方法是重要的方法,因为每次执行操作时都会调用此方法.例如,如果需要旋转对象,则可以在update方法中编写代码,然后将C#脚本文件分配给该对象.每次运行Unity程序时,都会调用此update方法,并且可以从游戏输出中看到对象继续旋转.(method is the important method as this method will be called every time to perform our action .For example if we need to rotate a Object we write code in the update method and assign the C# Script file to the Object. When we run the Unity program every time this update method will be called and we can see continues rotation of the object from our game output.)
using UnityEngine;
using System.Collections;
public class shanuBehaviour : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
简单的C#脚本文件来旋转对象(Simple C# Script file to rotate our Objects)
在这里我们可以看到我写了一个简单的代码(Here we can see I have a written a simple code on) Update()
按间隔时间旋转对象的方法.(Method to rotate the objects in set of interval times.)
transofrm.RotateAround()
方法将用于旋转对象,我们将参数(如旋转方向和时间间隔)传递给(method will be used to rotated the objects ,we pass the parameters as rotate directions and Time interval to the) RoteAround()
方法.(method.)
using UnityEngine;
using System.Collections;
public class shanuBehaviour : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.RotateAround (Vector3.right,Vector3.up , 40 * Time.deltaTime);
}
}
编写C#代码后,将其保存在(After writing the C# code save it in) MonoDevelop
我们可以在属性窗口的右侧看到Unity框架中的更改.(and we can see the changes in our Unity framework at the right side of the property window.)
控制台窗口:(Console Window:)我们可以从控制台窗口中看到C#脚本的错误消息.例如,我将" this"关键字拼写为" thisd",我们可以在控制台窗口的底部看到错误消息,并且当我们单击"游戏"窗口时,可以看到如下错误消息.(We can see the error messages of our C# Script from Console Window. For example I have misspelled the “this” keyword with “thisd”, we can see the error message at the bottom of the Console window and also when we click on Game window we can see the error message as below.)
如何将C#脚本添加到所选对象以产生结果(How to add the C# Script to selected object to produce the result) 要将C#脚本文件添加到我们选择的对象中,请单击C#脚本文件并将其拖到``层次结构'‘窗口中的选定对象中.请参见下面的Gif图像,以了解如何将C#脚本文件添加到我们的对象中.(To add our C# Script file to our selected object .Click on the C# Script file and drag it to our selected objects in Hierarchy window .See the below Gif image to see how to add the C# Script file to our Object.)
现在,我们使用Unity运行程序创建了简单的游戏应用程序,并检查了游戏窗口的输出.(Now we have created our simple Game Application using Unity run the program and check the output from the Game Window.) 如何将Unity 3d应用程序构建和运行到Web(How to Build and Run the Unity 3d Application to web) 要在浏览器中生成并运行输出,请单击文件->生成并运行.(To Build and run the output in Browser Click on File-> Build and Run.)
选择您的场景,因为我们仅创建了一个场景,所以我将单击"添加当前"按钮,这将添加要构建和运行的当前场景.在平台上,我们可以看到Web Player,PC,iOS,Android等,我们可以选择任何一个以在或所选平台上查看我们的输出.在这里,我选择了Web Player.(Select your Scene as we have created only one Scene, I will click on the Add Current button which will add our Current Scene to be build and run. In platform we can see Web Player,PC,iOS,Android and etc we can select any one to see our output in or selected platform. Here I have selected the Web Player.)
如果单击"生成",则可以看到结果输出文件将添加到我们选择的文件夹中.在这里,当我们单击BuildOutput.html文件时,我们可以看到我们的Unity游戏将显示在浏览器中.(If we click on Build we can see the result output files will be added to our selected folder. Here when we click on BuildOutput.html file we can see our Unity game will be displayed in browser.)
我们可以安装Unity Web Player来查看输出.(We can install the Unity web player to see the output.)
历史(History) shanuFirstUnity.zip-2015年8月6日(shanuFirstUnity.zip - 2015-08-06)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
HTML C# Mobile VS2010 Unity Unity3D Visual-Studio MonoDevelop 新闻 翻译