Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-settings.php on line 472

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-settings.php on line 487

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-settings.php on line 494

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-settings.php on line 530

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-includes/cache.php on line 103

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-includes/query.php on line 21

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-includes/theme.php on line 623
michaeljbowen.com » C#

Archive

Archive for the ‘C#’ Category

Instantiating a prefab from the library in C# with Unity 3D

August 9th, 2009

Unity 3D is really geared toward visually building your game through use of the Unity 3D editor. However, there are times when you want to instantiate a prefab that you don’t already have in the Scene.

Instantiating a prefab from your library is easy, once you know how. Figuring out how is a pain. The biggest quirk is that your prefab must be located in a directory called “Resources” if you want to instantiate it using C#.

Here are the steps.

1. Make sure you create a directory called “Resources” and place it in your Assets/ directory.
2. Create a prefab. Let’s all it “FooPrefab”.
3. In C#, instantiate the prefab as follows:

1
GameObject go = Instantiate(Resources.Load("FooPrefab")) as GameObject;

Why does the prefab have to be placed in a directory called “Resources”.
Who knows?

C#, Unity 3D ,

Using C# with Unity 3D to achieve code completion (intellisense)

August 8th, 2009

I’ve been playing around with Unity 3D lately, and its pretty awesome.  Its not very difficult to make the leap from Flash to Unity.  However, one detractor to Unity is its poor coding environment.

As a Flash programmer, I’m very spoiled by several of the development environments available to me.  FlashDevelop, Flex, etc. all provide intelligent editors that give me code completion.  Unity’s built in editor does not have this feature.  Once you get used to code completion, you can never go back to a plain text editor!

The good news is that you can use Visual C# 2008 Express for all of your Unity coding needs, and its totally free!  Thanks, Bill Gates.

Below are the steps to set up your environment.  Have fun.  Don’t say I never gave you anything.

One time setup:

1. Download Visual C# 2008 Express for free:
http://www.microsoft.com/express/vcsharp/

2. Install it.  ;)

3. Download the following file, which is needed for tool tips as you edit in C#.
UnityDocs.zip
Place the 2 XML files here:
C:\Program Files\Unity\Editor\Data\lib
(or wherever you installed Unity)

Repeat for each project:

1. When you create a new Unity 3D project, you need to create a new C# project somewhere in the Assets/ directory.  It can be right in Assets/, or you can make a sub directory.  Making a sub directory might be cleaner as C# will create solution files, etc.  Use “Empty Project” as your project type in C#.

2. Add a reference to the Unity dll files in your C# project.
In C#’s Solution Explorer panel (on the right), right-click the project and choose “Add Reference”.
Browse to and add UnityEngine.dll.
(you can add the editor dll as well if you are going to write editor code)

3. Create new class files from within C#.  If you do it from Unity, they don’t show up automatically in your C# project (there is probably a way to add them, but I don’t know it).

That’s it!
Code completion, tool tips, hints… it’s all there.

C#, Unity 3D ,