Unity3D实践系列03,使用Visual Studio编写脚本与调试
?
在Unity3D中,只有把腳本賦予Scene中的GameObject,腳本才會得以執行。
?
添加Camera類型的GameObject。
?
Unity3D默認使用"MonoDevelop"編輯器,這里,我想使用Visual Studio作為編輯器。
?
依次點擊"Edit","Preferences","External Tools",在"External Script Editor"項下選擇"Visual Studio"作為編輯器。
?
在"Project"窗口下的"Asserts"中,創建名稱為"_MyScripts"的文件夾。一旦創建名稱以下劃線開頭的文件夾,該文件夾將排在默認文件夾的上方。
?
開始運行時執行腳本
?
右鍵"_MyScripts"文件夾,點擊"Create"中的"C# Script",取名為"HelloWorld"。
?
雙擊"HelloWorld"類文件,將在"Visual Studio"中打開此文件。
?
在Visual Studio中編寫如下:
?
using UnityEngine; using System.Collections; public class HelloWorld : MonoBehaviour { // Use this for initialization void Start () { print("Hello World"); } // Update is called once per frame void Update () { } }?
保存。
?
如果在Unity3D軟件界面中,暫沒有"Console"窗口,依次點擊"Window"菜單下的"Console",并把"Console"窗口移動至"Game"窗口的右側。
?
此時,點擊Unity3D軟件上的播放按鈕,"Console"窗口還沒有顯示任何信息。因為,現在還沒有把腳本賦予給Scene中的GameObject。
?
點擊"Hierarchy"窗口中的"Camera",隨之"Inspector"窗口出現所有有關Camera的信息。
?
把"HelloWorld"這個類文件拖動到Camera對應的"Inspector"窗口的最下方,再次運行,可以看到腳本信息生效。
在Unity3D中,還有一種方法可以在控制臺輸出調試信息。那就是Debug.Log方法。修改HelloWorld類如下:
?
using UnityEngine; using System.Collections; public class HelloWorld : MonoBehaviour { // Use this for initialization void Start () { print("Hello World"); Debug.Log("Hello World from Log"); } // Update is called once per frame void Update () { } }?
保存。
?
先清空控制臺內的調試信息。再次運行,可以看到通過Debug.Log方法生成的調試信息生效。
?
當然Debug還有其它靜態方法,比如Debug.LogWarning, Debug.LogError,等等。
?
每過一幀執行腳本
?
在Update方法中,游戲每經過一幀都會執行這里的方法。修改HelloWorld類文件如下:
?
using UnityEngine; using System.Collections; public class HelloWorld : MonoBehaviour { // Use this for initialization void Start () { //print("Hello World"); //Debug.Log("Hello World from Log"); } // Update is called once per frame void Update () { print("Hello World"); } }?
保存。
?
運行,看到print("Hello World")被執行了很多次。
?
轉載于:https://www.cnblogs.com/darrenji/p/4587842.html
總結
以上是生活随笔為你收集整理的Unity3D实践系列03,使用Visual Studio编写脚本与调试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 封装axios的接口请求数据方法
- 下一篇: AngularJS中ui-router全