Purplelilgirl Makes Games
» Unity3D 游戏引擎之Unity3D回馈IOS高级界面消息
Tags: unity3d

» TexturePacker and Unity3D GUI with UIToolkit
» How to iTween GUI elements in Unity3d?

(it’s a link to Unity Answers)

Mini Tutorial: How to batch replace textures with different file formats in Unity?

In relation to my previous tutorial: Mini Tutorial: How to batch convert image files to PVR (for iPhone app development)?

So you have a Unity project that was previously not planned for iOS (for PC, Mac or web or whatever), and you might not be as conscious of the texture file formats (iOS is probably the only one with so many constraints).

According to Unity’s User Manual:

Use iOS native PVRT compression formats. They will not only decrease the size of your textures (resulting in faster load times and smaller memory footprint), but also can dramatically increase your rendering performance! Compressed texture requires only a fraction of memory bandwidth compared to full blown 32bit RGBA textures.

So after converting all my textures (or creating a copy of my textures in .pvr format using Automator), I’m supposed to change all the material textures in my Unity Project and that’s a lot. So I wrote this tiny Editor script to do that for me. It will even delete the old texture (.tga or .png or whatever) from the Assets folder.

So here it is:

using UnityEngine;
using System.Collections;
using UnityEditor;                      
 
public class ChangeTexturesToPVR : ScriptableWizard
{
     [MenuItem (“Project Tools/Change Textures to PVR”)]
    static void MakeFolder ()
    {   ChangeTextures();
    }
 
    static void ChangeTextures()
    {
        string filePath = AssetDatabase.GetAssetPath(Selection.activeGameObject);
        int index = filePath.LastIndexOf(“/”)+1;
        filePath = filePath.Remove(index);
        
        foreach (Transform child in Selection.activeGameObject.transform)
        {    Material[] mats = child.renderer.sharedMaterials;
           
            foreach(Material mat in mats)
            {    string texFilePath = filePath;
                texFilePath = texFilePath.Insert(texFilePath.Length, “Texture/”);
                if(mat.mainTexture != null)
                    texFilePath = texFilePath.Insert(texFilePath.Length, mat.mainTexture.name);
               
                string oldTexFilePath = texFilePath;
                oldTexFilePath = oldTexFilePath.Insert(oldTexFilePath.Length, “.tga”);
               
                texFilePath = texFilePath.Insert(texFilePath.Length, “.pvr”);
               
                mat.mainTexture = (Texture)AssetDatabase.LoadAssetAtPath(texFilePath, typeof(Texture));
                AssetDatabase.DeleteAsset(oldTexFilePath);
            }
        }
       
       
    }
}

If your old textures are in “.png”, just change “.tga” in the code above to well, “.png”.

Select the model (.fbx or .obj file) whose materials and textures that you would want to change.

And then go to the menu and click Project Tools > Change Textures to PVR (yes, I created a Menu item for it).

And then voila!

UPDATE:

I edited the code a bit:

using UnityEngine;
using System.Collections;
using UnityEditor;                     

public class ChangeMaterialTexturesToPVR : ScriptableWizard
{
     [MenuItem (“Project Tools/Change Material Textures to PVR”)]
    static void MakeFolder ()
    {   ChangeTextures();
    }

    static void ChangeTextures()
    {
        for(int i=0; i < Selection.gameObjects.Length; i++)
        {    if(Selection.gameObjects[i].transform.childCount == 0)
            {    changeTexture(Selection.gameObjects[i].transform);
            }
            else
            {    foreach (Transform child in Selection.gameObjects[i].transform)
                {    changeTexture(child);   
                }
            }
        }
    }

    static void changeTexture(Transform obj)
    {   
        if(obj.renderer)
        {    Material[] mats = obj.renderer.sharedMaterials;

            foreach(Material mat in mats)
            {   
                string texFilePath = “”;

                if(mat.mainTexture != null)
                {    texFilePath = AssetDatabase.GetAssetPath(mat.mainTexture);

                    if(!texFilePath.Contains(“.pvr”))
                    {    texFilePath = texFilePath.Remove(texFilePath.IndexOf(“.”));

                        string oldTexFilePath = texFilePath;
                        oldTexFilePath = oldTexFilePath.Insert(oldTexFilePath.Length, “.tga”);

                        texFilePath = texFilePath.Insert(texFilePath.Length, “.pvr”);

                        Texture tex = (Texture)AssetDatabase.LoadAssetAtPath(texFilePath, typeof(Texture));

                        if(tex != null)
                        {    mat.mainTexture = tex;
                            AssetDatabase.DeleteAsset(oldTexFilePath);

                            Debug.Log(texFilePath);
                        }    else
                        {    tex = (Texture)AssetDatabase.LoadAssetAtPath(oldTexFilePath, typeof(Texture));
                            mat.mainTexture = tex;

                            //Debug.Log(oldTexFilePath);
                        }
                    }
                }
            }

            obj.renderer.sharedMaterials = mats;
        }
    }
}

Now, all you have to make sure is that the .pvr and the .tga (you can change this to .png or .tif or .whatever), is in the same folder and that’s it.

That’s about it.

Mini Tutorial: How to batch convert image files to PVR (for iPhone app development)?

According to Unity3D’S manual, when developing for iOS

Use iOS native PVRT compression formats. They will not only decrease the size of your textures (resulting in faster load times and smaller memory footprint), but also can dramatically increase your rendering performance! Compressed texture requires only a fraction of memory bandwidth compared to full blown 32bit RGBA textures.

You can use Unity’s built in PVRTC compression, but according to @ToxicBlob

Not all PVRTC are created equal

http://www.toxicblob.com/files/Not_all_PVRTC_are_created_equal.php

Well, the Unity manual also suggested using PVRTexTool to create your PVRs. They have a GUI tool and a command line tool. (Please read ToxicBlob’s blog on how to install the tool).

Since, my artists gives me a lot of texture files, there’s no way that I’d do the conversion one by one using the GUI tool, so Mac’s Automator to the rescue! I love Mac’s Automator, it’s now my new best friend.

So I made an Automator Workflow:

The shell script:

for f
do
/Applications/PVRTexTool/PVRTexToolCL/MacOS_x86/PVRTexTool -fPVRTC4 -pvrtchighquality -yflip 1 -square -i”$f” -o”$f”
done

EDIT: I edited the previous script, adding “-o$”f”” so that the output file will be in the same folder as your input file.

Just add all your textures in the Get Specified Finder Items part and click Run!

And that’s it!

Build fun games even if you’ve never coded before

Reblog of my AltDevBlogADay post.

“Start developing games straight away. Build your first game in the first chapter.” “Fun and fast paced. Ideal for readers with no game programming experience.” “A cool guide to get into the game industry quickly.”

Doesn’t that sound like an advertisement for weight loss, except for well, making games. Those are the lines on the “back cover” (if eBooks have back covers) of game development beginner’s guides. But can you really make a game after reading the book? That’s what I want to find out.

So I grab a copy of Unity 3D Game Development by Example Beginner’s Guide (written by Ryan Henson Creighton aka Cassie the 5 yr old game developer’s dad) and I will write this blog post as I read through the book and make that game. There also Beginner’s Guide books for Cocos2d (Cocos2d for iPhone 0.99 Beginner’s Guide written by Pablo Ruiz), XNA (XNA 4.0 Game Development by Example: Beginner’s Guide written by Kurt Jaegers) and Flash (Flash Game Development by Example written by Emanuele Feronato).

Some disclaimer, I am not a complete beginner at Unity. I spent maybe a year (two years ago) making technical demos using Unity, for a game art outsourcing company in the Philippines that wants to break out from outsourcing to creating their own games. But I left that company, and country and well, to come to a tiny studio in Taiwan to make iPhone games. So here I am, reacquainting myself with Unity. Hi Unity, I’m Hsiao Wei Chen, nice to meet you (okay, that was lame).

So here we go, first chapter. Chapter 1: That’s One Fancy Hammer. (You can read the first chapter on the PacktPub website) Basically, a little intro about Unity. I don’t think I need to tell you guys what Unity is. And he starts telling us about what type of games you can build with Unity, and he shows us Fusion Fall (an MMORPG game by Cartoon Network)…  “You can make an MMORPG with Unity? That is awesome! I wanna make my own MMO!” screams the excited reader. And then he slams that thought by telling the readers to check the credits. You need to clone yourself 79 times, according to the author, and send yourself to school to study various disciplines.

Okay, so no MMO, so what game are we gonna make, Mr. Author sir? Then he points us to games on Blurst and Wooglie. Casual games? I don’t know about you, but I love casual games, I mean that how I got into this business in the first place (read about how I stumbled into this whole game making business on Gamasutra, in case you’re interested, but you’re probably not, anyway, moving on). Okay, casual games it is then, because we need to “Walk before you can run (or double jump)”. And then it’s hammer time, head on over to the Unity website and download well, Unity, and then install install, and then launch!

And then he starts explaining the interface. I took down some notes:

(my “notes”, forgive my terrible handwriting)

And then we move on to the next chapter! Chapter 2: Let’s Start with the Sky. Are we going to be making skies? Read on, the author asks us, “It’s time to build a game, right? But how do you start? Where do you start?” Um, I don’t know, you tell me. He tells us, that every game starts with an idea. But the number of ideas you come up with doesn’t matter. “The one thing that separates you from success is not the number of ideas you’ve had or the number of projects you’ve started and abandoned, it’s the games you’ve finished that count.” Is that directed at me? I have a tendency to have a lot of ideas, starting a lot of projects, but never finishing them. That line really struck me, sir. Yes, sir, I will try to finish my games, okay not try, I WILL finish my games.

And then he talks about “The Siren Song of 3D” (how beautifully put). He says that before we even begin, the odds are already stacked against us! That’s a mean thing to say. He points out that games like GTA, Mario or Fall Out 3 is they have “an animated character or first-person camera moving around in a physics-based environment; a rich and detailed 3D world with maps… teams of hundreds of people burning through multimillion dollar budgets…” Okay, we get it, we can’t make MMOs and AAA quality games.

And then he starts talking about features vs content, and giving a lot of examples of games along the way. The summary: “By cutting features from your game ideas, you can whittle your design down to a more manageable size than by cutting content”. Okay, got it. Can we get started now? I want to make some games! (or at least one game, before the night is over).

Chapter 3: Game #1 Ticker Taker. Yeah, finally! So what game are we going to make? Volleyball. He wants us to make a volleyball game, I hate volleyball (or rather, I suck at vollyeball). But he did ask nicely for us to pretend that we are totally pumped about volleyball. Fyeahvollyeball!

*takes a break to do the laundry*

Okay, now where were we? Oh yeah, game, and volleyball. I love volleyball. Read, read, read. The author Is explaining the rules and the complexities of volleyball and he came to conclude: “You + volleyball game = badIdea.” I know right, really bad idea. But then he insists that volleyball is my dream, my passion, that is why we must strip volleyball down to it’s bear essentials and start from there. Okay, he instructs me to get “a red pen and/or machete”, gotcha. Okay, I have my machete, who do we kill? -Sets, teams, multiple players, net, referee, spectators, and then the player. Wait, what? We’re even killing the player? What’s left? A ball. A ball floating in midair. A ball that cannot touch the floor. And something to bounce the ball on. Okay, I can work with a ball (in fact, I think I can even make a 3D model of a ball in Blender).

So let’s go to Unity. Create new project and then add a new object to the scene, a sphere, which will serve as our ball. So we name him, Ball!

(more “notes”, before and after)

He says that our ball right now is between the ground (half above ground, half below ground). And we must move the ball up to the sky. We change the Y position of the ball to 2 (in the Inspector panel). Okay, that was easy. Next, he complains that our ball is too big, so we scale it to 0.4 for X, Y and Z. Okay. Next step? Save the scene.

Now, we have our ball, next is our paddle (the thing that we are going to have our ball bounce on). We create a cube (Game Object > Create Other > Cube), and we name him Paddle, and then we scale him, so he’ll look less like a cube and more like a paddle. Alright then.

And then we add a light to our scene.

(our ball and paddle used to be dark an dreary, now it is all lighted up. like a make up commercial)

And then we test our game! We hit Play. *those black crows from Japanese animes fly by* Nothing happens.

The author is pulling our leg, he wants us “to test your game at this point, even though nothing happened, to pinpoint that magic moment when Unity becomes awesome.” Okay, well, I can’t wait for things to become awesome.

Add physics to our game! Click on Ball in the Hierarchy panel, and then go to Component > Physics > Rigidbody, to add a Rigidbody to our Ball. And make sure that Gravity is checked in the Inspector panel. And we could hit play again (at least he is telling us to hit play again). Our ball fell down and hit the paddle! Whee! It was “especially cool” (he says I can get ten points, if I said that that was especially cool).

Next, we want our ball to bounce. Because, in real life, ball bounce, right? At least volleyballs bounce (I know that much about volleyballs). So we set the Material property of the Sphere Collider of our ball to Bouncy. And press play again, and it is magically bouncy! “Especially cool”! And that’s the end of chapter 3.

But the ball just keeps bouncing and we can’t do anything. So we have to move on to the next chapter, Chapter 4: Code Comfort. So it’s time for some coding. Nervous? But the advertisement says that: “Build fun games even if you’ve never coded before”. I guess this is the part that will decide whether a book can really teach someone to make a game. Well, it’s 37 pages of condensed lesson in scripting.

The question at the back of my head: do I really want to read about scripting?

*will rest a bit tonight, will take on that chapter tomorrow*

Next day, a new chapter. Chapter 4:  Code Comfort. Here we go! I’m just gonna read it through for and give you guys a short review of what I read.

*reads*

Okay, done. This chapter starts from the basics, in an easy to read and easy to understand way for the readers, whom the author assumes has no background at all in coding. For one thing, he compares functions to a hoagie, the curly brackets being the buns, and the scripts inside as well, the tomato, the salami, the mayo. And a variable to a bucket, which contains things, you know. He also teaches the readers how to access the Unity Manual and Scripting guide and how to make sense of it. He also lets the readers try out code every step of the way. All in all, the author is actually quite a fun read, because of his wit and humor, and it is nothing like my Computer Programming 1 professor, who tends to drone. So, I’m done with Chapter 4.

I do realize that this book has 380 something pages and 12 chapters. And this blog post will go on like forever If I continue in this fashion. So um, I’m going to stop here.

Overall, I think this book is easy read, it’s not intimidating in such a way that it bombards you with information, code bits that you can’t even begin to grasp. I think that beginners, even those who have “never coded before” would find this book easy to digest, and might even be able to “build fun games”. For those who are interested in the book, you can check it here. The book actual offers samples codes and projects that you can download even if you haven’t bought the book, here.

By the way, you can also read my review of Cocos2d for iPhone 0.99 Beginner’s Guide here. But my conclusion after reading that books is: “Overall, I think this book is a very comprehensive guide to anyone, who has a bit of background in programming, and wants to start making games using Cocos2D.” Note, I said that it will be a good guide to anyone with a bit of background in programming.

So can someone who has never coded before learn how to make games by reading a book? That was the question at the beginning of this blog post. I guess it’s hard to tell, I’ll have to say it’s:

( sign from Mythbusters)

By the way, Packt Publishing gave me these books for free to review. Why? It’ because I’m a blogger and I occasionally blog about Cocos2d stuff. You can get free books too, if you are a blogger/ webmaster/ reviewer for a magazine or a popular and relevant website / educator in an academic institution, just email you details to reviewrequest@packtpub.com. If you’re not those, you can also exchange an original article that you wrote of course, for a free book. More details on the Packt website.

How to make a First Person Shooter Part 1

Recipe style~!

Part 1: Preparation

Ingredients:

1 weapon (preferably a gun, I picked an AK47)

1 environment (any will do, I picked a military camp environment)

1 shoot target (enemy, or something to shoot at)

1 3D program (I’m using 3ds Max, but Blender would do too)

1 2D program (like Photoshop, Gimp, Paint.net, I’m sorry but MS Paint, won’t do, I’ll explain later)

1 game engine (I’m using Unity, you should too)

some scripts

Directions:

1. Pick up some models (3D models, that is) from your own collection, or from TurboSquid (as I talked about in a previous post, it’s a website with a wide collection of 3D models, and a lot of them are free).

You need to register or sign in to download. When looking for models, take note of the file types (.3ds, .max, .c4d, .blend, .obj, .fbx, etc.), because different 3D programs open different kinds of files, such as .3ds and .maxare for 3ds Max, while .blend is for Blender, and .c4d is for Cinema4D. I just found out that TurboSquid even offers free file format conversion services (not available for free and model collections, though). I suggest you go for .obj and .fbx files, most programs can open them, also they can be imported into the engine, as is.

I downloaded an AK47 by robbiedoes, an US military camp by Jonfletc, and a shooting target by Benjamin vdZ.

2. Fix the models if you have to. Unity can import Max and Maya files. But sometimes, if you have an old .max file, it might not import correctly, so what I do is I open them in 3ds Max and export it as .fbx. 

When you open old .max files, you might encounter some errors.

Just ignore them (I’m serious, just click Continue or OK).

If you don’t see the texture (how do I explain textures? oh found a definition here) of your model, click M and the Material Editor will appear, go to Maps, and click on the file name next to Diffuse, and you might have to change the directory of the file, so it can be read, and voila~!

Now, we can export. Click on the Max icon, go to Export, then Export Selected.

I missed a screen (but my Max trial ended and I can’t go back to make a screenshot). Anyways, there’s another screen before that has all sorts of settings, just click OK. If you encounter any errors, just ignore it again.

Now we have a .fbx file version of our models that we are sure that will work in Unity.

3. Open Unity (if you don’t have Unity yet, go to their website and download it for free). 

Once Unity is open, go to File,and then New Project to make a new project.

Now that we have a new project, we can officially get started (finally~!).

4. First we need to create the environment, our player would move around in. To do this, we create a terrain. Click Terrain, and then Create Terrain. To add a texture to your terrain, in the Inspector (on the right side, usually), under Terrain Script, look for a pain brush icon, and click on that, that’s the Paint Texture button, and then under Textures, click on Edit Textures, a popup window will appear, where you can pick a texture for you terrain.

Now, we have a textured terrain.

5. We continue setting up our environment. Remember the models we got from TurboSquid? Now let’s get them into our engine.

How? Just click and drag! Open the folder where your .fbx file is, and click on the file and drag it into your already open Unity engine (in the Project area). And it’ll import it, it might take a while though, depending on the size of your model. Oh, also don’t forget to also drag in your texture files.

Of course, you can also go to Assets, and then click on Import New Asset…

6. Do the same thing for the rest of your 3D models. Importing, I mean.

Stay tuned for the next part, where we actually start making our game :)

Tweet me for comment and suggestions :)

2D shooter experiment in 3D


2D shooter experiment in 3D

Tools of Trade

Well, if we’re going to make games, we’re going to need some tools.

And since we’re prolly all hobbyists and indies (meaning, we make games for fun, and not for money), and that is also why, we prolly don’t have the money to buy all sorts of fancy software. No Photoshop, guys, unless you are willing to shell out, $699, is it? No Maya or 3DS Max, either, ‘coz those are really expensive, their website says, it’s $3,495. Now, I have no idea where to get that money. I mean, sure, I have a day job, but it would take half a year’s worth of salary to even buy one of those softwares. So the solution: OPEN SOURCE and FREE softwares.

There are actually some pretty cool open source softwares out there. And they will be our TOOLS OF TRADE.

Okay, now to make a game, the first thing we need is a GAME ENGINE, ‘coz we’re not that hardcore nor nerdy, I mean, why code from scratch, when there are game engines that can make your life easier? Our game engine of choice is UNITY.


Website Link: http://www.unity3d.com/

Now, I picked Unity, ‘coz I have been working with Unity for a year now, and I find that it is awesome! And the best bit is: it’s free! And you can build your game as a standalone game or web game, it comes with a shiny webplayer plug in that is only around 3 mb. And if you’re willing to shell out a bit, around $399, and if you have OS X, you can also publish your games on the iPhone. And if you’re willing to shell even a bit more, you can always contact the Unity people for a Wii standalone and a WiiWare license.

Next up, we would need at least a 3D modeling software or a Paint like software. That depends on if we’re going 3D or simply stick with 2D sprites. Both is good, I, I’m not really sure, what I prefer better. I guess I’m okay with both.

But anyway, for 3D, I’ll be going with BLENDER.

Website Link: http://www.blender.org/

Because I took up a course on Blender back in college, and oh, it’s free and open source. It could be a bit tricky at first, but once you get to all it’s shortcuts (lots of keyboard shortcuts that needs mastering), it’s pretty easy. And you can always go to the Blender site for tutorials.

Now, for a 2D, basic image and photo manipulation, could also be used for drawing stuff, I’m going to suggest you check out Paint.Net and GIMP.

Website Link: http://www.getpaint.net/index.html


Website Link: http://www.gimp.org/

I have to admit that I am not very fluent with these software, ‘coz at work, of course, I can use Photoshop, and am quite spoiled by it. But since, I can’t get Photoshop for my homebrew stuff, I would have to learn these free opensource software. I have tried Paint.net, while I just downloaded GIMP. So maybe we can learn these things together.

So, go and download those softwares, so we can get started!