lørdag 26. mars 2011

Getting started with Boo, using Unity 3D.

For this tutorial I am going to assume no prior knowledge of the Boo programming language, or for that matter programming. My main goal is to get you started learning a cool language(Boo), in a great environment (Unity).

What makes Boo so special? 
You have probably heard about Java, C#,javascript. At least in passing. 
These are programming languages, like Boo. But if you have ever written C you will immediately see quite a few differences. 
Boo does things quite differently from other languages. 
In fact it borrows a lot of its features from Python. A language often praised for being easy to read, maintain and write. And you can have all that power. In Unity (or anywhere). Right now.

1. Configuring Unity 3D 

The first thing you need to do is set Mono Develop as your default editor.
Select “Unity” >> “Preferences” and from the drop down list select “MonoDevelop”

You may need to browse to your Unity 3D installation directory and manually select the “MonoDevelop” executable.

In your Unity project tab. Left click, select Create and then “Boo Script”

Rename your new script to HelloWorld. Your project directory should look like this:

To make this all come together we have to tell MonoDevelop about this new script. So Select Assets in the menubar, and select "Sync MonoDevelop Project"

That's it, welcome to your new IDE, and we're ready to start writing Boo scripts!

2. Diving into Boo
Mono Develop now lives in your project directory. On the right side you can navigate your projects script assets. Locate the HelloWorld Boo file you just created and open it with MonoDevelop
Your window should look something like this: (I may be using a different theme than you)

Essentially you have your code view on the left side, and your project view on the right side. I am not going to cover all the features of MonoDevelop itself in this post. That's several posts for later.

Now you need to change the line, that says

class NewBehaviourScript (MonoBehaviour):
class HelloWorld (MonoBehaviour):
And change the lines that say
def Start():
def Start():
Debug.Log(“Hello World”!)

So what have we done here? We have told the compiler (the program that turns your Boo code into something the computer can deal with) that we want it to prepare our Unity tools for us. By saying

import UnityEngine

We have named our class HelloWorld. What's a class you might ask? Well that's not important right now. Just stay with me. But your class name needs to be the same as your script name. And since our script is called “HelloWorld.boo” the class needs to be called “HelloWorld”

On the subsequent lines, two functions are declared. “Start” and “Update”. Since our Boo code runs in the Unity environment most of what I have just said is only relevant to using Boo with Unity3D. So this code will probably not work (at least as intended) when ran outside Unity.

In Bo we declare functions with the keyword called “def” short for define. Followed by the function name, then a colon.
def MyFunction:
But a function is no good unless it's doing something. That's where our new Start function comes in.
As for
def Start ():
Debug.Log(“Hello World”)

You can probably guess what this does right?
Start..Debug..Log.."Hello World"
It would be a reasonable guess to assume that this would Log “Hello World” somewhere when run. Which is exactly what it does.

3. Running it!
In the Unity 3D environment your Start function is automatically run when your script starts running, when it's attached to a active gameObject. So create a new gameObject in Unity, either a Cube or for that matter any old model you have around. And drag your new script onto it.
When you now start Unity you should see “Hello World” in your log window. If you don't have a log window, open it from “Window”>>”Console”

Now click Run in Unity, and you should see “Hello World” in your console.

If you are entirely new to the concept of programming, this is probably quite confusing. But I promise that you are just a few minor epiphanies away from getting it. It just takes a little bit of time. But now you are at least ready to start your journey. I suggest checking out some more tutorials, you will find tons of resources in this thread:

1 kommentar:

  1. Greetings I tried all you have said but i get the following error when mono opens trough unity:

    System.IO.FileNotFoundException: Could not load file or assembly 'UnityScript, Version=, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
    File name: 'UnityScript, Version=, Culture=neutral, PublicKeyToken=null'
    at UnityScript.Ide.UnityScriptProjectIndexFactory.CreateUnityScriptProjectIndex () [0x00000] in :0
    at Boo.MonoDevelop.Util.ProjectIndexFactory.CreateIndexFor (MonoDevelop.Projects.DotNetProject project) [0x00000] in :0
    at Boo.MonoDevelop.Util.ProjectIndexFactory.ForProject (MonoDevelop.Projects.DotNetProject project) [0x00000] in :0
    at Boo.MonoDevelop.Util.Completion.BooCompletionTextEditorExtension.ProjectIndexFor (MonoDevelop.Projects.DotNetProject project) [0x00000] in :0
    at Boo.MonoDevelop.Util.Completion.BooCompletionTextEditorExtension.Initialize () [0x00000] in :0
    at MonoDevelop.Ide.Gui.Content.TextEditorExtension.Initialize (MonoDevelop.Ide.Gui.Document document) [0x00000] in :0
    at MonoDevelop.Ide.Gui.Document.OnDocumentAttached () [0x00000] in :0
    at MonoDevelop.Ide.Gui.Document.SetProject (MonoDevelop.Projects.Project project) [0x00000] in :0
    at MonoDevelop.Ide.RootWorkspace.ReattachDocumentProjects (IEnumerable`1 closedDocs) [0x00000] in :0
    at MonoDevelop.Ide.RootWorkspace+c__AnonStoreyA5.<>m__141 (System.Object , System.EventArgs ) [0x00000] in :0
    at Gtk.Application+InvokeCB.Invoke () [0x00000] in :0
    at GLib.Timeout+TimeoutProxy.Handler () [0x00000] in :0

    Any idea how i can solve that?